DrawableUtils

public class DrawableUtils {

private DrawableUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}

public static void updateLeftDrawable(Context context,
@NonNull TextView textView,
@DrawableRes int drawableLeftRes) {
updateDrawable(context, textView, drawableLeftRes, 0, 0, 0);
}

public static void updateLeftDrawable(@NonNull TextView textView, @NonNull Drawable leftDrawable) {
textView.setCompoundDrawables(leftDrawable, null, null, null);
}

public static void updateTopDrawable(Context context,
@NonNull TextView textView,
@DrawableRes int drawableTopRes) {
updateDrawable(context, textView, 0, drawableTopRes, 0, 0);
}

public static void updateTopDrawable(@NonNull TextView textView, @NonNull Drawable topDrawable) {
textView.setCompoundDrawables(null, topDrawable, null, null);
}

public static void updateRightDrawable(Context context,
@NonNull TextView textView,
@DrawableRes int drawableRightRes) {
updateDrawable(context, textView, 0, 0, drawableRightRes, 0);
}

public static void updateRightDrawable(@NonNull TextView textView, @NonNull Drawable rightDrawable) {
textView.setCompoundDrawables(null, null, rightDrawable, null);
}

public static void updateBottomDrawable(Context context,
@NonNull TextView textView,
@DrawableRes int drawableBottomRes) {
updateDrawable(context, textView, 0, 0, 0, drawableBottomRes);
}

public static void updateBottomDrawable(@NonNull TextView textView, @NonNull Drawable bottomDrawable) {
textView.setCompoundDrawables(null, null, null, bottomDrawable);
}

public static void updateNonDrawable(Context context, @NonNull TextView textView) {
updateDrawable(context, textView, 0, 0, 0, 0);
}

public static void updateDrawable(Context context,
TextView textView,
int drawableLeftRes,
int drawableTopRes,
int drawableRightRes,
int drawableBottomRes) {
Drawable drawableLeft = null;
Drawable drawableRight = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
if (drawableLeftRes != 0) {
drawableLeft = ContextCompat.getDrawable(context, drawableLeftRes);
drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
}
if (drawableTopRes != 0) {
drawableTop = ContextCompat.getDrawable(context, drawableTopRes);
drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight());
}
if (drawableRightRes != 0) {
drawableRight = ContextCompat.getDrawable(context, drawableRightRes);
drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());
}
if (drawableBottomRes != 0) {
drawableBottom = ContextCompat.getDrawable(context, drawableBottomRes);
drawableBottom.setBounds(0, 0, drawableBottom.getMinimumWidth(), drawableBottom.getMinimumHeight());
}
textView.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
}
}
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
4 DensityUtils
public class DensityUtils {

private DensityUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}(http://www.my516.com)

public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}

public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}

public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}

public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
--------------------- 

posted @ 2019-08-09 19:25  李艳艳665  阅读(218)  评论(0编辑  收藏  举报