Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

how to get android screen size programmatically, once and for all?

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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...