rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * Copyright (C) 2017 Baidu Inc. All rights reserved.
 */
package com.example.datalibrary.utils;
 
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
 
/**
 * 显示设备信息工具类
 */
public final class DensityUtils {
 
    /**
     * 四舍五入
     */
    private static final float DOT_FIVE = 0.5f;
    /**
     * portrait degree:90
     */
    private static final int PORTRAIT_DEGREE_90 = 90;
 
    /**
     * portrait degree:270
     */
    private static final int PORTRAIT_DEGREE_270 = 270;
 
 
    /**
     * 需要比较的设置型号
     */
    private static final String[] BUILD_MODELS = {
            "i700v", // 斐讯i700v
            "A862W", // 夏新A862W
            "V8526"  // 桑菲V8526
    };
 
    /**
     * Private constructor to prohibit nonsense instance creation.
     */
    private DensityUtils() {
    }
 
    /**
     * sp转px.
     *
     * @param context context
     * @param spValue spValue
     * @return 换算后的px值
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
 
    /**
     * dip转换成px
     *
     * @param context Context
     * @param dip     dip Value
     * @return 换算后的px值
     */
    public static int dip2px(Context context, float dip) {
        float density = getDensity(context);
        return (int) (dip * density + DensityUtils.DOT_FIVE);
    }
 
    /**
     * px转换成dip
     *
     * @param context Context
     * @param px      px Value
     * @return 换算后的dip值
     */
    public static int px2dip(Context context, float px) {
        float density = getDensity(context);
        return (int) (px / density + DOT_FIVE);
    }
 
    /**
     * 得到显示宽度
     *
     * @param context Context
     * @return 宽度
     */
    public static int getDisplayWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }
 
    /**
     * 得到显示高度
     *
     * @param context Context
     * @return 高度
     */
    public static int getDisplayHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
 
    /**
     * 得到显示密度
     *
     * @param context Context
     * @return 密度
     */
    public static float getDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }
 
    /**
     * 得到DPI
     *
     * @param context Context
     * @return DPI
     */
    public static int getDensityDpi(Context context) {
        return context.getResources().getDisplayMetrics().densityDpi;
    }
 
 
    /**
     * 判断当前Android系统摄像头旋转多少度
     *
     * @return 当前Android系统能竖着屏幕,
     * 正常应该旋转90度,
     * 但是斐讯i700v、夏新A862W、桑菲V8526需要旋转270度
     */
    public static int getPortraitDegree() {
        int degree = PORTRAIT_DEGREE_90;
        // 为了更好的扩展更多的特殊设置型号,将要比较的设备型号提成一个数组,遍历这个数据。
        for (String model : BUILD_MODELS) {
            if (TextUtils.equals(model, Build.MODEL)) {
                degree = PORTRAIT_DEGREE_270;
                break;
            }
        }
        return degree;
    }
 
 
}