3

我正在使用 Hello Android 第 3 版中的代码来实现捏到缩放功能。使用此捏缩放工作正常,但缩放后我想获得图像视图上的绝对点击位置。

这是我的代码

package org.example.touch;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.GridView;
import android.widget.ImageView;

public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";

private static final float MIN_ZOOM = 1.0f;
private static final float MAX_ZOOM = 5.0f;

// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

ImageView image2;
image2 = (ImageView)this.findViewById(R.id.imageView2);
image2.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {

                     ImageView view = (ImageView) image2;
                      // Handle touch events here...
                        switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            savedMatrix.set(matrix);
                            start.set(event.getX(), event.getY());
                            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;
                            break;
                        case MotionEvent.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;
                                    matrix.postScale(scale, scale, mid.x, mid.y);
                                }
                            }
                            break;
                        }
                        view.setImageMatrix(matrix);
}

        /** 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);
        }
4

2 回答 2

2

您可以使用当前应用于图像视图的矩阵将点映射到原始默认点/从原始默认点映射点。

//points from a touch/click event on the zoomed/translated imageview
float X;
float Y;

float initialPoints = new float[]{X, Y};

//invert the matrix and map points back
    if(transformMatrix.invert(inverseTransformMatrix))
            inverseTransformMatrix.mapPoints(initialFocalPoints);

initialPoints现在包含原始点,即如果没有矩阵应用于图像视图,点击点将在哪里

编辑:应该在某处添加它,这样你就知道它们是什么了。

Matrix inverseTransformMatrix = new Matrix();
Matrix transformMatrix = new Matrix();
于 2013-09-11T15:35:31.660 回答
0

应用以下逻辑

float[] values = new float[9];
matrix.getValues(values);

在捏缩放之前计算比例因子;

scaleX=values[0];
scaleY=vales[4];

现在在捏缩放后根据iamge计算相对触摸坐标

float relativeX = ((event.getX() - values[2])*sacleX) / values[0];
float relativeY = ((event.getY() - values[5])*scaleY) / values[4];
于 2012-11-21T12:41:53.980 回答