Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Created August 29, 2013 14:40
Show Gist options
  • Save Kursulla/6378967 to your computer and use it in GitHub Desktop.
Save Kursulla/6378967 to your computer and use it in GitHub Desktop.
Zoom on ImageView.
package rs.webnet.locmar;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
public class ImageViewZoomListener implements OnTouchListener {
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private ImageView imageView;
private Context context;
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
private int mode = NONE;
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
private GestureDetector doubleTapGestureDetector;
private int zoomLevel = 1;
public ImageViewZoomListener(Context context) {
super();
this.context = context;
doubleTapGestureDetector = new GestureDetector(context, new DoubleTapGesureDetector());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
imageView = (ImageView) v;
doubleTapGestureDetector.onTouchEvent(event);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d("d", "ACTION_DOWN");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d("d", "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d("d", "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
Log.d("d", "ACTION_UP");
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d("d", "ACTION_POINTER_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.d("d", "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);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
Log.d("d", "Scale: " + scale);
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
imageView.setImageMatrix(matrix);
return true;
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
// private void dumpEvent(MotionEvent event) {
// String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
// StringBuilder sb = new StringBuilder();
// int action = event.getAction();
// int actionCode = action & MotionEvent.ACTION_MASK;
// sb.append("event ACTION_").append(names[actionCode]);
// if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
// sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
// sb.append(")");
// }
// sb.append("[");
// for (int i = 0; i < event.getPointerCount(); i++) {
// sb.append("#").append(i);
// sb.append("(pid ").append(event.getPointerId(i));
// sb.append(")=").append((int) event.getX(i));
// sb.append(",").append((int) event.getY(i));
// if (i + 1 < event.getPointerCount())
// sb.append(";");
// }
// sb.append("]");
// Log.d("d", sb.toString());
// }
class DoubleTapGesureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("Taghere..", "onDoubleTap " + imageView.getWidth());
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
// Display display = wm.getDefaultDisplay();
// int width = display.getWidth();
// int height = display.getHeight();
if (zoomLevel == 1) {
float scale = 2;
matrix.postScale(scale, scale, mid.x, mid.y);
imageView.setImageMatrix(matrix);
zoomLevel++;
} else {
float scale = (float) 0.5;
matrix.postScale(scale, scale, mid.x, mid.y);
imageView.setImageMatrix(matrix);
zoomLevel--;
}
Log.i("d", "width: " + imageView.getMeasuredHeight());
return true;
}
}
}
//add to imageview
imageView.setOnTouchListener(new ImageViewZoomListener(SomeActivity.this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment