ScreenUtil.java
2.14 KB
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
package com.drp.mobliemall.utils;
import android.content.Context;
import java.lang.reflect.Field;
/**
* Created by xiniu_wutao on 15/6/30.
* 屏幕帮助类
*/
public class ScreenUtil {
private Context mCtx;
private static ScreenUtil mScreenTools;
public static ScreenUtil instance(Context ctx) {
if (null == mScreenTools) {
mScreenTools = new ScreenUtil(ctx);
}
return mScreenTools;
}
private ScreenUtil(Context ctx) {
mCtx = ctx.getApplicationContext();
}
public int getScreenWidth() {
return mCtx.getResources().getDisplayMetrics().widthPixels;
}
public int dip2px(int dip) {
float density = getDensity(mCtx);
return (int) (dip * density + 0.5);
}
public int px2dip(int px) {
float density = getDensity(mCtx);
return (int) ((px - 0.5) / density);
}
private float getDensity(Context ctx) {
return ctx.getResources().getDisplayMetrics().density;
}
/**
* 540 的分辨率上是85 (
*
* @return
*/
public int getScal() {
return (int) (getScreenWidth() * 100 / 480);
}
/**
* 宽全屏, 根据当前分辨率 动态获取高度
* height 在800*480情况下 的高度
*
* @return
*/
public int get480Height(int height480) {
int width = getScreenWidth();
return (height480 * width / 480);
}
/**
* 获取状态栏高度
*
* @return
*/
public int getStatusBarHeight() {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 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());
sbar = mCtx.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}
public int getScreenHeight() {
return mCtx.getResources().getDisplayMetrics().heightPixels;
}
}