title: Android Animation: How to move a view smoothly along with finger and move to its initial position if condition is not satisfied
body: <p>I want to move a view along with movement of finger smoothly, then I want to check the distance moved, if the distance moved is less than expected distance then I have to move the view back to its initial position.</p>
  
  <p>I have searched a lot, and I have arrived at the below code, but this is not working as expected, when I touch it shows two views and move haphazardly not according to my finger movement.</p>
  
  <pre><code>myView.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View view, MotionEvent motionEvent) {
   float dX = 0, dY = 0;
   originalX = myView.getX();
   originalY = myView.getY();
  switch (motionEvent.getAction()) {
  case MotionEvent.ACTION_DOWN:
   x1 = motionEvent.getX();
   break;
  case MotionEvent.ACTION_UP:
  x2 = motionEvent.getX();
  float deltaX = x2 - x1;
  if (Math.abs(deltaX) &gt; MIN_DISTANCE) {
   myView.animate()
         .alpha(0.0f)
         .x(motionEvent.getRawX())
         .y(motionEvent.getRawY())
         .setDuration(500)
         .setListener(new Animator.AnimatorListener() {
   @Override
   public void onAnimationStart(Animator animator) {
  }
  @Override
  public void onAnimationEnd(Animator animator) {
   myView.setVisibility(View.GONE);
   }
  @Override
  public void onAnimationCancel(Animator animator) {
   }
   @Override
  public void onAnimationRepeat(Animator animator) {
   }
  })
  .start();
  }else {
  myView.setX(originalX);
  myView.setY(originalY);
  }
  break;
   case MotionEvent.ACTION_MOVE:
  myView.setX(motionEvent.getX());
  myView.setY(motionEvent.getY());
  break;
   }
  return true;
   }
      });
  </code></pre>
  
  <p>Please help me how can I get this correct.</p>