获取屏幕尺寸,包括状态栏

一大波干货来袭,屏幕尺寸各种获得,状态栏尺寸可正确获得,亲测。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package  com.elyar.app.util.measure;
 
import  java.lang.reflect.Field;
import  android.app.Activity;
import  android.util.DisplayMetrics;
public  class  DisplayMeasure {
  /**
   * Note:个人经验不服来辩<br>
   * 只有activity可以使用getWindowManager,否则应该使用<br>
   * Context.getResources().getDisplayMetrics()来获取.<br>
  
   * @author ElyarAnwar 2016-1-29 21:24 Aksu
  
   */
  public  DisplayMeasure() {
  
  }
  
  
  /**
   * 获取屏幕尺寸,单位为像素
   * @param context Activity的上下文
   * @return 屏幕宽度 单位PX
   */
  public  static  int  getScreenWidthPixel(Activity context){
   DisplayMetrics outMetrics =  new  DisplayMetrics();
   context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
   int  widthPixels = outMetrics.widthPixels;
   return  widthPixels;
  }
  
  /**
   * 获取屏幕尺寸,单位为像素
   * @param context Activity的上下文
   * @return 屏幕高度 单位PX
   */
  public  static  int  getScreenHeightPixel(Activity context){
   DisplayMetrics outMetrics =  new  DisplayMetrics();
   context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
   int  heightPixels = outMetrics.heightPixels;
   return  heightPixels;
  }
  
  /**
   * 获取屏幕尺寸,单位为dp
   * @param context Activity的上下文
   * @return 屏幕宽度 单位Dp
   */
  public  static  int  getScreenWidthInDp(Activity context){
   DisplayMetrics outMetrics =  new  DisplayMetrics();
   context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
   int  widthDp = outMetrics.widthPixels;
   return  ChangeUnits.px2dp(context, widthDp); //将PX转换成Dp
  }
  
  /**
   * 获取屏幕尺寸,单位为dp
   * @param context Activity的上下文
   * @return 屏幕高度 单位Dp
   */
  public  static  int  getScreenHeightInDp(Activity context){
   DisplayMetrics outMetrics =  new  DisplayMetrics();
   context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
   int  heightDp = outMetrics.heightPixels;
   return  ChangeUnits.px2dp(context, heightDp);
  }
  
  
  /**
   * 获取状态栏高度
   * @param context 上下文
   * @return 状态栏高度 单位像素
   */
  public  static  int  getStatusBarHeightPixel(Activity context){ 
         Class<?> c =  null
         Object obj =  null
         Field field =  null
         int  x =  0 , statusBarHeight =  0
         try 
             c = Class.forName( "com.android.internal.R$dimen" ); 
             obj = c.newInstance(); 
             field = c.getField( "status_bar_height" ); 
             x = Integer.parseInt(field.get(obj).toString()); 
             statusBarHeight = context.getResources().getDimensionPixelSize(x);  
         catch  (Exception e1) { 
             e1.printStackTrace(); 
         }  
         return  statusBarHeight; 
     }
  
  
  /**
   * 获取状态栏高度
   * @param context 上下文
   * @return 状态栏高度 单位dp
   */
  public  static  int  getStatusBarHeightInDp(Activity context){ 
         Class<?> c =  null
         Object obj =  null
         Field field =  null
         int  x =  0 , statusBarHeight =  0
         try 
             c = Class.forName( "com.android.internal.R$dimen" ); 
             obj = c.newInstance(); 
             field = c.getField( "status_bar_height" ); 
             x = Integer.parseInt(field.get(obj).toString()); 
             statusBarHeight = context.getResources().getDimensionPixelSize(x);  
         catch  (Exception e1) { 
             e1.printStackTrace(); 
         }  
         return  ChangeUnits.px2dp(context, statusBarHeight); 
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值