简体   繁体   English

有没有办法获得dpi的屏幕尺寸

[英]Is there a way to get screen size in dpi

I'm using this: 我正在使用这个:

int wi=getWindowManager().getDefaultDisplay().getWidth();

to get the screen size in pixels. 获取以像素为单位的屏幕尺寸。 Is there a way to get the screen size in dpi ??? 有没有办法获得dpi的屏幕尺寸?

I'm doing this to select different layouts base on the screen size. 我这样做是根据屏幕尺寸选择不同的布局。

Is there a way to get the escreen size in dpi??? 有没有办法获得以dpi为单位的电子屏幕尺寸???

DisplayMetrics can tell you the screen size in pixels, plus the screen density. DisplayMetrics可以告诉您屏幕大小(以像素为单位)以及屏幕密度。 From there, you can calculate the screen size in dp . 从那里,您可以计算dp的屏幕大小。

I'm doing this to select different layouts base on the screen size. 我这样做是根据屏幕尺寸选择不同的布局。

This is automatically handled for you by the resource framework, if you put your layouts in the proper directories (eg, res/layout-large/ , res/layout-sw600dp/ ). 如果您将布局放在正确的目录中(例如res/layout-large/res/layout-sw600dp/ ),那么资源框架会自动为您处理。

you must create metric object and do below 您必须创建指标对象并在下面执行

 public class LocalUtil extends Application {
private static Context context1;
private static DisplayMetrics  metrics;
public static void setContext(Context context)
{

        context1=context;
        metrics=context1.getResources().getDisplayMetrics();
}
public static float getDensity()
{
    return metrics.density;
}
public static int getScreenWidth()
{
    return metrics.widthPixels;
}
public static int getScreenHeight()
{
    return metrics.heightPixels;
}
public static float getScreenWidthInDpi()
{
    return metrics.widthPixels/metrics.density;
}
public static float getScreenHeightInDpi()
{
    return metrics.heightPixels/metrics.density;
}

and every you want to use this method you can set context with setcontext method and call best method with your purpose like this code this code is oncreateView of main activity: 每个想要使用此方法的人都可以使用setcontext方法设置上下文,并根据您的目的调用最佳方法,例如此代码,此代码位于主要活动的oncreateView上:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    LocalUtil.setContext(getApplicationContext());
    LocalUtil.getScreenWidthInDpi();
    //or call  each method you want
  }

The "screen size in dpi" is a meaningless statement. “以dpi为单位的屏幕尺寸”是毫无意义的陈述。 Do you just want to get the DPI of the display? 您是否只想获取显示器的DPI?

Secondly, don't do this . 其次, 不要这样做

Seriously, stop. 认真一点,停下来。 Don't do it. 不要这样

Use the layout folders as they are intended. 按预期使用布局文件夹。 If you need a different layout for HDPI, put your custom layout in layout-hdpi . 如果您需要不同的HDPI布局,请将自定义布局放入layout-hdpi

That said, if you just need the density: 就是说,如果您只需要密度:

DisplayMetrics metrics = new DisplayMetrics();
getWindow().getDisplayMetrics(metrics);
int dpi = metrics.densityDpi;

Here is a way to calculate the width and height of the screen in pixels. 这是一种以像素为单位计算屏幕的宽度和高度的方法。

int widthPixels = getWindowManager().getDefaultDisplay().getWidth();
int heightPixels = getWindowManager().getDefaultDisplay().getHeight();

float scale = getApplicationContext().getResources().getDisplayMetrics().density;

int width = (int) (widthPixels - 0.5f)/scale; //width of screen in dpi
int height = (int) (heightPixels - 0.5f)/scale; //height of screen in dpi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM