While Dragging Or Moving the Image, it going outside of Screen
<p>I am creating a app to drag the image. But while dragging, it going outside of screen. I want to fix this so it can move only inside the screen. below is the code which I tried to Drag the image. It working fine but Only moving outside of Screen also. Some code I tried to from internet which are comment in event section but it also doesn't work. Let me know if any other further information is required here to explain this.</p>

<pre><code>public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        ImageView view = (ImageView)v;
        int rotation = 25;
          dumpEvent(event);


          // Handle touch events here...
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {


          case MotionEvent.ACTION_DOWN:

             savedMatrix.set(matrix);
             start.set(event.getX(), event.getY());
             Log.d(TAG, "mode=DRAG");
             mode = DRAG;
             break;



          case MotionEvent.ACTION_POINTER_DOWN:


             oldDist = spacing(event);
             Log.d(TAG, "oldDist=" + oldDist);
             if (oldDist &gt; 10f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                mode = ZOOM;
                Log.d(TAG, "mode=ZOOM");
             }
             break;

         case MotionEvent.ACTION_UP:

             mode = NONE;
             Log.d(TAG, "mode=NONE");
             savedMatrix.set(matrix);
           //  matrix.postRotate(90);

             break;


          case MotionEvent.ACTION_MOVE:
             if (mode == DRAG) {
                // To dragging the image

                 screenHight = displaymetrics.heightPixels;
                screenWidth = displaymetrics.widthPixels;

                 float newX = event.getX() - start.x;
                  float newY = event.getY() - start.y;
                  Log.v("X","new X: "+start.x);
                  Log.v("Y","new Y:"+start.y);

              //   if ( (newX &lt; 0 || newX &gt;= screenWidth) ||
               //        (newY &lt; 0 || newY &gt;= screenHight) )
               //      break;
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - start.x,
                      event.getY() - start.y);
             }
             else if (mode == ZOOM) {
                float newDist = spacing(event);
                Log.d(TAG, "newDist=" + newDist);
                if (newDist &gt; 10f) {
                   matrix.set(savedMatrix);
                   float scale = newDist / oldDist;
                   matrix.postScale(scale, scale, mid.x, mid.y);
                }
             }
             break;
          }

          view.setImageMatrix(matrix);
          return true; // indicate event was handled
    }
</code></pre>
