Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all x and y coordinates of an imageview in Android?

Tags:

java

android

I am implementing an app for getting the correct x and y location of the image-view and x and y coordinates of an image-view when I move the position of images on the screen. When I run the app and when I move my image-view it will get only 0 and 1. Is there a way for find out the x and y location?

Here is my Main-activity class:

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

    }


 }
like image 701
tazeenmulani Avatar asked Jun 19 '13 11:06

tazeenmulani


People also ask

What is ImageView code?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.


1 Answers

I am using View.getLocationOnScreen(int[]) to get X and Y of a view relative to its parent/screen:

int[] posXY = new int[2];
myView.getLocationOnScreen(posXY);
int x = posXY[0];
int y = posXY[1];
like image 145
gunar Avatar answered Dec 03 '22 21:12

gunar