title: Android Move ImageView
body: <p>I searched the web for a simple solution to freely move an <code>ImageView</code>. I finally found some code that produces the perfect result:</p>
  
  <pre><code>public class MainActivity extends Activity implements View.OnTouchListener {
  
      private int _xDelta;
      private int _yDelta;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ImageView j = (ImageView)findViewById(R.id.j);
  
          j.setOnTouchListener(this);
  
      }
  
      public boolean onTouch(View view, MotionEvent event) {
          final int X = (int) event.getRawX();
          final int Y = (int) event.getRawY();
          ImageView j = (ImageView)findViewById(R.id.j);
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_DOWN:
                  RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                  _xDelta = (int) (X - j.getTranslationX());
                  _yDelta = (int) (Y - j.getTranslationY());
                  break;
              case MotionEvent.ACTION_UP:
                  break;
              case MotionEvent.ACTION_POINTER_DOWN:
                  break;
              case MotionEvent.ACTION_POINTER_UP:
                  break;
              case MotionEvent.ACTION_MOVE:
                  RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  
                  j.setTranslationX(X - _xDelta);
                  j.setTranslationY(Y - _yDelta);
                  break;
          }
  
          return true;
      }}
  </code></pre>
  
  <p>Since I don't really know how my code works, I wanted to see if there were better solutions.</p>