如何在Android中获取imageview的所有x和y坐标?

前端之家收集整理的这篇文章主要介绍了如何在Android中获取imageview的所有x和y坐标?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在实现一个应用程序,用于在我移动屏幕上的图像位置时获取图像视图的正确x和y位置以及图像视图的x和y坐标.当我运行应用程序时,当我移动我的图像视图时,它将只获得0和1.有没有办法找出x和y位置?

这是我的主要活动类:

public class MainActivity  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[4];

        @Override
        public void onCreate(Bundle savedInstanceState)
        {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        final Bitmap icon = BitmapFactory.decodeResource(this.getResources(),R.drawable.yellowrose);

        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;
                    System.out.println("mode = drag");
                    RectF r = new RectF();
                    matrix.mapRect(r);

                    break;

                case MotionEvent.ACTION_UP:

                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    System.out.println("mode = none = "  + mode);

                    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;
                            System.out.println("scale " + scale);
                            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;
                        System.out.println("mode " + mode);
                    }
                    break;

                }

                // Perform the transformation
                view.setImageMatrix(matrix);
                return true;
            }
        });
    }

    private float spacing(MotionEvent event)
    {
        float x = event.getRawX() - event.getRawX();
        System.out.println("spacing x" + x);

        float y = event.getRawY() - event.getRawY();
        System.out.println("spacing y" + y);

        return FloatMath.sqrt(x * x + y * y);

    }

    private void midPoint(PointF point,MotionEvent event)
    {
        float x = event.getRawX() + event.getRawX();
        System.out.println("midpoint x" + x);

        float y = event.getRawY() + event.getRawY();
        System.out.println("midpoint y" + y);

        point.set(x / 2,y / 2);

    }


 }

解决方法

我使用 View.getLocationOnScreen(int[])获取相对于其父/屏幕的视图的X和Y:
int[] posXY = new int[2];
myView.getLocationOnScreen(posXY);
int x = posXY[0];
int y = posXY[1];
原文链接:https://www.f2er.com/android/309583.html

猜你在找的Android相关文章