ScaleUtils.java
1.91 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
package com.mylhyl.circledialog.scale;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
/**
* Created by hupei on 2016/3/8 17:19.
*/
public class ScaleUtils {
public static int scaleValue(int val) {
return (int) (val * ScaleLayoutConfig.getInstance().getScale());
}
public static int[] getRealScreenSize(Context context) {
int[] size = new int[2];
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
} else if (Build.VERSION.SDK_INT >= 17) {// includes window decorations (statusbar bar/menu bar)
Point realSize = new Point();
d.getRealSize(realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
}
size[0] = widthPixels;
size[1] = heightPixels;
return size;
}
public static double getDevicePhysicalSize(Context context) {
int[] size = getRealScreenSize(context);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
double x = Math.pow(size[0] / dm.xdpi, 2);
double y = Math.pow(size[1] / dm.ydpi, 2);
return Math.sqrt(x + y);
}
}