如何获得屏幕宽度DPI的机器人? [英] How to get screen width dpi in ANDROID?

查看:146
本文介绍了如何获得屏幕宽度DPI的机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能让屏幕宽度DPI?

How can I get the screen width dpi?

DisplayMetrics metrics = getResources().getDisplayMetrics();
int f = metrics.densityDpi;


  //  getWindowManager().getDefaultDisplay().getMetrics(metrics);

int width = metrics.widthPixels;
int dp = (int)(width / (metrics.density));

我这样做,所以让我们假设我densitydpi是120..and widthpixel 240 .. 通过计算,我得到240 / 0.75 ....所以我有320 widthdpi?

I do this, so lets assume my densitydpi is 120..and widthpixel 240.. via calculation i get 240/0.75.... so I have 320 widthdpi?

然而事实并非如此.... widthdpi是那么小的320,我认为......因为320去与我的错在屏幕....使用120幅作品。

However that's not the case....widthdpi is smaller then 320 I think...because 320 goes with me wrong in the screen.... using 120 works.

在Nexus 4 ...宽度为768和densitydpi 320 ......我把二分之七百六十八和我得到384正确的宽度密度DPI(它的工作原理)

on Nexus 4...with width 768 and densitydpi 320...I divide 768/2 and i get 384 correct width density dpi (it works)

但在其他的类似240,160,120密度dpi..the计算widthdpi似乎错了......

But on other ones like 240, 160, 120 density dpi..the calculation of widthdpi seems wrong ...

推荐答案

按照支持多个屏幕指南的定义DP

px = dp * (dpi / 160)

因此​​

dp = px / (dpi / 160)

或等价

dp = px * 160 / dpi

记住DP代表'密度无关象素' - 即1DP是一个LDPI设备上相同的物理尺寸,因为它是一个xxhdpi设备上。因此,你应该期望所有的手机有大约〜宽度300-400dp,通过DP指出桶的大小:

Remember that dp stands for 'density-independent pixel' - i.e., 1dp is the same physical size on a ldpi device as it is on an xxhdpi device. Therefore you should expect all phones to have roughly ~300-400dp of width, noting the bucket sizes by dp:

  • XLARGE屏幕至少960dp x 720dp
  • 在大屏幕至少640dp x 480dp
  • 在标准屏幕至少470dp x 320dp
  • 在小屏幕至少426dp x 320dp

这篇关于如何获得屏幕宽度DPI的机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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