Retrieve coordinates for ImageView

一曲冷凌霜 提交于 2019-12-02 18:42:51

问题


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


来源:https://stackoverflow.com/questions/7689690/retrieve-coordinates-for-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!