I am creating an application that uses scaling and drag and drop. The problem is that at the moment I can get the image out of it. I wanted to know how I can use drag and drop and make sure that the image remains on the screen at the same time.
Here is my code:
public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView)v; //handle touch events here. switch (event.getAction() & 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 > 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; Log.d(TAG, "mode=ZOOM" ); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; Log.d(TAG, "mode=NONE" ); break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { 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 > 10f) { matrix.set(savedMatrix); Float scale = newDist / oldDist; Matrix temp = new Matrix(); temp.set(matrix); temp.postScale(scale, scale, mid.x, mid.y); float[] f = new float[9]; temp.getValues(f); Float xScale = f[0]; if(xScale >= 1 && xScale <= 10){ matrix.postScale(scale, scale, mid.x, mid.y); savedMatrixZoom.set(matrix); }else{ matrix.set(savedMatrixZoom); } } break; } } //perform the transformation. view.setImageMatrix(matrix); return true; // indicate event was handled }
source share