简体   繁体   English

Android:有没有办法获得屏幕尺寸?

[英]Android: Is there a way to get the screen size?

I'm making a puzzle app with animations. 我正在制作带有动画的拼图应用程序。 The animations look a lot faster on small screens then on larger screens. 在小屏幕上看动画比在大屏幕上看快得多。 I'm assuming this is because on a larger screen they are moving a longer distance. 我假设这是因为它们在更大的屏幕上移动的距离更长。

I was going to change the play back speed depending on the screen size. 我打算根据屏幕尺寸更改播放速度。 I cn get the screen size in pixcels is there a way to get it in dpi???? 我会以像素为单位获取屏幕尺寸吗,有没有办法以dpi为单位获取屏幕尺寸?

There is your answer: Get screen dimensions in pixels . 有您的答案: 获取以像素为单位的屏幕尺寸 This however, may not solve your problems. 但是,这可能无法解决您的问题。 You should check your animation speeds, because I'm pretty sure, that it is not from the differences in the displays. 您应该检查动画速度,因为我很确定,这不是来自显示的差异。 You should post some code, for better help. 您应该发布一些代码,以获得更好的帮助。 Hope this helps. 希望这可以帮助。

If you want the the display dimensions in pixels you can use getSize : 如果要以像素为单位显示尺寸,可以使用getSize

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

If you're not in an Activity you can get the default Display via WINDOW_SERVICE : 如果您不在活动中,则可以通过WINDOW_SERVICE获取默认的Display:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced ( in API level 13 ), you could use the getWidth and getHeight methods that are now deprecated: 在引入getSize之前( 在API级别13中 ),可以使用现已弃用的getWidthgetHeight方法:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Referred from : How to get screen dimensions 引用自: 如何获取屏幕尺寸

Display display = getWindowManager().getDefaultDisplay();

int width = display.getWidth();
int height = display.getHeight();

OR 要么

 Display display = getWindowManager().getDefaultDisplay();
 Point sSize = new Point();
 display.getSize(sSize);
 int width = sSize.x;
 int height = sSize.y;

you can use this: 您可以使用此:

 Display display = getWindowManager().getDefaultDisplay();
 Point sSize = new Point();
 display.getSize(sSize);
 int width = sSize.x;
 int height = sSize.y;

you can also use this for inches: 您还可以使用英寸:

    float mXDpi;
    float mYDpi;
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    mXDpi = metrics.xdpi; 
    mYDpi = metrics.ydpi;
    float  mMetersToPixelsX = mXDpi / 0.0254f; 
    float  mMetersToPixelsY = mYDpi / 0.0254f;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM