Every thing is fine in first time when I move ImageView
on the screen, but in second time ImageView
doesn't move properly.
This is what I have done so far.
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams mParams = (FrameLayout.LayoutParams) img.getLayoutParams();
int x = (int) event.getRawX();
int y = (int) event.getRawY();
mParams.leftMargin = x-50;
mParams.topMargin = y-50;
img.setLayoutParams(mParams);
break;
case MotionEvent.ACTION_DOWN:
x1=img.getX();
y1=img.getY();
break;
case MotionEvent.ACTION_UP:
img.setX(x1);
img.setY(y1);
break;
default:
break;
}
return true;
}
});
The following is the case what I think it might work.
I saw you're using img.getX()
, img.getY()
,
so I assume you're using API Level 11 or above.
And I assume your img
is the instance of ImageView
.
( The usage of FrameLayout.LayoutParams
for ImageView is wierd though... )
The following is how to make it properly:
img.setOnTouchListener(new OnTouchListener()
{
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE :
img.setX((int)(StartPT.x + event.getX() - DownPT.x));
img.setY((int)(StartPT.y + event.getY() - DownPT.y));
StartPT.set( img.getX(), img.getY() );
break;
case MotionEvent.ACTION_DOWN :
DownPT.set( event.getX(), event.getY() );
StartPT.set( img.getX(), img.getY() );
break;
case MotionEvent.ACTION_UP :
// Nothing have to do
break;
default :
break;
}
return true;
}
});
========================================================================
========================== [2013/05/15 Added ] =============================
========================================================================
The new object presented here is PointF
.
Please use the following code to import PointF
object :
import android.graphics.PointF;
And actually, this is just an object for recording float x and float y. If you really can not import that object, write one yourself like the following :
public class PointF
{
public float x = 0;
public float y = 0;
public PointF(){};
public PointF( float _x, float _y ){ x = _x; y = _y; }
public void set( float _x, float _y ){ x = _x; y = _y; }
}
This code helps you to drag the imageview within the boundry of the screen. Hope this will help.
@Override
public boolean onTouch(View v, MotionEvent event) {
int X = (int) event.getRawX();
int Y = (int) event.getRawY();
RelativeLayout.LayoutParams paramsnew = (RelativeLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
_xDelta = X - paramsnew.leftMargin;
_yDelta = Y - paramsnew.topMargin;
imgposx = img.getX();
imgposy = img.getY();
break;
case MotionEvent.ACTION_UP:
img.setX(diyoposx);
img.setY( diyoposy );
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) v.getLayoutParams();
param.leftMargin = X - _xDelta;
param.topMargin = Y - _yDelta;
param.rightMargin=(250*-1);
param.bottomMargin=(250*-1);
v.setLayoutParams(param);
break;
}
mViewGroup.invalidate(); }
Here mViewGroup is the ViewGroup Instance of Relative layout that bounds the ImageView
mViewGroup= (ViewGroup) findViewById(R.id.relativeLayout);
Also, imgposx, imgposy are the global varaible
float imgposx, imgposy;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With