Android:获取屏幕分辨率/像素作为整数值

发布于 2024-09-03 08:03:18 字数 268 浏览 3 评论 0原文

这是一个非常简单的问题,我没有找到答案:/ 如何快速获取整数值的屏幕分辨率(宽度、高度)?

我已经尝试过这个,但它在我的模拟器上总是显示零:

DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;

在我的例子中,我想动态创建一个带有 tableRows 的表,每个表包含两个列。该列全部应填满屏幕宽度的一半。

有人可以给我提示吗?

this is a really simple question on which I've found no answer :/
How can I quickly access the screen resolution (width, height) as integer values?

I've tried this one, but it always shows zero on my emulator:

DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;

In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.

Can someone give me a hint?

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

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

发布评论

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

评论(5

并安 2024-09-10 08:03:18

垃圾卡尔玛的答案是正确的。

但是,结果将特定于活动上下文。如果您想要整个设备屏幕分辨率:

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;

请注意,结果还取决于当前设备方向。

The trashkalmar answer is correct.

However, the results will be specific to the activity context. If you want the whole device screen resolution:

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;

Note that the results also depend on the current device orientation.

此岸叶落 2024-09-10 08:03:18

您可以通过以下方式获取屏幕指标:

Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();

You can get screen metrics in this way:

Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
樱&纷飞 2024-09-10 08:03:18

1.只需使用此内部活动即可获取屏幕宽度和高度像素。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
    .getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

2.这也可以使用,但需要 Api 级别内联检查

Display display = getWindowManager().getDefaultDisplay();

Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    display.getSize(size);
    int width2 = size.x;
    int height2 = size.y;

} else {
    int width2 = display.getWidth();
    int height2 = display.getHeight();
}

3.当您不在活动中但有上下文时使用此

WindowManager wm = (WindowManager) context.getSystemService(
            Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();

int width1;
int height1;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

    display1.getSize(size1);
    width1 = size1.x;
    height1 = size1.y;

} else {
    width2 = display.getWidth();
    height2 = display.getHeight();
}

1.Simply use This inside activity to get screen width and height pixels.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
    .getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

2.This can also be used But requires Api level inlined check

Display display = getWindowManager().getDefaultDisplay();

Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    display.getSize(size);
    int width2 = size.x;
    int height2 = size.y;

} else {
    int width2 = display.getWidth();
    int height2 = display.getHeight();
}

3.Use This when you are not in activity but having context

WindowManager wm = (WindowManager) context.getSystemService(
            Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();

int width1;
int height1;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

    display1.getSize(size1);
    width1 = size1.x;
    height1 = size1.y;

} else {
    width2 = display.getWidth();
    height2 = display.getHeight();
}
囍孤女 2024-09-10 08:03:18

我使用以下内容:

int scrWidth  = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();

没有混乱,没有大惊小怪,只有 2 个类变量

I use the following:

int scrWidth  = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();

No muss, no fuss, just 2 class variables

抚笙 2024-09-10 08:03:18

最简单的方法是实现 onSizeChanged。您可能会得到 0 值,因为视图尚未初始化。例如,您不能在 View 构造函数中调用上述代码。

The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

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