forked from azhon/MyUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenSizeUtils.java
More file actions
69 lines (59 loc) · 1.92 KB
/
Copy pathScreenSizeUtils.java
File metadata and controls
69 lines (59 loc) · 1.92 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
package cn.hjtech.pigeon.common.utils;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
/**
* 文件名: SharePreUtils
* 创建时间: 2017/05/24 on 17:58
* 描述:
* 1.获取手机屏幕的宽高
* 2.dip 与 px 之间的转换
*
* @see android.support.annotation.Px
*/
public class ScreenSizeUtils {
private static ScreenSizeUtils instance = null;
private int screenWidth, screenHeight;
public static ScreenSizeUtils getInstance(Context mContext) {
if (instance == null) {
synchronized (ScreenSizeUtils.class) {
if (instance == null)
instance = new ScreenSizeUtils(mContext);
}
}
return instance;
}
private ScreenSizeUtils(Context mContext) {
WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(dm);
// 获取屏幕分辨率宽度
screenWidth = dm.widthPixels;
// 获取屏幕分辨率高度
screenHeight = dm.heightPixels;
}
//获取屏幕宽度
public int getScreenWidth() {
return screenWidth;
}
//获取屏幕高度
public int getScreenHeight() {
return screenHeight;
}
/**
* 根据手机的分辨率从 dip 的单位 转成为 px(像素)
*
* @return px
*/
private int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}