can any one help me how to drag and drop the text view on entire image view. i found from this drag n drop textview in android here the text view is static, in my app textview and image view's are dynamically change. `#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
i wright this code, but it's not working properly.
You can always change the image and text of existing view programatically using setText sort of methods.
There are many drag and drop tutrials if you google it. strongly suggest you try them out.
Basically what you do is implement a OnTouchListener on the TextView like
textview.setOntouchListener(listen);
OnTouchListener onThumbTouch = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(v.getId())
{
case R.id.slider_thumb_id:
{
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
}
case MotionEvent.ACTION_UP:
{
}
}
break;
}
}
return true;
}
};
Here from the event attribute you get the value of x and y and the touch event and
use that to alter the position of the textview. I did it by add add/subtracting the margin values.
EDIT:
This is what I did to update the position of my view
Look event.getRawY(); is going to give you the distance from the parent view. So if I get event.getRawY() = 50. I know I need the text view to be 50 pixels from the top.
LinearLayout.LayoutParams iconParams = (LinearLayout.LayoutParams)
v.getLayoutParams();
iconParams.topMargin = 50;
v.setLayoutParams(iconParams);
So now the view that i selected is 50 px from the top of the parent view.
Related
I've created an OnTouchListener that will allow me to drag views around the screen. This works well, except for one problem:
My views resize instead of leaving the parent. They are in a RelativeLayout, which I suspect is contributing to the problem. They only resize when touching the right or bottom boundaries of my RelativeLayout.
I'm hoping there is a simple solution to this problem, (ideally not involving resizing the RelativeLayout), but I haven't found a solution yet.
Ideally, this solution would be applicable for all views, as I use this OnTouchListener for TextViews as well as ImageViews. I was hoping to find an XML solution. I'm assuming others have dealt with this issue??
PS: Before I post this..I've just remembered I believe there is a flag for hitting a RelativeLayout's border, so maybe I can apply negative margin to the TextView when that happens. Thoughts?
EDIT
Here is my OnTouchListener:
// Dragging functionality for all views and double tap to remove
// functionality for ImageViews
final OnTouchListener onTouchListener = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
v.bringToFront();
//if(v instanceof android.widget.ImageView)
//mScaleDetector.onTouchEvent(event);
layoutParams = (LayoutParams) v.getLayoutParams();
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
// Where the user started the drag
pressed_x = (int) event.getRawX();
pressed_y = (int) event.getRawY();
if (v instanceof android.widget.ImageView)
{
curTime = System.currentTimeMillis();
if(curTime - prevTime <= DOUBLE_TAP_INTERVAL)
{
v.setVisibility(View.GONE);
}
prevTime = curTime;
}
case MotionEvent.ACTION_MOVE:
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
dx = x - pressed_x;
dy = y - pressed_y;
// Update the margins
layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;
v.setLayoutParams(layoutParams);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x = x;
pressed_y = y;
break;
default:
break;
}
return true;
}
}
I have a GridView with images. On long click the screen gets dark (with black half transparent image that gets visible) and another image become visible that has the same resource of the image long clicked from the grid.
What i want is to be able to drag that image (which i have succeeded), but the image shows at the top corner (as i designed the layout in the XML) and I want it to show up where I clicked (To be precised, i want the center of the dragable picture to be where I perfumed the long click).
My image is fill_parent on FrameLayout and is set on matrix scale to control position...
this is the image part of the XML:
<ImageView
android:id="#+id/imgMainSelectedMovie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:visibility="invisible"
android:src="#drawable/escape" />
and this is the OnTouch() part:
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
X = ev.getX();
Y = ev.getY();
if (longClicked) return true;
break;
case MotionEvent.ACTION_MOVE:
if (longClicked) {
if (!dragStarted) {
dragStarted = true;
myImageView.setX(X);
myImageView.setY(Y);
}
matrix.postTranslate((ev.getX() - X), (ev.getY() - Y));
X = ev.getX();
Y = ev.getY();
myImageView.setImageMatrix(matrix);
return true;
}
break;
case MotionEvent.ACTION_UP:
if (longClicked) {
dark.setVisibility(View.INVISIBLE);
myImageView.setVisibility(View.INVISIBLE);
longClicked = false;
return true;
}
}
return false;
}
thist is what I tried to solve the problem:
if (!dragStarted) {
dragStarted = true; //Changed to true when OnItemLongClick called
myImageView.setX(X);
myImageView.setY(Y);
}
But it only changes the "start point" of the image layout to the entered X and Y.
Problem solved :)
What I did is when onItemLongClick calls I took the X and Y measured from the ACTION_DOWN case in the onTouch, took the matrix current location and did postTranslate to the coordinates from the ACTION_DOWN minus the location of the image.
this is the solution part from the onItemLongCliick:
matrix.getValues(values);
matrixX = values[Matrix.MTRANS_X];
matrixY = values[Matrix.MTRANS_Y];
matrix.postTranslate(X-matrixX, Y-matrixY);
myImageView.setImageMatrix(matrix);
I'm wondering why they didn't put some kind of "setPlace" method for matrix...
I am guessing this means making the whole screen touchable. How exactly is that done?
and secondly calculating if an X,Y is within the EditText.
override the onTouchEvent in the activity you want to implement this....and get the X, Y co-ordinates using event.getX() and event.getY()
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
but i suggest you must search thoroughly before posting a question.
I would like to know if it is possible to get the coordinates, left and top, of an ImageView.
I have 2 ImageView inside a RelativeLayout inside a ScrollView.
I tried to retrieve the matrix of the ImageView with matrix = _iv.getImageMatrix(); but it is not helpful Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}.
Any ideas? Would it make a difference if the getImageMatix() is run in a different place?
Here's a better way to do it. I implemented an OnTouchListener in the ImageView itself and the X,Y coordinates are corresponding to the ImageView.
_iv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//Choose which motion action has been performed
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//Get X, Y coordinates from the ImageView
int X = (int) event.getX();
int Y = (int) event.getY();
//Do something
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}); //End setOnTouchListner()
Is there a way in android to get the area (a collection of points) of my finger when i touch the screen. Usually, the return is one point location. I need to get the several points which can represent the area of my touching. Any one knows? Thnaks
Just use View class's onTouchEvent() or implements OnTouchListener in your activity and get the X and Y co-ordinates like,
This is View class's OnTouchEvent()..
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
float x = ev.getX();
float y = ev.getY();
break;
}
case MotionEvent.ACTION_MOVE: {
float mLastTouchX = x;
float mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL: {
break;
}
case MotionEvent.ACTION_POINTER_UP: {
break;
}
}
return true;
}
EDIT:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
For more details Android - View.
Does it have to be the real shape of the finger? Otherwise you could calculate a circle around the center of the touch.
I believe this is not possible in the touch's coordinate along with it's area. It's natively unsupported by Android API.
Ref:
http://developer.android.com/guide/topics/ui/ui-events.html
http://developer.android.com/reference/android/gesture/package-summary.html
Unfortunately not. I ran in to this a while ago - you could try normalising the data over several 'frames'. May be worth trying to figure out why you need an exact point, or set of points, and find another way of doing it.