0

嘿,这是我的课:

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);
}

}

问题是,它只适用于实际的捏合/缩放,而不适用于全球规模......我试图保存全球规模,每次我缩放时,我都会将实际规模乘以全球规模,但这是无用的......它接缝从矩阵中获得的比例是全局比例,但它不像一个比例。我究竟做错了什么?有人可以帮忙吗?感谢您的回答。

4

2 回答 2

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)
    }
}

我对你关于全局比例的问题有点困惑,但矩阵/比例因子包含图像视图的全局比例。希望这会有所帮助,即使它有点晚了!

于 2013-09-11T15:27:03.907 回答
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);
                         }
                     }
                  }
              }

这是我编辑的代码的唯一和平

于 2012-04-22T06:32:35.467 回答