I'm trying my hand at some simple photo manipulation on an image at the top of an `activity` within an `ImageView`, which on average has a `200dp` height and matches the width of its parent. 

I have implemented a custom `onTouchListener` class, which creates a `matrix` and translates the image within the `ImageView` 

**Drag**:

```
case MotionEvent.ACTION_MOVE:
     if (mode == DRAG) {
        matrix.set(savedMatrix);
        matrix.postTranslate(v.getX(), event.getY() - start.y);
     }
     break;

```

This works perfectly, it drags the image on the *y axis* only. But when the user releases I want the image to crop down to the bounds of the visible `ImageView` area. 

**Crop**:

```
case MotionEvent.ACTION_UP:
     if (mode == DRAG) {
        Bitmap bitmap = drawableToBitmap(view.getDrawable());
        Bitmap result = Bitmap.createBitmap(bitmap,view.getLeft(), view.getTop(), view.getWidth(), view.getHeight());
        view.setImageBitmap(result);
      }
      break;

```

This crops the image to the right size, but only from the top of background of the imageview not the visible boundaries. With the call to the `Bitmap` class function `createBitmap(int x, int y, int width, int height)` my only assumption would be `view.getTop()` is making it start at the top. 

So, my question is: How could I calculate the difference between the top of the visible view bounds and the background/matrix boundaries?

[![The dark blue area is the ImageView](https://i.stack.imgur.com/9zOYQ.png)](https://i.stack.imgur.com/9zOYQ.png)

Note: The image displays an image (blue) which is located at the top of an Activity (the cyan looking areas are just the background invisible boundaries of the `ImageView`, not the activity)

I would like to emphasize that I don't wish to use any third party libraries to achieve this.

I'm more than willing to provide additional code, if need be.

Update:

Full `onTouch` method:

```
@Override
public boolean onTouch(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    // Dump touch event to log
    dumpEvent(event);

    // Handle touch events here...
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(v.getX(), event.getY());
            mode = DRAG;
            originalY.y = (int) event.getY();
            break;
        case MotionEvent.ACTION_UP:
             if (mode == DRAG) {
        Bitmap bitmap = drawableToBitmap(view.getDrawable());
        Bitmap result = Bitmap.createBitmap(bitmap,view.getLeft(), view.getTop(), view.getWidth(), view.getHeight());
        view.setImageBitmap(result);
      }
      break;
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
                matrix.set(savedMatrix);
                matrix.postTranslate(v.getX(), event.getY() - start.y);
            }
            break;
    }
    view.setImageMatrix(matrix);
    return true; // indicate event was handled
}

```