现在的位置: 首页 > 综合 > 正文

关于android手机屏幕密度的实测和状态栏的高度

2013年08月19日 ⁄ 综合 ⁄ 共 1737字 ⁄ 字号 评论关闭

这是获取状态栏的高度:

 public static int getStatusHeight(Activity activity){
         int statusHeight = 0;
         Rect localRect = new Rect();
         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
         statusHeight = localRect.top;
         if (0 == statusHeight){
             Class<?> localClass;
             try {
                 localClass = Class.forName("com.android.internal.R$dimen");
                 Object localObject = localClass.newInstance();
                 int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
                 statusHeight = activity.getResources().getDimensionPixelSize(i5);
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             } catch (InstantiationException e) {
                 e.printStackTrace();
             } catch (NumberFormatException e) {
                 e.printStackTrace();
             } catch (IllegalArgumentException e) {
                 e.printStackTrace();
             } catch (SecurityException e) {
                 e.printStackTrace();
             } catch (NoSuchFieldException e) {
                 e.printStackTrace();
             }
         }
         return statusHeight;
     }

 

 

这是关于屏幕密度的方法:

DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);

        textView3.setText("屏幕密度:"+metric.density);
        textView4.setText("屏幕密度DPI:"+metric.densityDpi);

 

 

经过实测得出结论如下:

低于160的按照160,高于160小于240的按照240,大于240的按照320

型号                                实际屏幕密度           通过代码获得

GALAXY Note II            267ppi                        320

lg n4                                318ppi                       320

 

 

 

 

 

 

根据google的推荐,像素统一使用dip,字体统一使用sp

 

 

 

 

 

 

 

抱歉!评论已关闭.