forked from Shouheng88/utils-android
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResUtils.java
160 lines (128 loc) · 5.58 KB
/
ResUtils.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package me.shouheng.utils.app;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION_CODES;
import androidx.annotation.ArrayRes;
import androidx.annotation.BoolRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.FontRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.PluralsRes;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.appcompat.content.res.AppCompatResources;
import android.util.TypedValue;
import java.io.InputStream;
import me.shouheng.utils.UtilsApp;
/**
* @author Shouheng Wang 2019-05-08 20:20
*/
public final class ResUtils {
public static float getAttrFloatValue(Context context, int attrRes){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrRes, typedValue, true);
return typedValue.getFloat();
}
public static int getAttrColor(Context context, int attrRes){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrRes, typedValue, true);
return typedValue.data;
}
public static ColorStateList getAttrColorStateList(Context context, int attrRes){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrRes, typedValue, true);
return ContextCompat.getColorStateList(context, typedValue.resourceId);
}
public static Drawable getAttrDrawable(Context context, int attrRes){
int[] attrs = new int[] { attrRes };
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawable = getAttrDrawable(context, ta, 0);
ta.recycle();
return drawable;
}
public static Drawable getAttrDrawable(Context context, TypedArray typedArray, int index){
TypedValue value = typedArray.peekValue(index);
if (value != null) {
if (value.type != TypedValue.TYPE_ATTRIBUTE && value.resourceId != 0) {
return AppCompatResources.getDrawable(context, value.resourceId);
}
}
return null;
}
public static int getAttrDimen(Context context, int attrRes){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrRes, typedValue, true);
return TypedValue.complexToDimensionPixelSize(typedValue.data, context.getResources().getDisplayMetrics());
}
/*----------------------------------wrapper methods--------------------------------------*/
public static int[] getIntArray(@ArrayRes int id) {
return UtilsApp.getApp().getResources().getIntArray(id);
}
public static CharSequence[] getTextArray(@ArrayRes int id) {
return UtilsApp.getApp().getResources().getTextArray(id);
}
public static String[] getStringArray(@ArrayRes int id) {
return UtilsApp.getApp().getResources().getStringArray(id);
}
@ColorInt
public static int getColor(@ColorRes int id) {
return UtilsApp.getApp().getResources().getColor(id);
}
public static String getString(@StringRes int id) {
return UtilsApp.getApp().getResources().getString(id);
}
public static String getString(@StringRes int id, Object... formatArgs) {
return UtilsApp.getApp().getResources().getString(id, formatArgs);
}
public static CharSequence getText(@StringRes int id) {
return UtilsApp.getApp().getResources().getText(id);
}
public static CharSequence getQuantityText(@PluralsRes int id, int quantity) {
return UtilsApp.getApp().getResources().getQuantityText(id, quantity);
}
public static String getQuantityString(@PluralsRes int id, int quantity) {
return UtilsApp.getApp().getResources().getQuantityString(id, quantity);
}
public static String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs) {
return UtilsApp.getApp().getResources().getQuantityString(id, quantity, formatArgs);
}
public static Drawable getDrawable(@DrawableRes int id) {
return UtilsApp.getApp().getResources().getDrawable(id);
}
@TargetApi(VERSION_CODES.O)
public static Typeface getFont(@FontRes int id) {
return UtilsApp.getApp().getResources().getFont(id);
}
public static float getDimension(@DimenRes int id) {
return UtilsApp.getApp().getResources().getDimension(id);
}
public static boolean getBoolean(@BoolRes int id) {
return UtilsApp.getApp().getResources().getBoolean(id);
}
public static int getInteger(@IntegerRes int id) {
return UtilsApp.getApp().getResources().getInteger(id);
}
public static InputStream openRawResource(@RawRes int id) {
return UtilsApp.getApp().getResources().openRawResource(id);
}
public static AssetFileDescriptor openRawResourceFd(@RawRes int id) {
return UtilsApp.getApp().getResources().openRawResourceFd(id);
}
public static AssetManager getAssets() {
return UtilsApp.getApp().getResources().getAssets();
}
/*----------------------------------inner methods--------------------------------------*/
private ResUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}