如何在android中的imageview中获取图像的x和y坐标?

时间:2012-07-17 21:25:06

标签: android image imageview coordinates

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

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

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

非常感谢。

的Ondrej

这是我的代码:

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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)