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

查看:100
本文介绍了如何获得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));

我这样做,所以假设我的密度dpi是120 ..,宽度像素是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 ...我除以768/2,然后得到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上..宽度dpi的计算似乎是错误的...

But on other ones like 240, 160, 120 density dpi..the calculation of width dpi 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:

  • 超大屏幕至少为960dp x 720dp
  • 大屏幕至少为640dp x 480dp
  • 正常屏幕至少为470dp x 320dp
  • 小屏幕尺寸至少为426dp x 320dp

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

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