ResLoader.java
9.6 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.cnlive.goldenline.util;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Movie;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
/**
* res 资源文件加载工具 TODO:TODO Date:2015年9月17日下午1:53:19 Author:萧
*/
public class ResLoader {
/**
* 生成CheckBox 或者 RadioButton 背景状态
*
* @param context
* @param checkedRes 选中
* @param unCheckedRes 未选中
* @param disableRes 不可用
* @return
*/
@NonNull
public static Drawable generateCheckBoxBgDrawable(@NonNull Context context, int checkedRes, int unCheckedRes, int disableRes) {
Drawable checkedDrawable = Drawable(context, checkedRes);
Drawable unCheckedDrawable = Drawable(context, unCheckedRes);
StateListDrawable stateListDrawable = new StateListDrawable();
int checked = android.R.attr.state_checked;
if (disableRes != 0) {
Drawable disAbleDrawable = Drawable(context, disableRes);
int enable = android.R.attr.state_enabled;
stateListDrawable.addState(new int[]{-enable}, disAbleDrawable);
}
stateListDrawable.addState(new int[]{-checked}, unCheckedDrawable);
stateListDrawable.addState(new int[]{checked}, checkedDrawable);
return stateListDrawable;
}
/**
* 注意调用时如果使用setBackground 需判断 api版本 16以下没有该方法 由资源drawable Id 使用代码构建点击效果
*
* @param normalRes 默认状态 非点击态
* @param pressedRes 点击态 选中 或者 具有焦点
* @param disableRes 不可用状态 没有可为0
* @return
*/
@NonNull
public static Drawable generateClickBgDrawable(@NonNull Context context, int normalRes, int pressedRes, int disableRes) {
Drawable normalDrawable = Drawable(context, normalRes);
Drawable pressedDrawable = Drawable(context, pressedRes);
StateListDrawable stateListDrawable = new StateListDrawable();
int pressed = android.R.attr.state_pressed;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
if (disableRes != 0) {
Drawable disAbleDrawable = Drawable(context, disableRes);
int enable = android.R.attr.state_enabled;
stateListDrawable.addState(new int[]{-enable}, disAbleDrawable);
}
stateListDrawable.addState(new int[]{-focused, -pressed, -selected}, normalDrawable);
stateListDrawable.addState(new int[]{-pressed, selected}, pressedDrawable);
stateListDrawable.addState(new int[]{focused, -pressed, -selected}, pressedDrawable);
stateListDrawable.addState(new int[]{focused, -pressed, selected}, pressedDrawable);
stateListDrawable.addState(new int[]{pressed}, pressedDrawable);
return stateListDrawable;
}
/**
* 设置控件点击效果
*
* @param view
* @param context
* @param normalRes
* @param pressedRes
* @param disableRes
*/
@SuppressWarnings("deprecation")
public static void setBackgroundDrawable(View view, @NonNull Context context, int normalRes, int pressedRes, int disableRes) {
Drawable drawable = generateClickBgDrawable(context, normalRes, pressedRes, disableRes);
if (view instanceof ImageView) {
((ImageView) view).setImageDrawable(drawable);
} else if (view instanceof ImageButton) {
((ImageButton) view).setImageDrawable(drawable);
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
}
/**
* 设置控件选中效果
*
* @param view
* @param context
* @param checkedRes
* @param unCheckedRes
* @param disableRes
*/
@SuppressWarnings("deprecation")
public static void setChosedBgDrawable(View view, @NonNull Context context, int checkedRes, int unCheckedRes, int disableRes) {
Drawable drawable = generateCheckBoxBgDrawable(context, checkedRes, unCheckedRes, disableRes);
if (view instanceof CheckBox) {
((CheckBox) view).setButtonDrawable(drawable);
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
}
@NonNull
public static ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};
int[][] states = new int[6][];
states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
states[2] = new int[]{android.R.attr.state_enabled};
states[3] = new int[]{android.R.attr.state_focused};
states[4] = new int[]{android.R.attr.state_window_focused};
states[5] = new int[]{};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
}
@Nullable
public static Object loadRes(@NonNull ResType type, @Nullable Context context, int id) {
if (context == null || id < 1) return null;
switch (type) {
case Animation:
return Animation(context, id);
case Boolean:
return Boolean(context, id);
case Color:
return Color(context, id);
case ColorStateList:
return ColorStateList(context, id);
case Dimension:
return Dimension(context, id);
case DimensionPixelOffset:
return DimensionPixelOffset(context, id);
case DimensionPixelSize:
return DimensionPixelSize(context, id);
case Drawable:
return Drawable(context, id);
case Integer:
return Integer(context, id);
case IntArray:
return IntArray(context, id);
case Movie:
return Movie(context, id);
case String:
return String(context, id);
case StringArray:
return StringArray(context, id);
case Text:
return Text(context, id);
case TextArray:
return TextArray(context, id);
case Xml:
return Xml(context, id);
default:
break;
}
return null;
}
public static Animation Animation(Context context, int id) {
return AnimationUtils.loadAnimation(context, id);
}
public static boolean Boolean(@NonNull Context context, int id) {
return context.getResources().getBoolean(id);
}
public static int Color(@NonNull Context context, int id) {
return context.getResources().getColor(id);
}
@Nullable
public static ColorStateList ColorStateList(@NonNull Context context, int id) {
return context.getResources().getColorStateList(id);
}
public static float Dimension(@NonNull Context context, int id) {
return context.getResources().getDimension(id);
}
public static int DimensionPixelOffset(@NonNull Context context, int id) {
return context.getResources().getDimensionPixelOffset(id);
}
public static int DimensionPixelSize(@NonNull Context context, int id) {
return context.getResources().getDimensionPixelSize(id);
}
@Nullable
@SuppressWarnings("deprecation")
public static Drawable Drawable(@NonNull Context context, int id) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return context.getResources().getDrawable(id);
} else {
Drawable drawable = context.getResources().getDrawable(id, null);
return drawable == null ? ResourcesCompat.getDrawable(context.getResources(), id, null) : drawable;
}
}
public static int Integer(@NonNull Context context, int id) {
return context.getResources().getInteger(id);
}
public static int[] IntArray(@NonNull Context context, int id) {
return context.getResources().getIntArray(id);
}
public static Movie Movie(@NonNull Context context, int id) {
return context.getResources().getMovie(id);
}
public static String String(@NonNull Context context, int id) {
return context.getResources().getString(id);
}
public static String[] StringArray(@NonNull Context context, int id) {
return context.getResources().getStringArray(id);
}
public static CharSequence Text(@NonNull Context context, int id) {
return context.getResources().getText(id);
}
public static CharSequence[] TextArray(@NonNull Context context, int id) {
return context.getResources().getTextArray(id);
}
public static XmlResourceParser Xml(@NonNull Context context, int id) {
return context.getResources().getXml(id);
}
}