简体   繁体   中英

How to get x and y coordinates of image inside an imageview in android?

I'm stuck in this problem. I have an imageView and I would like to get a position of my touch only inside the image. When I touch out of image I must display x=0 and y=0 . In this app I must know resize, zoom and rotate the image, there functions are fixed, but I have problem with the coordinates of image only inside the image.

Here is a link, what I mean link

This is a same problem what I try to solve: How to get Coordinates X,Y of Image after Zoom and Pan

Thanks a lot.

Ondrej

Here is my code:

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

And main.xml looks like:

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

I don't really understand what you're asking, but I'll try to answer anyway. It sounds like you want to use getRawX() and getRawY() to get the touch position on the screen and not on the view. You may also have to override the onTouchEvent method for whatever is outside of the ImageView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM