title: Adding natural dragging effect to ImageView same as Facebook Messanger chat heads using Rebound library
body: <p>I am developing one app where I am dragging around my ImageView in Activity.
  I have configured <a href="http://facebook.github.io/rebound/" rel="nofollow"><strong>Facebook Rebound library</strong></a> for spring animation which is originally used in Facebook Messenger's chat heads animations. I want to add this kind of animations to my ImageView when I drag it.
    <a href="https://www.youtube.com/watch?v=ECLkrDfDAG4" rel="nofollow"><strong>VIDEO</strong> </a> </p>
  
  <p>So far I am able to get spring animation when I touch ImageView (implemented spring on rootview), this is my code. How can I implement that natural type of dragging effect to my ImageView. </p>
  
  <pre><code>public class MainTry extends Activity {
  
      int windowwidth;
      int windowheight;
  
      private LayoutParams layoutParams;
      private final BaseSpringSystem mSpringSystem = SpringSystem.create();
      private FrameLayout mRootView;
      private Spring spring;
      private View mImageView;
      private VelocityTracker velocity = null;
      private float dx;
      private float dy;
      private View rootView;
      private ImageView img;
  
      @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout);
  
      // Create a system to run the physics loop for a set of springs.
      SpringSystem springSystem = SpringSystem.create();
  
      // Add a spring to the system.
      spring = springSystem.createSpring();
  
      rootView = getWindow().getDecorView()
              .findViewById(android.R.id.content);
  
      windowwidth = getWindowManager().getDefaultDisplay().getWidth();
      windowheight = getWindowManager().getDefaultDisplay().getHeight();
      img = (ImageView) findViewById(R.id.imageView2);
  
      rootView.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  // spring.setEndValue(1);
                  break;
              case MotionEvent.ACTION_UP:
              case MotionEvent.ACTION_CANCEL:
                  // spring.setEndValue(0);
                  break;
              }
              return true;
          }
      });
      // Add a listener to observe the motion of the spring.
      spring.addListener(new SimpleSpringListener() {
  
          @Override
          public void onSpringUpdate(Spring spring) {
              // You can observe the updates in the spring
              // state by asking its current value in onSpringUpdate.
              float value = (float) spring.getCurrentValue();
              float scale = .5f - (value * 0.1f);
              img.setScaleX(scale);
              img.setScaleY(scale);
          }
      });
      // spring.setEndValue(1);
  
      img.setOnTouchListener(new View.OnTouchListener() {
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              LayoutParams layoutParams = (LayoutParams) img
                      .getLayoutParams();
  
              switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  dx = event.getRawX() - 10;
                  dy = event.getRawY() - 10;
  
                  if (velocity == null) {
                      // Retrieve a new VelocityTracker object to watch the
                      // velocity of a motion.
                      velocity = VelocityTracker.obtain();
                  } else {
                      // Reset the velocity tracker back to its initial state.
                      velocity.clear();
                  }
                  break;
              case MotionEvent.ACTION_MOVE:
                  dx = event.getRawX() - 10;
                  dy = event.getRawY() - 10;
  
  
                  velocity.addMovement(event);
                  spring.setVelocity(velocity.getYVelocity());
                  spring.setCurrentValue(dy);
                  spring.setEndValue(dy);
  
  
                  layoutParams.leftMargin = (int) dx - 10;
                  layoutParams.topMargin = (int) dy - 10;
                  img.setLayoutParams(layoutParams);
                  break;
              case MotionEvent.ACTION_CANCEL:
                  break;
              case MotionEvent.ACTION_UP:
                  velocity.addMovement(event);
                  spring.setVelocity(velocity.getYVelocity());
                  spring.setCurrentValue(event.getRawY() - 10);
                  spring.setEndValue(0);
  
                  break;
  
              default:
                  break;
              }
              return true;
          }
      });
  
  }
  
  @Override
  public void onResume() {
      super.onResume();
  }
  
  @Override
  public void onPause() {
      super.onPause();
  }
  
  
  }
  </code></pre>