I do not understand why when dragging an image up and down, that the image
lags behind the drag. I have tried changing the line:
y=arg1.getY()-iv.getWidth()*2;
in the following code with no luck.
Some numbers for multiplication or division attempts in the line make the
image vanish after letting go of the drag.
Can someone explain why these things are happening and how to correct them?
Thanks in advance.
public class MoveImages extends AppCompatActivity implements
View.OnTouchListener{
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_images);
iv=(ImageView)findViewById(R.id.iv);
iv.setOnTouchListener(this);
}
float x,y=0.0f;
boolean moving=false;
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch(arg1.getAction()){
case MotionEvent.ACTION_DOWN:
moving=true;
break;
case MotionEvent.ACTION_MOVE:
if(moving){
// x=arg1.getRawX()-iv.getWidth()/2;
x=arg1.getRawX()-iv.getWidth()/4;
//y=arg1.getY()-iv.getWidth()*3/2;
y=arg1.getY()-iv.getWidth()*2;
iv.setX(x);
iv.setY(y);
}
break;
case MotionEvent.ACTION_UP:
moving=false;
break;
}
return true;
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/8b0c1837-d828-4cd7-b086-be4ec119f35a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.