Retrieve coordinates for ImageView

2020-05-06 14:28发布

问题:

I would like to know if it is possible to get the coordinates, left and top, of an ImageView. I have 2 ImageView inside a RelativeLayout inside a ScrollView. I tried to retrieve the matrix of the ImageView with matrix = _iv.getImageMatrix(); but it is not helpful Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}. Any ideas? Would it make a difference if the getImageMatix() is run in a different place?

回答1:

Here's a better way to do it. I implemented an OnTouchListener in the ImageView itself and the X,Y coordinates are corresponding to the ImageView.

_iv.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Choose which motion action has been performed
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                //Get X, Y coordinates from the ImageView
                int X = (int) event.getX();
                int Y = (int) event.getY();

                //Do something

                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
            }
            return true;
        }
}); //End setOnTouchListner()