0

I am trying to build an app that has an imageview in it. The user can drag the imageview horizontally to some position on screen using below code:

@Override
    public boolean onTouchEvent(MotionEvent event){ 
       img.getLocationOnScreen(viewCoords);
       newimgpos[0]=event.getX()-(viewCoords[0]-img.getX());
       img.setX(newimgpos[0]);
    }

Now, I want that when user lifts up his finger off the screen, the imageview be moved back to x=0. I can directly set the x coordinate of the imageview to be zero but it looks like a very abrupt change; I want the imageview to move smoothly to x =0. Can this be done? I tried using below logic:

for(int i= (int)newimgpos[0]; i>=0; i--){
    img.setX(i);
}

It moves and I can see the actual movement; still it is not smooth enough.

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61

1 Answers1

0

You need to use Animation here. Check the following thread for more info: Android translate animation - permanently move View to new position using AnimationListener

Robert K.
  • 1,043
  • 1
  • 8
  • 20