1

我陷入了这个问题。我有一个imageView并且我想只在图像内获得我的触摸位置。当我触摸超出图像时,我必须显示x=0y=0。在这个应用程序中,我必须知道调整图像大小、缩放和旋转图像,这些功能是固定的,但我对图像的坐标只有图像内部有问题。

这是一个链接,我的意思是链接

这是我试图解决的同一个问题:How to get Coordinates X,Y of Image after Zoom and Pan

非常感谢。

翁德雷

这是我的代码:

public class Sample extends Activity{
        static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    int[] location = new int[2];

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ImageView image = (ImageView) findViewById(R.id.mapView);

        final Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.mapa);
        image.setImageBitmap(icon);



        image.setOnTouchListener(new View.OnTouchListener()
        {

            public boolean onTouch(View v, MotionEvent event)
            {
                final ImageView view = (ImageView) v;


                switch (event.getAction() & MotionEvent.ACTION_MASK)
                {
                case MotionEvent.ACTION_DOWN:
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    mode = DRAG;
                    RectF r = new RectF();
                    matrix.mapRect(r);

                    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;
                case MotionEvent.ACTION_POINTER_DOWN:
                    oldDist = spacing(event);
                    if (oldDist > 10f)
                    {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;

                }

                // Perform the transformation
                view.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);
    }

main.xml 看起来像:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="right"
        android:adjustViewBounds="true"
        android:scaleType="matrix" >
    </ImageView>

</FrameLayout>
4

2 回答 2

0

我真的不明白你在问什么,但无论如何我都会尝试回答。听起来您想使用 getRawX() 和 getRawY() 来获取屏幕上的触摸位置,而不是视图上的触摸位置。您可能还必须为 imageView 之外的任何内容覆盖 onTouchEvent 方法。

于 2012-07-17T21:39:00.533 回答