【发布时间】:2013-07-03 02:40:47
【问题描述】:
如何以编程方式找出我的屏幕尺寸,
在触摸事件使用的单位中
和查看测量/布局?
换句话说,我想要坐标
屏幕右下角的,
在触摸事件使用的坐标系中'
getRawX()/getRawY() 和 View.getLocationOnScreen()。
我不愿将所需的事件/视图单位称为“像素” 因为显然有几个“像素”的概念 在我的手机上以各种模式, 而且它们并没有形成一个一致的故事。
我看到这个问题已经被问过很多次了 在stackoverflow和其他地方, 但没有的答案实际上在我的手机上有效 (droid 4, android 4.1.2) 在所有模式下:
- Can I find out width of screen programmatically in android app?
- How do I get the ScreenSize programmatically in android
- Android set View visibility programmatically based on screen size
- How do I get the height of the screen in Android?
- Get the screen height in Android
- https://groups.google.com/forum/#!topic/android-developers/IpxnfvDFrpc
- http://shuklaxyz.blogspot.com/2012/02/how-to-programmatically-figure-out.html
- http://coderock.net/how-to-determine-screen-resolution-programmatically/
- http://www.codeproject.com/Tips/313848/Get-actual-screen-size-for-the-application-layout
- Android and setting width and height programmatically in dp units
- http://www.simplecodestuffs.com/how-to-get-screen-dimensions-programmatically-in-android/
- http://www.androidsnippets.com/get-size-and-orientation-of-the-screen
- http://grokbase.com/t/gg/android-developers/127aatfqb6/how-to-determine-screen-resolution-programmatically
(哇!)
这适用于需要工作的库代码 无论应用是否处于“屏幕兼容模式” (即清单中的 targetSdkVersion
以下是事实:
我的手机(运行 android 4.1.2 的 droid 4) 具有 540x960 物理像素, 即小彩色发光点。
以所需单位显示的屏幕尺寸, 通过查看触摸事件和查看测量,是 360x640 当应用处于屏幕兼容模式时, 当应用程序未处于屏幕兼容模式时为 540x960。 这些是我需要以编程方式找到的数字, 无需使用触摸事件或视图来查找它们, 但我很难找到任何 API 这将返回这些数字。
已获得 Display 和 DisplayMetrics 对象 以各种方式都声称屏幕尺寸 是 540x960“像素” (无论是否在屏幕兼容模式下)。 具体来说,以下都是 540x960: DisplayMetrics.{width,height}像素, 显示.getSize(), Display.getRealSize(), Display.get{Width,Height}(),
通过各种方式获得的配置对象 都说屏幕{Width,Height}Dp = 360x614 (无论是否在屏幕兼容模式下)。 我不相信那代表整个屏幕, 因为纵横比是错误的。 (我认为是整个屏幕 减去状态栏;我需要整个屏幕。) 我认为可以肯定地说整个屏幕是 360x640 dp, 虽然我不知道任何返回 640 的 API。
DisplayMetrics 以各种方式获得 说“密度”是 1.0f 在屏幕兼容模式下, 不处于屏幕兼容模式时为 1.5f。
活动的
getWindow().getAttributes().{width,height}没有帮助,因为它通常包含MATCH_PARENT而不是实际尺寸。 但我显然可以从活动中得到想要的答案getWindow().getDecorView().getMeasured{Width,Height}()(这实际上令人惊讶,因为 活动窗口的 decorView 看起来不像是占据了整个屏幕; 看起来它正在占用屏幕减去状态栏)。 但我不想依赖这个,因为如果窗口被调整大小 (出现软键盘?有人调用 window.setAttributes()? 或者我可能根本不在活动中), 这显然是错误的。
我了解以下公式应该成立: 像素 = dp * 密度 这似乎与所有报告的数字一致(上面的(3),(4),(5)) 当 not 处于屏幕兼容模式时: 540x960 = 360x640 * 1.5 但在屏幕兼容模式下,它不会加起来: 540x960 != 360x640 * 1 所以,有些不对劲。
我认为最简单的解释是, 就是上面(3)中列出的方法 只是在“像素”时给出错误的答案 在屏幕兼容模式下——也就是说,它们是有意的 返回 360x640“像素”,但他们错误地返回 540x960。 但可能还有其他方式来看待它。
在任何情况下,无论模式如何,都能获得所需的数字, 从上面的拼图来看,肯定是一个棘手的难题。 我找到了一种似乎在两种模式下都可以在手机上使用的方法, 但它非常迂回, 它依赖于两个看起来仍然相当不稳定的假设 (如下面的代码 cmets 所述)。
这是我的食谱:
/** 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);
}
有没有更好/更清洁的方法 找到屏幕的大小??
【问题讨论】:
-
+1 很好的问题,并且展示了很多研究工作。
-
@user117 - 有趣。好吧,DisplayMetrics 的 部分 至少得到了更新——密度更改为 1.0。但是 widthPixels/heightPixels 似乎并没有被更改为匹配。关于引用的问题,看起来行为恢复到 3.1 行为(即我看到 heightPixels include 状态栏......但似乎单位错误)
-
@DonHatch 谢谢,好心——我实际上是想赞扬一个深思熟虑的问题,奇怪的是在 StackExchange 上不那么受欢迎——请注意这个问题不仅仅是“真实”的问题答案,讲的是听众对该主题的知识的(低)质量。现在,我可能会因为自己的问题因“不真实”而被一个接一个地关闭而受到创伤,幽默感可能让我失望,但它开始看起来像是有“好”答案的问题真的是这一切都在这里发生了什么......当然,我赞成你的:)
-
顺便说一句!这是我怀疑的内容:@ 987654336@(尤其是参见他的(1)点,即快速制作的答案——实际上就是这个Q的情况!)。无论如何,快乐的纽约(我的在 11 分钟内到来)! :)
标签: android