How do android screen coordinates work?
Asked Answered
F

3

174

I am working with Android Animation and I have found the Android coordinate system to be quite confusing so I am here to ask this question about how coordinates work in Android. I am following this image for moving one view to another but it seems it's not working:

image

Freberg answered 14/7, 2012 at 11:24 Comment(1)
in fact, what you've drawn is actually correct, just rotated 90 degrees counterclockwise.Margretmargreta
F
335

enter image description here

This image presents both orientation(Landscape/Portrait)

To get MaxX and MaxY, read on.

For Android device screen coordinates, below concept will work.

Display mdisp = getWindowManager().getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x; 
int maxY = mdispSize.y;

EDIT:- ** **for devices supporting android api level older than 13. Can use below code.

    Display mdisp = getWindowManager().getDefaultDisplay();
    int maxX= mdisp.getWidth();
    int maxY= mdisp.getHeight();

(x,y) :-

1) (0,0) is top left corner.

2) (maxX,0) is top right corner

3) (0,maxY) is bottom left corner

4) (maxX,maxY) is bottom right corner

here maxX and maxY are screen maximum height and width in pixels, which we have retrieved in above given code.

Fixture answered 14/7, 2012 at 11:34 Comment(2)
If we've got the View with layout_width="1" layout_height="1" would it mean that top left is (0,0) and bottom right is (1, 1)? Wouldn't that mean that View's width = 2 and height = 2? Contradiction.Silsby
@Fixture can you please confrim , this is valid for both screen orientations (Portrait and Landscape), does screen orientation change affects on (0,0) -> top left corner. (maxX,0) -> top right corner (0,maxY) -> bottom left corner (maxX,maxY) -> bottom right cornerSpinning
O
20

For Android API level 13 and you need to use this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int maxX = size.x; 
int maxY = size.y;

Then (0,0) is top left corner and (maxX,maxY) is bottom right corner of the screen.

The 'getWidth()' for screen size is deprecated since API 13

Furthermore getwidth() and getHeight() are methods of android.view.View class in android.So when your java class extends View class there is no windowManager overheads.

          int maxX=getwidht();
          int maxY=getHeight();

as simple as that.

Ornate answered 26/9, 2013 at 4:1 Comment(0)
M
10

This picture will remove everyone's confusion hopefully which is collected from there.

Android screen coordinate

Mowbray answered 1/4, 2020 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.