title: Android - move an ImageView on screen (Like dragging)
body: <p>I'm trying to create an app that can move an <code>ImageView</code> on your device like dragging and when I put like 75% of the <code>ImageView</code> out of the screen show a <code>Toast</code> for example. I've been reading about <code>MotionEvent</code> and <code>onTouchListener</code> and I've followed this <a href="https://stackoverflow.com/questions/5894736/rotate-zoom-drag-image-in-android-imageview">question</a>, but it doesn't convince me.</p>
  
  <h2>Edit</h2>
  
  <p>My current code is :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
  
      int windowwidth;
      int windowheight;
      private ImageView mImageView;
      private ViewGroup mRrootLayout;
      private int _xDelta;
      private int _yDelta;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          DisplayMetrics displaymetrics = new DisplayMetrics();
          this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          windowwidth = displaymetrics.widthPixels;
          windowheight = displaymetrics.heightPixels;
          mRrootLayout = (ViewGroup) findViewById(R.id.root);
          mImageView = (ImageView) mRrootLayout.findViewById(R.id.im_move_zoom_rotate);
  
          RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
          mImageView.setLayoutParams(layoutParams);
          mImageView.setOnTouchListener(this);
      }
      public boolean onTouch(View view, MotionEvent event) {
          final int X = (int) event.getRawX();
          final int Y = (int) event.getRawY();
          if(X == 0){
              Toast.makeText(this, "OUT", Toast.LENGTH_SHORT).show();
          }
          else if (Y == 0){
              Toast.makeText(this, "OUT", Toast.LENGTH_SHORT).show();
          }
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_DOWN:
                  RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                  _xDelta = X - lParams.leftMargin;
                  _yDelta = Y - lParams.topMargin;
                  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();
                  layoutParams.leftMargin = X - _xDelta;
                  layoutParams.topMargin = Y - _yDelta;
                  layoutParams.rightMargin = -250;
                  layoutParams.bottomMargin = -250;
                  view.setLayoutParams(layoutParams);
                  break;
          }
          mRrootLayout.invalidate();
          return true;
      }
  }
  </code></pre>
  
  <p>I did those <code>if</code> and <code>else if</code> just to know if the <code>ImageView</code> is getting out of the device, on the left and right side of the device seems like it's okay, but I'd like to make it cleaner and not hardwritted, also I don't get the <code>LayoutParams(150,150)</code> why 150? Also I don't get why I have to create a <code>RelativeLayout.LayoutParams</code> and why I have to put </p>
  
  <pre><code>layoutParams.rightMargin = -250;
  layoutParams.bottomMargin = -250;
  </code></pre>
  
  <p>I did the <code>if/else if</code> because I want to delete when the user want to put the <code>ImageView</code> out of the device, so I need to control when he tries to, at the moment I only got it TOP/LEFT/RIGHT not down, I also get the dimensions of my device just to try if X or Y is the same as height or widht just show the <code>Toast</code> but it's not doing it correctly.</p>
  
  <p>Now my <code>ImageView</code> is the <code>ic_launcher</code> but it will be bigger (almost middle screen).</p>
  
  <h2>NOTE</h2>
  
  <p>If you know any other way to do that easier or cleanest, feel free to put it here, I don't care about my code, I can adapt it, I just want it to be clear and not hardcoded.</p>