Android Honeycomb 中如何获取屏幕分辨率?

发布于 2024-12-01 11:09:29 字数 377 浏览 1 评论 0原文

我想在 Android Honeycomb 上获得屏幕的真实分辨率。

这是我的代码

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

我的设备是 Asus Transformer TF101,尺寸为 1280x800。

但上面的代码使 w = 1280 和 h = 752 (我想要的是 800 而不是 752)。

我知道 h < 800,因为状态栏减去了它。

有什么办法获得屏幕的真实高度吗?

非常感谢!

I want to get real resolution of screen on Android Honeycomb.

Here's my code

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

My device is Asus Transformer TF101 with size are 1280x800.

But above code make w = 1280 and h = 752 (That i want is 800 not 752).

I know h < 800 because it's subtracted for status bar.

Have any way to get real height of screen?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

欢你一世 2024-12-08 11:09:29

从 Andorid 3.2 开始,系统状态栏的高度不包含在 DisplayMetrics 的高度中,您必须使用未文档化的 API(Display.getRawWidth() 和 Display.getRawHeight())来获取物理屏幕宽度或高度。

以下示例向您展示如何获取物理屏幕宽度或高度。

Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);

更新:
对于API 13-16,您必须使用上面的代码来获取真实的宽度/高度。
对于 API 17+,您现在可以使用新的公共 API Display.getRealSize()。

Starting Andorid 3.2, the height of system status bar is not included in DisplayMetrics's height, you have to use undocumented APIs (Display.getRawWidth() and Display.getRawHeight()) to get the physical screen width or height.

Here is the example to show you how to get the physical screen width or height.

Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);

UPDATED:
For API 13-16, you have to use the above code to get real width/height.
For API 17+, you can now use the new public API, Display.getRealSize().

夜声 2024-12-08 11:09:29

在这里,使用一些小技巧并假设屏幕装饰位于顶部/底部,而不是左侧/右侧:

Display d = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int h = d.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int w = d.getWidth();

Here you are, with little trick and assumption that screen decorations are on top/bottom and never on left/right:

Display d = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int h = d.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int w = d.getWidth();
那请放手 2024-12-08 11:09:29

我手头有一个 Xoom,并使用 getWindowManager().getDefaultDisplay() 来检索尺寸,它返回我 1280 x 800,这是全屏尺寸。我想知道3.0版本之前如何用API获取减去导航栏的分辨率。

I have a Xoom in hand, and used getWindowManager().getDefaultDisplay() to retrieve the dimension, it returns me 1280 x 800 which is the full screen size. I wonder how to get the resolution minus navigation bar with API before version 3.0.

°如果伤别离去 2024-12-08 11:09:29

使用 DisplayMetrics 结构,描述有关显示器的一般信息,例如其大小、密度和字体缩放。

用于获取显示器高度的代码如下:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Log.d("log", "OOO " + metrics.heightPixels);

它应该返回显示器的绝对高度(以像素为单位)。

Use the DisplayMetrics structure, describing general information about the display, such as its size, density, and font scaling.

The code used to get the display's height is as follows:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Log.d("log", "OOO " + metrics.heightPixels);

It's supposed to return the absolute height of the display, in pixels.

墨小沫ゞ 2024-12-08 11:09:29

正如正确推断的那样,您得到的答案是由于状态栏。您所要做的就是在窗口显示启动之前去掉状态栏。然后您可以在设置活动的内容视图之前重置状态栏。

这样做的原因是,除非您动态处理所有测量、布局和绘制,否则摆脱状态栏会影响您的视图绘制。在运行时的中间执行此操作将导致状态栏消失,然后根据需要重新出现,从而导致用户感到困惑。

隐藏状态栏:

在你的onCreate()中:

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Add the flag to the Window Attributes
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
//Disassociate Display from the Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

现在你的默认显示应该可以正常工作

仍然在你的onCreate()中:

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

现在在你设置内容之前再次

,在 onCreate() 中:

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Show the statubar
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
// Reassociate.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

最后:

setContentView(r.layout.myView);

实际上,前面的代码段几乎可以在任何地方工作。我只是考虑你的用户体验。当然,您可以随意将它们放置在任何地方。这些是从我的一个项目中提取的功能部分。我在一些家庭启动器中也看到了类似的技术。注意:根据 Android 版本,您可能需要在 onWindowAttached() 中执行状态栏操作。如果这样做,请确保仍然调用 super.onWindowAttached()。

另一种技术:
当然,如果您无论如何都想这样做,您始终可以在清单中以这种方式设置活动的属性。

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

The answer you are getting, as properly deduced is because of the status bar. All you have to do is get rid of the status bar before your window display initiates. And then you can reset the status bar before you set the content view of the activity.

The reason to do it this way is that getting rid of the status bar affects your view drawing unless you handle all measure, layout and draw dynamically. Doing this is the middle of your runtime will cause the status bar to disappear, and then reappear if you want it to, resulting in confusion from users.

To Hide the StatusBar:

In your onCreate():

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Add the flag to the Window Attributes
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
//Disassociate Display from the Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Now your Default Display should work correctly

Still in your onCreate():

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

Now before you set the Content

Again, in your onCreate():

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Show the statubar
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
// Reassociate.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Finally:

setContentView(r.layout.myView);

The previous code segments will work almost anywhere, actually. I'm just thinking about your user experience. Feel free to place them whereever, of course. These are functional segments pulled from one of my projects. I've seen techniques similar in some Home Launchers as well. Note: depending on the Android version, you might have to do the status bar stuff in onWindowAttached(). If you do, make sure you still call super.onWindowAttached().

Another Technique:
Of course, if you want to do this anyway, you could always set the attribute of the activity this way in your manifest.

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
酸甜透明夹心 2024-12-08 11:09:29

在问题状态栏高度?中解释如何获取状态栏高度。有了 statusBarHeight 值,您可以执行以下操作:

Display display = getWindowManager().getDefaultDisplay();
int h = display.getHeight() + statusBarHeight;

In the question Height of statusbar? explain how you get the statusbar height. Having the statusBarHeight value, you can do:

Display display = getWindowManager().getDefaultDisplay();
int h = display.getHeight() + statusBarHeight;
舞袖。长 2024-12-08 11:09:29

您可以扩展一个布局,该布局将成为您的顶部布局(将设置为 fill_parent 宽度和高度)并覆盖 onSizeChange(int width, int height, int oldwidth, int oldheight)。
宽度和高度是显示器的实际宽度和高度。

You can extend a layout which will be your top layout (which will be set to fill_parent width and height) and override the onSizeChange(int width, int height, int oldwidth, int oldheight).
The width and height is the real width and height of your display.

旧梦荧光笔 2024-12-08 11:09:29

也许您可以添加状态栏的高度。 状态栏的高度?

   Rect rectgle= new Rect();
   Window window= getWindow();
   window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
   int StatusBarHeight= rectgle.top;
   int contentViewTop= `enter code here`
      window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
   int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);

Perhaps you can add the height of the status bar. Height of statusbar?

   Rect rectgle= new Rect();
   Window window= getWindow();
   window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
   int StatusBarHeight= rectgle.top;
   int contentViewTop= `enter code here`
      window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
   int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
墨落成白 2024-12-08 11:09:29

API11级别最低获取屏幕尺寸的函数;还包括更高级别 API 的方法(更改函数以使用所选 API、其函数调用及其返回对象类型):

public int getScreenOrientation()
{
    int nOrientation = Configuration.ORIENTATION_UNDEFINED;

    try
    {
        DisplayMetrics displayMetrics = null;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(windowManager != null)
        {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            if(defaultDisplay != null)
            {
                defaultDisplay.getMetrics(displayMetrics);//API11
                //defaultDisplay.getRectSize(rectGet);//API13
                //defaultDisplay.getSize(pointGet);//API13
                //defaultDisplay.getCurrentSizeRange(pointGet,pointGet);//API16
                //defaultDisplay.getRealSize(pointGet);//API17
                //defaultDisplay.getRealMetrics(displayMetrics);//API17
                if((displayMetrics.widthPixels == displayMetrics.heightPixels) || (displayMetrics.widthPixels < displayMetrics.heightPixels))
                {
                    nOrientation = Configuration.ORIENTATION_PORTRAIT;
                }
                else
                {
                    nOrientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
        }
    }
    catch(Exception e)
    {
        showMessage(false,"Error","[getScreenOrientation]: " + e.toString());
    }

    return nOrientation;
}

注意:Configuration.ORIENTATION_SQUARE 已弃用,因此此处将其替换为默认值 ORIENTATION_PORTRAIT。

A function to get the screen size at API11 level minimum; also includes methods for higher level APIs (change function to use the selected API, its function call and its return object type):

public int getScreenOrientation()
{
    int nOrientation = Configuration.ORIENTATION_UNDEFINED;

    try
    {
        DisplayMetrics displayMetrics = null;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(windowManager != null)
        {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            if(defaultDisplay != null)
            {
                defaultDisplay.getMetrics(displayMetrics);//API11
                //defaultDisplay.getRectSize(rectGet);//API13
                //defaultDisplay.getSize(pointGet);//API13
                //defaultDisplay.getCurrentSizeRange(pointGet,pointGet);//API16
                //defaultDisplay.getRealSize(pointGet);//API17
                //defaultDisplay.getRealMetrics(displayMetrics);//API17
                if((displayMetrics.widthPixels == displayMetrics.heightPixels) || (displayMetrics.widthPixels < displayMetrics.heightPixels))
                {
                    nOrientation = Configuration.ORIENTATION_PORTRAIT;
                }
                else
                {
                    nOrientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
        }
    }
    catch(Exception e)
    {
        showMessage(false,"Error","[getScreenOrientation]: " + e.toString());
    }

    return nOrientation;
}

Note: Configuration.ORIENTATION_SQUARE is deprecated, so here it is replaced to default to ORIENTATION_PORTRAIT.

混吃等死 2024-12-08 11:09:29

这是测量屏幕尺寸的另一个可行的解决方案(API 11+):

public static ABDimension calculateScreenDimensions(Activity activityGet)
{
    ABDimension abdReturn = new ABDimension();

    try
    {
        Rect rectGet = new Rect();
        Window winGet = null;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        int nStatusBarHeight = 0;
        int nContentViewTop = 0;
        int nTitleBarHeight = 0;
        int nScreenHeight = 0;

        if((activityGet != null) && (rectGet != null) && (displayMetrics != null))
        {
            winGet = activityGet.getWindow();
            if(winGet != null)
            {
                winGet.getDecorView().getWindowVisibleDisplayFrame(rectGet);
                nStatusBarHeight = rectGet.top;
                nContentViewTop = winGet.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                nTitleBarHeight = nContentViewTop - nStatusBarHeight;

                activityGet.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                int screenHeight = displayMetrics.heightPixels;
                abdReturn.nWidth = displayMetrics.widthPixels;
                abdReturn.nHeight = screenHeight - (nTitleBarHeight + nStatusBarHeight);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage(false,"Error","[calculateScreenDimensions]: "+e.toString());
    }

    return abdReturn;
}

其中 ABDimension 类包含六个标记为 nWidth、nHeight、nMarginLeft、nMarginTop、nMarginRight 和 nMarginBottom 的整数。此版本考虑了常见的 Android 装饰组件,例如 TitleBar/StatusBar。

This is another viable solution for measuring the screen size (API 11+):

public static ABDimension calculateScreenDimensions(Activity activityGet)
{
    ABDimension abdReturn = new ABDimension();

    try
    {
        Rect rectGet = new Rect();
        Window winGet = null;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        int nStatusBarHeight = 0;
        int nContentViewTop = 0;
        int nTitleBarHeight = 0;
        int nScreenHeight = 0;

        if((activityGet != null) && (rectGet != null) && (displayMetrics != null))
        {
            winGet = activityGet.getWindow();
            if(winGet != null)
            {
                winGet.getDecorView().getWindowVisibleDisplayFrame(rectGet);
                nStatusBarHeight = rectGet.top;
                nContentViewTop = winGet.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                nTitleBarHeight = nContentViewTop - nStatusBarHeight;

                activityGet.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                int screenHeight = displayMetrics.heightPixels;
                abdReturn.nWidth = displayMetrics.widthPixels;
                abdReturn.nHeight = screenHeight - (nTitleBarHeight + nStatusBarHeight);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage(false,"Error","[calculateScreenDimensions]: "+e.toString());
    }

    return abdReturn;
}

Where the ABDimension class contains six integers labeled nWidth, nHeight, nMarginLeft, nMarginTop, nMarginRight and nMarginBottom. This version accounts for common Android decor components like the TitleBar/StatusBar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文