android屏幕坐标如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11483345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do android screen coordinates work?
提问by Yasir Khan
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:
我正在使用 Android Animation
,我发现 Android 坐标系非常混乱,所以我在这里询问有关坐标如何在 Android 中工作的问题。我正在关注此图像以将一个视图移动到另一个视图,但它似乎不起作用:
回答by AAnkit
This image presents both orientation(Landscape/Portrait)
此图像呈现两个方向(横向/纵向)
To get MaxX and MaxY, read on.
要获得 MaxX 和 MaxY,请继续阅读。
For Android device screen coordinates, below concept will work.
对于 Android 设备屏幕坐标,以下概念将起作用。
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.
编辑:- ** ** 对于支持 android api 级别早于 13 的设备。可以使用下面的代码。
Display mdisp = getWindowManager().getDefaultDisplay();
int maxX= mdisp.getWidth();
int maxY= mdisp.getHeight();
(x,y) :-
(x,y) :-
1) (0,0)is top left corner.
1) (0,0)是左上角。
2) (maxX,0)is top right corner
2) (maxX,0)是右上角
3) (0,maxY)is bottom left corner
3) (0,maxY)是左下角
4) (maxX,maxY)is bottom right corner
4) (maxX,maxY)是右下角
here maxX and maxY are screen maximum height and width in pixels, which we have retrieved in above given code.
这里 maxX 和 maxY 是以像素为单位的屏幕最大高度和宽度,我们在上面给定的代码中检索到。
回答by vids
For Android API level 13 and you need to use this:
对于 Android API 级别 13,您需要使用它:
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.
然后 (0,0) 是屏幕的左上角,(maxX,maxY) 是屏幕的右下角。
The 'getWidth()' for screen size is deprecated since API 13
自 API 13 起,不推荐使用屏幕尺寸的“getWidth()”
Furthermore getwidth()and getHeight()are methods of android.view.Viewclass in android.So when your java class extends View class there is no windowManager overheads.
此外getwidth()和getHeight()是android.view.View类在 android.So中的方法,所以当你的 java 类扩展 View 类时,没有 windowManager 开销。
int maxX=getwidht();
int maxY=getHeight();
as simple as that.
就如此容易。