Hoy的, 这是我的班级:
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);
}
}
问题是,它只适用于实际的捏合/缩放,而不适用于全球规模......我试图保存全球规模,每次我按比例扩大实际尺度乘以全球尺度,但这是用途..它接触到矩阵得到的尺度是全局的,但它不像一个。我究竟做错了什么?有人可以帮忙吗?谢谢你的回答。
答案 0 :(得分:1)
为什么没有人使用ScaleGestureDetector
来做这类事情?我正在研究一些非常相似的东西,并且正在使用一个扩展ScaleGestureDetector.SimpleOnScaleListener
的小助手类。
class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
float scaleFactor;
Matrix matrix;
public ScaleListener(){
super();
scaleFactor = 1.0f;
matrix = new Matrix();
}
public boolean onScale(ScaleGestureDetector detector){
//updating the scalefactor after the current gesture
scaleFactor *= detector.getScaleFactor();
//making sure the scale is within the limits
scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
//applying the scale via a matrix to the imageview
matrix.setScale(scaleFactor, scaleFactor);
imageview.setImageMatrix(matrix)
}
}
我对你关于全球规模的问题感到有些困惑,但矩阵/ scaleFactor包含了imageview的全局比例。希望这有帮助,即使它有点晚了!
答案 1 :(得分:0)
所以这就是我解决它的方式......它不是很优雅,但它有效
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
matrix.getValues(values);
if(values[0] < MAX_ZOOM && values[0] > MIN_ZOOM){
scale = newDist / oldDist;
scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
matrix.postScale(scale, scale, mid.x, mid.y);
reticle.setImageMatrix(matrix);
} else if(values[0] >= MAX_ZOOM){
newScale = newDist / oldDist;
if(newScale < scale){
scale = newScale;
scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
matrix.postScale(scale, scale, mid.x, mid.y);
reticle.setImageMatrix(matrix);
}
} else if(values[0] <= MIN_ZOOM){
newScale = newDist / oldDist;
if(newScale > scale){
scale = newScale;
scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
matrix.postScale(scale, scale, mid.x, mid.y);
reticle.setImageMatrix(matrix);
}
}
}
}
这是我编辑的唯一代码和平