如何让Android的屏幕尺寸编程方式,一劳永逸? [英] how to get android screen size programmatically, once and for all?

查看:111
本文介绍了如何让Android的屏幕尺寸编程方式,一劳永逸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能找到我的屏幕尺寸编程, 在使用触摸事件的单位 和查看测量/布局? 换句话说,我要的坐标 在屏幕的右下角的, 所使用的触摸事件'的坐标系中 getRawX()/ getRawY() View.getLocationOnScreen()

我犹豫来调用所需的事件/视图单位像素 因为显然有像素的几个概念 在我的手机在各种模式下, 而他们没有加入到一个一致的故事。

我认为这已被要求,并回答了很多 在计算器和其他地方, 但答案中的没有的实际工作,我的手机 (机器人4,安卓4.1.2)在所有模式:

  • <一个href="http://stackoverflow.com/questions/6842642/can-i-in-android-app-find-out-width-of-screen-programmatically">Can我的Andr​​oid应用程序找出宽屏幕编程的?
  • <一个href="http://stackoverflow.com/questions/11252067/how-do-i-get-the-screensize-programmatically-in-android">How我得到的屏幕尺寸编程方式在android的
  • <一个href="http://stackoverflow.com/questions/16321553/android-set-view-visibility-programmatically-based-on-screen-size">Android集查看知名度编程根据屏幕大小
  • <一个href="http://stackoverflow.com/questions/17101880/how-do-i-get-the-height-of-the-screen-in-android">How我得到的Andr​​oid屏幕的高度?
  • 获取Android的
  • 屏幕高度
  • <一个href="https://groups.google.com/forum/#!topic/android-developers/IpxnfvDFrpc">https://groups.google.com/forum/#!topic/android-developers/IpxnfvDFrpc
  • <一个href="http://shuklaxyz.blogspot.com/2012/02/how-to-programmatically-figure-out.html">http://shuklaxyz.blogspot.com/2012/02/how-to-programmatically-figure-out.html
  • <一个href="http://$c$crock.net/how-to-determine-screen-resolution-programmatically/">http://$c$crock.net/how-to-determine-screen-resolution-programmatically/
  • <一个href="http://www.$c$cproject.com/Tips/313848/Get-actual-screen-size-for-the-application-layout">http://www.$c$cproject.com/Tips/313848/Get-actual-screen-size-for-the-application-layout
  • <一个href="http://stackoverflow.com/questions/5255184/android-and-setting-width-and-height-programmatically-in-dp-units">Android并设置宽度和高度以编程方式在DP单元
  • <一个href="http://www.simple$c$cstuffs.com/how-to-get-screen-dimensions-programmatically-in-android/">http://www.simple$c$cstuffs.com/how-to-get-screen-dimensions-programmatically-in-android/
  • <一个href="http://www.androidsnippets.com/get-size-and-orientation-of-the-screen">http://www.androidsnippets.com/get-size-and-orientation-of-the-screen
  • <一个href="http://grokbase.com/t/gg/android-developers/127aatfqb6/how-to-determine-screen-resolution-programmatically">http://grokbase.com/t/gg/android-developers/127aatfqb6/how-to-determine-screen-resolution-programmatically

(哇!)

这是一个需要努力库code 无论应用程序是否在屏幕兼容模式 (即targetSdkVersion&LT; = 10在清单)与否, 我也不想作任何假设 关于状态栏的位置,大小,或存在 或任何其他类似的屏幕装饰品。


下面是事实:

  1. 我的手机(一个机器人4运行的是Android 4.1.2) 拥有540x960物理像素, 即小的彩色发光点。

  2. 在屏幕中所需的单位的大小, 从观察的触摸事件,并查看测量,是 为360x640当应用程序在屏幕兼容模式, 540x960时,应用程序是不是在屏幕兼容模式。 这些都是我需要找到编程的号码, 不带触摸事件或浏览找到他们碴, 但我有极大的困难找到任何API 这将返回这些数字。

  3. 获得
  4. 显示和DisplayMetrics对象 以各种方式都声称屏幕尺寸 是540x960像素 (无论是在屏幕兼容模式或不)。 具体而言,以下都说540x960所有的时间:     DisplayMetrics {宽度,高度}像素,     Display.getSize(),     Display.getRealSize(),     Display.get {宽度,高度}()

  5. 以各种方式获得
  6. 配置对象 都说屏幕{宽度,高度} DP = 360x614 (无论是在屏幕兼容模式或不)。 我不相信,再presents整个屏幕, 由于纵横比是错误的。 (我认为这是整个屏幕 减去状态栏;我需要整个屏幕。) 我认为这是肯定地说,整个屏幕为360x640 DP, 虽然我不知道返回640任何API。

  7. 在各种方式获得的DisplayMetrics 说,密度是 1.0F当屏幕兼容模式, 1.5F,不用时屏幕兼容模式。

  8. 活动的      getWindow()的getAttributes(){宽度,高度} 是没有帮助的,因为它通常包含 MATCH_PARENT 而不是实际尺寸。 但我可以明显地得到一个活动的所需答案      getWindow()。getDecorView()。getMeasured {宽度,高度}() (这是因为真正令人惊讶 活动窗口的decorView 不会的的喜欢它的占用整个屏幕; 它看起来像它采取了屏幕减去状态栏)。 但我不希望依靠这一点,因为如果窗口不断被调整 (软键盘出现?有人叫window.setAttributes()? 也许我不是一个活动的话), 这显然​​将是完全错误的。

据我所知,下面的公式应该成立:     像素= DP *密度 这似乎同意所有报告的数字((3),(4),上述(5)) 当的不是的屏幕兼容模式:     540x960 =为360x640 * 1.5 但在屏幕兼容模式下它不会加起来:     540x960!=为360x640 * 1 那么,什么是歪。

最简单的解释,我认为, 的是,在列出的方法(3)中 只是给了错误的答案像素的时候 在屏幕compat的mode--即,他们的目的 返回为360x640像素,但他们错误地返回540x960代替。 但也有可能是在寻找它的其他方式。

在任何情况下,得到所期望的号码,不考虑模式, 从上面的拼图,无疑是一个棘手的难题。 我发现,似乎在两种模式下工作,我的电话的方式, 但它是非常曲折的, 它依赖于两个假设似乎仍然相当不稳 (如code下面的评论中所述)。

下面是我的食谱:

  / **获取屏幕大小在像素,即的TouchEvent /视图单位。
*在我的Droid 4,这是为360x640或540x960
*取决于应用程序的屏幕兼容模式
*(即targetSdkVersion&LT; =在清单10)或没有。 * /
公共无效getScreenSizePixels(INT widthHeightInPixels [/ * 2 * /])
{
    资源资源= getResources();
    构配置= resources.getConfiguration();
    DisplayMetrics DM = resources.getDisplayMetrics();
    //注意,screenHeightDp是不可靠
    //(它似乎是太小的状态栏的高度),
    //但我们假设screenWidthDp是可靠的。
    //还要注意,dm.widthPixels,dm.heightPixels不可靠像素
    //(他们得到的时候在屏幕兼容模式困惑,似乎)
    //但我们假设它们的比值是正确的。
    双screenWidthInPixels =(双)config.screenWidthDp * dm.density;
    双screenHeightInPixels = screenWidthInPixels * dm.heightPixels / dm.widthPixels;
    widthHeightInPixels [0] =(int)的(screenWidthInPixels + 0.5);
    widthHeightInPixels [1] =(int)的(screenHeightInPixels + 0.5);
}
 

有没有更好/更清洁的方式 找到屏幕??

的大小
解决方案

 显示显示= getWindowManager()getDefaultDisplay()。
点大小=新的点();
display.getSize(大小);
INT宽度= size.x;
INT高= size.y;
 

现在你可以衡量你的屏幕大小的像素这是一个更好的测量单位比厘米,因为所有的按钮,textviews等。在本单位进行测量。那我用什么正常

How can I find out my screen size programmatically, in the units used by touch events and View measurement/layout? In other words, I want the coordinates of the bottom-right corner of the screen, in the coordinate system used by touch events' getRawX()/getRawY() and View.getLocationOnScreen().

I'm hesitant to call the desired event/view units "pixels" since evidently there are several notions of "pixels" on my phone in various modes, and they're not adding up to a consistent story.

I see this has been asked and answered a lot on stackoverflow and elsewhere, but none of the answers actually work on my phone (droid 4, android 4.1.2) in all modes:

(wow!)

This is for library code that needs to work regardless of whether the app is in "screen compatibility mode" (i.e. targetSdkVersion<=10 in the manifest) or not, and I also don't want to make any assumptions about the position, size, or existence of the status bar or any other similar screen decorations.


Here are the facts:

  1. my phone (a droid 4 running android 4.1.2) has 540x960 physical pixels, i.e. little colored glowing dots.

  2. the size of the screen in the desired units, from looking at touch events and View measurements, is 360x640 when the app is in screen compat mode, 540x960 when the app is not in screen compat mode. These are the numbers I need to find programmatically, without mucking with touch events or Views to find them, but I'm having extreme difficulty finding any API that will return these numbers.

  3. Display and DisplayMetrics objects obtained in various ways all claim the screen size is 540x960 "pixels" (whether in screen compat mode or not). To be specific, the following all say 540x960 all the time: DisplayMetrics.{width,height}Pixels, Display.getSize(), Display.getRealSize(), Display.get{Width,Height}(),

  4. Configuration objects obtained in various ways all say screen{Width,Height}Dp = 360x614 (whether in screen compat mode or not). I don't believe that represents the whole screen, since the aspect ratio is wrong. (I think it's the whole screen minus the status bar; I need the whole screen.) I think it's safe to say that the whole screen is 360x640 dp, though I don't know any API that returns that 640.

  5. DisplayMetrics obtained in various ways say the "density" is 1.0f when in screen compat mode, 1.5f when not in screen compat mode.

  6. The activity's getWindow().getAttributes().{width,height} isn't helpful since it typically contains MATCH_PARENT rather than actual sizes. But I can apparently get the desired answer from an activity's getWindow().getDecorView().getMeasured{Width,Height}() (which is actually surprising since the activity's window's decorView doesn't look like it's taking up the whole screen; it looks like it's taking up the screen minus the status bar). But I don't want to rely on this because if the window ever gets resized (soft keyboard appearing? someone calling window.setAttributes()? or maybe I'm not in an Activity at all), this is clearly going to be all wrong.

I understand the following formula is supposed to hold: pixels = dp * density That seems to agree with all the reported numbers ((3),(4),(5) above) when not in screen compatibility mode: 540x960 = 360x640 * 1.5 But in screen compatibility mode it doesn't add up: 540x960 != 360x640 * 1 So, something is awry.

The simplest explanation, I think, is that the methods listed in (3) above are simply giving the wrong answer for "pixels" when in screen compat mode-- that is, they were intended to return 360x640 "pixels" but they are wrongly returning 540x960 instead. But there may be other ways of looking at it.

In any case, getting the desired numbers regardless of mode, from the above puzzle pieces, is certainly a tricky puzzle. I have found a way that seems to work on my phone in both modes, but it is extremely circuitous, and it relies on two assumptions that still seem rather shaky (as described in the code comments below).

Here is my recipe:

/** get screen size in "pixels", i.e. touchevent/view units.
* on my droid 4, this is 360x640 or 540x960
* depending on whether the app is in screen compatibility mode
* (i.e. targetSdkVersion<=10 in the manifest) or not. */
public void getScreenSizePixels(int widthHeightInPixels[/*2*/])
{
    Resources resources = getResources();
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    // Note, screenHeightDp isn't reliable
    // (it seems to be too small by the height of the status bar),
    // but we assume screenWidthDp is reliable.
    // Note also, dm.widthPixels,dm.heightPixels aren't reliably pixels
    // (they get confused when in screen compatibility mode, it seems),
    // but we assume their ratio is correct.
    double screenWidthInPixels = (double)config.screenWidthDp * dm.density;
    double screenHeightInPixels = screenWidthInPixels * dm.heightPixels / dm.widthPixels;
    widthHeightInPixels[0] = (int)(screenWidthInPixels + .5);
    widthHeightInPixels[1] = (int)(screenHeightInPixels + .5);
}

Is there any better/cleaner way to find the size of the screen??

解决方案

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

Now you can measure your screen size in pixel which is a better measurement unit than centimeter because all the buttons ,textviews etc.. are measured in this unit. That what I use normally

这篇关于如何让Android的屏幕尺寸编程方式,一劳永逸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆