嘿,这是我的课:
public class MultiTouchHandler implements OnTouchListener {
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private static final float MIN_ZOOM = 0.5f;
private static final float MAX_ZOOM = 2.0f;
private int mode = NONE;
private float oldDist;
private PointF mid;
private PointF start;
private Matrix matrix;
private Matrix savedMatrix;
private ImageView reticle;
private int imageWidth;
private int imageHeight;
private float[] values;
private boolean isTouched;
private float width;
private float height;
private float scale;
public MultiTouchHandler(ImageView v) {
reticle = v;
v.setOnTouchListener(this);
mid = new PointF();
start = new PointF();
imageWidth = v.getDrawable().getIntrinsicWidth();
imageHeight = v.getDrawable().getIntrinsicHeight();
matrix = new Matrix();
savedMatrix = new Matrix();
values = new float[9];
}
public boolean onTouch(View v, MotionEvent event) {
synchronized (this) {
if(reticle.getScaleType() != ScaleType.MATRIX){
reticle.setScaleType(ScaleType.MATRIX);
matrix.postTranslate(reticle.getWidth()/2 - imageWidth/2, reticle.getHeight()/2 - imageHeight/2);
reticle.setImageMatrix(matrix);
}
matrix.getValues(values);
width = values[0]*imageWidth;
height = values[4]*imageHeight;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
if(event.getX() > values[2] && event.getX() < values[2] + width &&
event.getY() > values[5] && event.getY() < values[5] + height){
start.set(event.getX(), event.getY());
isTouched = true;
}
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
isTouched = false;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG && isTouched) {
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);
scale = newDist / oldDist;
Log.v("scale", "" + scale);
scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
Log.v("new scale", "" + scale);
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
reticle.setImageMatrix(matrix);
return true;
}
}
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);
}
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);
}
}
问题是,它只适用于实际的捏合/缩放,而不适用于全球规模......我试图保存全球规模,每次我缩放时,我都会将实际规模乘以全球规模,但这是无用的......它接缝从矩阵中获得的比例是全局比例,但它不像一个比例。我究竟做错了什么?有人可以帮忙吗?感谢您的回答。