i need a solution for implement drag and zoom , routate ImageView around screen onTouch event without using matrix. i searched a lot but all answers done it with matrix and because i have multi objects in screen matrix is not suitable for me, i want drag and zoom ImageView object not matrix in the ImageView, after some try i implement a class that drag view fine but in zoom it disturb, thanks if someone can help me and sorry for bad language skills and if i have misspelling.
package com.example.painting1;
import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class Drag implements OnTouchListener {
ImageView img;
// We can be in one of these 3 states
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
private PointF mid = new PointF();
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
float oldDist = 1f;
public Drag( ImageView img ) {
// TODO Auto-generated constructor stub
this.img = img;
img.setOnTouchListener(this);
}
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);
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
int evid = event.getAction();
int moid = MotionEvent.ACTION_MASK;
switch (evid & moid)
{
case MotionEvent.ACTION_MOVE :
if (mode == DRAG) {
PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
img.setX((int)(StartPT.x+mv.x));
img.setY((int)(StartPT.y+mv.y));
StartPT = new PointF( img.getX(), img.getY() );
}
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
//matrix.set(savedMatrix);
float scale = (newDist / oldDist);
//matrix.postScale(scale, scale, mid.x, mid.y);
img.layout((int)scale, (int)scale, (int)mid.x, (int)mid.y);
img.setScaleX(scale);
img.setScaleY(scale);
}
}
break;
case MotionEvent.ACTION_DOWN :
mode = DRAG;
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF( img.getX(), img.getY() );
break;
case MotionEvent.ACTION_UP :
// Nothing have to do
case MotionEvent.ACTION_POINTER_UP :
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN :
oldDist = spacing(event);
if (oldDist > 10f) {
//savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
default :
break;
}
return true;
}
}