简体   繁体   中英

x and y coordinates of image in Layout

How to getthe X and y coordinates of an image in Layout in Android?
And how to get top, bottom, left and right of an image in Layout in Android?

by using Touch listeners you can get X and Y values of view

layout.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) { 
            float x = event.getX();
            float y = event.getY();
            return false;
        }
    });

for x and y coordinates of any view compared to its parents , use : getLeft() , getTop() , getRight() ,and getBottom() .

for raw x and y coordinates of any view compared to the screen/window , use: getLocationInWindow(int[] location) , getLocationOnScreen(int[] location)

do note that all of those methods won't work till the view is positioned and layout procedure has finished.

more methods are written on the API here .

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