得到屏幕像素密度编程的机器人? [英] getting the screen density programmatically in android?
问题描述
如何让屏幕像素密度编程的机器人?
How to get the screen density programmatically in android?
我的意思是如何找到当前设备的屏幕DPI?
I mean How to find the screen dpi of the current device?
推荐答案
您可以获取信息从的 DisplayMetrics 结构:
You can get info on the display from the DisplayMetrics struct:
DisplayMetrics metrics = getResources().getDisplayMetrics();
尽管Android不使用直接像素映射,它采用了一把量化密度独立的像素值,然后扩展到实际屏幕尺寸。因此, metrics.densityDpi
属性将其中的密度_ ???
常数(120,160,213,240 ,320,480或640 DPI)。
Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size. So the metrics.densityDpi
property will be one of the DENSITY_???
constants (120, 160, 213, 240, 320, 480 or 640 dpi).
如果您需要的实际的LCD的像素密度(也许是一个OpenGL应用程序),你可以从它的 metrics.xdpi
和 metrics.ydpi
属性,分别为水平和垂直密度。
If you need the actual lcd pixel density (perhaps for an OpenGL app) you can get it from the metrics.xdpi
and metrics.ydpi
properties for horizontal and vertical density respectively.
如果你的目标API级别早于4, metrics.density
属性是从参考密度浮点比例因子(160dpi)。现在 metrics.densityDpi
提供的相同的值可以计算出
If you are targeting API Levels earlier than 4. The metrics.density
property is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpi
can be calculated
int densityDpi = (int)(metrics.density * 160f);
这篇关于得到屏幕像素密度编程的机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!