items) {
68 | this.context = context;
69 | this.items = items;
70 | }
71 |
72 | @NonNull
73 | @Override
74 | public SkinViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
75 | LayoutInflater layoutInflater = LayoutInflater.from(context);
76 | return new SkinViewHolder(layoutInflater.inflate(R.layout.item_test, null));
77 | }
78 |
79 | @Override
80 | public void onBindViewHolder(@NonNull SkinViewHolder skinViewHolder, int i) {
81 | skinViewHolder.setData(items.get(i));
82 | }
83 |
84 | @Override
85 | public int getItemCount() {
86 | return items.size();
87 | }
88 | }
89 |
90 | public static class SkinViewHolder extends RecyclerView.ViewHolder {
91 | private TextView textView;
92 |
93 | public SkinViewHolder(@NonNull View itemView) {
94 | super(itemView);
95 | textView = itemView.findViewById(R.id.tv_item);
96 | ChangeSkinHelper.setSkin(textView);
97 | }
98 |
99 | public void setData(String item) {
100 | ChangeSkinHelper.applyViews(itemView);
101 | textView.setText(item);
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/app/src/main/java/net/arvin/changeskinhelper/sample/SecondActivity.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.sample;
2 |
3 | import android.Manifest;
4 | import android.os.Environment;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 | import android.view.View;
8 |
9 | import net.arvin.changeskinhelper.ChangeSkinHelper;
10 | import net.arvin.changeskinhelper.core.ChangeSkinActivity;
11 | import net.arvin.changeskinhelper.core.ChangeSkinPreferenceUtil;
12 | import net.arvin.permissionhelper.PermissionUtil;
13 |
14 | import java.io.File;
15 |
16 | public class SecondActivity extends ChangeSkinActivity {
17 |
18 | private int currSkinIndex;
19 | private PermissionUtil permissionUtil;
20 |
21 | @Override
22 | protected boolean isChangeSkin() {
23 | return true;
24 | }
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_second);
30 | setBarsColor();
31 | getSupportFragmentManager().beginTransaction().add(R.id.layout_main, new MainFragment()).commit();
32 | String suffix = ChangeSkinPreferenceUtil.getString(getApplicationContext(), ChangeSkinHelper.KEY_SKIN_SUFFIX);
33 | currSkinIndex = MainActivity.getSkinIndexBySuffix(suffix);
34 | }
35 |
36 | public void changeSkin(View view) {
37 | currSkinIndex = (currSkinIndex + 1) % 4;
38 | String suffix = MainActivity.getSuffixBySkinIndex(currSkinIndex);
39 | if (currSkinIndex == 3) {
40 | final String skinPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "blue.skin";
41 | if (permissionUtil == null) {
42 | permissionUtil = new PermissionUtil.Builder().with(this).build();
43 | }
44 | final String tempSuffix = suffix;
45 | permissionUtil.request("需要读取文件权限", Manifest.permission.READ_EXTERNAL_STORAGE,
46 | new PermissionUtil.RequestPermissionListener() {
47 | @Override
48 | public void callback(boolean granted, boolean isAlwaysDenied) {
49 | dynamicSkin(skinPath, tempSuffix);
50 | }
51 | });
52 | } else {
53 | dynamicSkin(suffix);
54 | }
55 | }
56 |
57 | public void defaultSkin(View view) {
58 | defaultSkin();
59 | currSkinIndex = 0;
60 | }
61 |
62 | @Override
63 | public void changeSkin() {
64 | super.changeSkin();
65 | setBarsColor();
66 | }
67 |
68 | private void setBarsColor() {
69 | StatusBarUtil.setColorNoTranslucent(this, ChangeSkinHelper.getColor(R.color.colorPrimary));
70 | ChangeSkinHelper.setNavigation(this, R.color.colorPrimary);
71 | ChangeSkinHelper.setActionBar(this, R.color.colorPrimary);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/java/net/arvin/changeskinhelper/sample/StatusBarUtil.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.sample;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Color;
8 | import android.os.Build;
9 | import android.support.annotation.ColorInt;
10 | import android.support.annotation.IntRange;
11 | import android.support.annotation.NonNull;
12 | import android.support.design.widget.CoordinatorLayout;
13 | import android.support.v4.widget.DrawerLayout;
14 | import android.view.View;
15 | import android.view.ViewGroup;
16 | import android.view.Window;
17 | import android.view.WindowManager;
18 | import android.widget.LinearLayout;
19 |
20 | import java.lang.reflect.Field;
21 | import java.lang.reflect.Method;
22 |
23 | /**
24 | * Created by Jaeger on 16/2/14.
25 | *
26 | * Email: chjie.jaeger@gmail.com
27 | * GitHub: https://github.com/laobie
28 | */
29 | public class StatusBarUtil {
30 |
31 | public static final int DEFAULT_STATUS_BAR_ALPHA = 112;
32 | private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view;
33 | private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view;
34 | private static final int TAG_KEY_HAVE_SET_OFFSET = -123;
35 |
36 | public static void forNightStatusBar(Activity activity, int colorFor4_4) {
37 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
38 | TypedArray a = activity.getTheme().obtainStyledAttributes(0, new int[]{
39 | android.R.attr.statusBarColor
40 | });
41 | int color = a.getColor(0, 0);
42 | activity.getWindow().setStatusBarColor(color);
43 | a.recycle();
44 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
45 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
46 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
47 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
48 | if (fakeStatusBarView != null) {
49 | if (fakeStatusBarView.getVisibility() == View.GONE) {
50 | fakeStatusBarView.setVisibility(View.VISIBLE);
51 | }
52 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(colorFor4_4, 0));
53 | } else {
54 | decorView.addView(createStatusBarView(activity, colorFor4_4, 0));
55 | }
56 | setRootView(activity);
57 | }
58 | }
59 |
60 | /**
61 | * 设置状态栏颜色
62 | *
63 | * @param activity 需要设置的 activity
64 | * @param color 状态栏颜色值
65 | */
66 | public static void setColor(Activity activity, @ColorInt int color) {
67 | setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA);
68 | }
69 |
70 | /**
71 | * 设置状态栏颜色
72 | *
73 | * @param activity 需要设置的activity
74 | * @param color 状态栏颜色值
75 | * @param statusBarAlpha 状态栏透明度
76 | */
77 | public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
78 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
79 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
80 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
81 | activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
82 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
83 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
84 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
85 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
86 | if (fakeStatusBarView != null) {
87 | if (fakeStatusBarView.getVisibility() == View.GONE) {
88 | fakeStatusBarView.setVisibility(View.VISIBLE);
89 | }
90 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
91 | } else {
92 | decorView.addView(createStatusBarView(activity, color, statusBarAlpha));
93 | }
94 | setRootView(activity);
95 | }
96 | }
97 |
98 | /**
99 | * 为滑动返回界面设置状态栏颜色
100 | *
101 | * @param activity 需要设置的activity
102 | * @param color 状态栏颜色值
103 | */
104 | public static void setColorForSwipeBack(Activity activity, int color) {
105 | setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA);
106 | }
107 |
108 | /**
109 | * 为滑动返回界面设置状态栏颜色
110 | *
111 | * @param activity 需要设置的activity
112 | * @param color 状态栏颜色值
113 | * @param statusBarAlpha 状态栏透明度
114 | */
115 | public static void setColorForSwipeBack(Activity activity, @ColorInt int color,
116 | @IntRange(from = 0, to = 255) int statusBarAlpha) {
117 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
118 |
119 | ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content));
120 | View rootView = contentView.getChildAt(0);
121 | int statusBarHeight = getStatusBarHeight(activity);
122 | if (rootView != null && rootView instanceof CoordinatorLayout) {
123 | final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView;
124 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
125 | coordinatorLayout.setFitsSystemWindows(false);
126 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
127 | boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight;
128 | if (isNeedRequestLayout) {
129 | contentView.setPadding(0, statusBarHeight, 0, 0);
130 | coordinatorLayout.post(new Runnable() {
131 | @Override
132 | public void run() {
133 | coordinatorLayout.requestLayout();
134 | }
135 | });
136 | }
137 | } else {
138 | coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha));
139 | }
140 | } else {
141 | contentView.setPadding(0, statusBarHeight, 0, 0);
142 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
143 | }
144 | setTransparentForWindow(activity);
145 | }
146 | }
147 |
148 | /**
149 | * 设置状态栏纯色 不加半透明效果
150 | *
151 | * @param activity 需要设置的 activity
152 | * @param color 状态栏颜色值
153 | */
154 | public static void setColorNoTranslucent(Activity activity, @ColorInt int color) {
155 | setColor(activity, color, 0);
156 | }
157 |
158 | /**
159 | * 设置状态栏颜色(5.0以下无半透明效果,不建议使用)
160 | *
161 | * @param activity 需要设置的 activity
162 | * @param color 状态栏颜色值
163 | */
164 | @Deprecated
165 | public static void setColorDiff(Activity activity, @ColorInt int color) {
166 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
167 | return;
168 | }
169 | transparentStatusBar(activity);
170 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
171 | // 移除半透明矩形,以免叠加
172 | View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
173 | if (fakeStatusBarView != null) {
174 | if (fakeStatusBarView.getVisibility() == View.GONE) {
175 | fakeStatusBarView.setVisibility(View.VISIBLE);
176 | }
177 | fakeStatusBarView.setBackgroundColor(color);
178 | } else {
179 | contentView.addView(createStatusBarView(activity, color));
180 | }
181 | setRootView(activity);
182 | }
183 |
184 | /**
185 | * 使状态栏半透明
186 | *
187 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏
188 | *
189 | * @param activity 需要设置的activity
190 | */
191 | public static void setTranslucent(Activity activity) {
192 | setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
193 | }
194 |
195 | /**
196 | * 使状态栏半透明
197 | *
198 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏
199 | *
200 | * @param activity 需要设置的activity
201 | * @param statusBarAlpha 状态栏透明度
202 | */
203 | public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
204 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
205 | return;
206 | }
207 | setTransparent(activity);
208 | addTranslucentView(activity, statusBarAlpha);
209 | }
210 |
211 | /**
212 | * 针对根布局是 CoordinatorLayout, 使状态栏半透明
213 | *
214 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏
215 | *
216 | * @param activity 需要设置的activity
217 | * @param statusBarAlpha 状态栏透明度
218 | */
219 | public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
220 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
221 | return;
222 | }
223 | transparentStatusBar(activity);
224 | addTranslucentView(activity, statusBarAlpha);
225 | }
226 |
227 | /**
228 | * 设置状态栏全透明
229 | *
230 | * @param activity 需要设置的activity
231 | */
232 | public static void setTransparent(Activity activity) {
233 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
234 | return;
235 | }
236 | transparentStatusBar(activity);
237 | setRootView(activity);
238 | }
239 |
240 | /**
241 | * 使状态栏透明(5.0以上半透明效果,不建议使用)
242 | *
243 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏
244 | *
245 | * @param activity 需要设置的activity
246 | */
247 | @Deprecated
248 | public static void setTranslucentDiff(Activity activity) {
249 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
250 | // 设置状态栏透明
251 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
252 | setRootView(activity);
253 | }
254 | }
255 |
256 | /**
257 | * 为DrawerLayout 布局设置状态栏变色
258 | *
259 | * @param activity 需要设置的activity
260 | * @param drawerLayout DrawerLayout
261 | * @param color 状态栏颜色值
262 | */
263 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
264 | setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA);
265 | }
266 |
267 | /**
268 | * 为DrawerLayout 布局设置状态栏颜色,纯色
269 | *
270 | * @param activity 需要设置的activity
271 | * @param drawerLayout DrawerLayout
272 | * @param color 状态栏颜色值
273 | */
274 | public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
275 | setColorForDrawerLayout(activity, drawerLayout, color, 0);
276 | }
277 |
278 | /**
279 | * 为DrawerLayout 布局设置状态栏变色
280 | *
281 | * @param activity 需要设置的activity
282 | * @param drawerLayout DrawerLayout
283 | * @param color 状态栏颜色值
284 | * @param statusBarAlpha 状态栏透明度
285 | */
286 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
287 | @IntRange(from = 0, to = 255) int statusBarAlpha) {
288 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
289 | return;
290 | }
291 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
292 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
293 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
294 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
295 | } else {
296 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
297 | }
298 | // 生成一个状态栏大小的矩形
299 | // 添加 statusBarView 到布局中
300 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
301 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
302 | if (fakeStatusBarView != null) {
303 | if (fakeStatusBarView.getVisibility() == View.GONE) {
304 | fakeStatusBarView.setVisibility(View.VISIBLE);
305 | }
306 | fakeStatusBarView.setBackgroundColor(color);
307 | } else {
308 | contentLayout.addView(createStatusBarView(activity, color), 0);
309 | }
310 | // 内容布局不是 LinearLayout 时,设置padding top
311 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
312 | contentLayout.getChildAt(1)
313 | .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
314 | contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
315 | }
316 | // 设置属性
317 | setDrawerLayoutProperty(drawerLayout, contentLayout);
318 | addTranslucentView(activity, statusBarAlpha);
319 | }
320 |
321 | /**
322 | * 设置 DrawerLayout 属性
323 | *
324 | * @param drawerLayout DrawerLayout
325 | * @param drawerLayoutContentLayout DrawerLayout 的内容布局
326 | */
327 | private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) {
328 | ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
329 | drawerLayout.setFitsSystemWindows(false);
330 | drawerLayoutContentLayout.setFitsSystemWindows(false);
331 | drawerLayoutContentLayout.setClipToPadding(true);
332 | drawer.setFitsSystemWindows(false);
333 | }
334 |
335 | /**
336 | * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
337 | *
338 | * @param activity 需要设置的activity
339 | * @param drawerLayout DrawerLayout
340 | * @param color 状态栏颜色值
341 | */
342 | @Deprecated
343 | public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
344 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
345 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
346 | // 生成一个状态栏大小的矩形
347 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
348 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
349 | if (fakeStatusBarView != null) {
350 | if (fakeStatusBarView.getVisibility() == View.GONE) {
351 | fakeStatusBarView.setVisibility(View.VISIBLE);
352 | }
353 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA));
354 | } else {
355 | // 添加 statusBarView 到布局中
356 | contentLayout.addView(createStatusBarView(activity, color), 0);
357 | }
358 | // 内容布局不是 LinearLayout 时,设置padding top
359 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
360 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
361 | }
362 | // 设置属性
363 | setDrawerLayoutProperty(drawerLayout, contentLayout);
364 | }
365 | }
366 |
367 | /**
368 | * 为 DrawerLayout 布局设置状态栏透明
369 | *
370 | * @param activity 需要设置的activity
371 | * @param drawerLayout DrawerLayout
372 | */
373 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
374 | setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
375 | }
376 |
377 | /**
378 | * 为 DrawerLayout 布局设置状态栏透明
379 | *
380 | * @param activity 需要设置的activity
381 | * @param drawerLayout DrawerLayout
382 | */
383 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout,
384 | @IntRange(from = 0, to = 255) int statusBarAlpha) {
385 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
386 | return;
387 | }
388 | setTransparentForDrawerLayout(activity, drawerLayout);
389 | addTranslucentView(activity, statusBarAlpha);
390 | }
391 |
392 | /**
393 | * 为 DrawerLayout 布局设置状态栏透明
394 | *
395 | * @param activity 需要设置的activity
396 | * @param drawerLayout DrawerLayout
397 | */
398 | public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
399 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
400 | return;
401 | }
402 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
403 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
404 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
405 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
406 | } else {
407 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
408 | }
409 |
410 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
411 | // 内容布局不是 LinearLayout 时,设置padding top
412 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
413 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
414 | }
415 |
416 | // 设置属性
417 | setDrawerLayoutProperty(drawerLayout, contentLayout);
418 | }
419 |
420 | /**
421 | * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
422 | *
423 | * @param activity 需要设置的activity
424 | * @param drawerLayout DrawerLayout
425 | */
426 | @Deprecated
427 | public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) {
428 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
429 | // 设置状态栏透明
430 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
431 | // 设置内容布局属性
432 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
433 | contentLayout.setFitsSystemWindows(true);
434 | contentLayout.setClipToPadding(true);
435 | // 设置抽屉布局属性
436 | ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
437 | vg.setFitsSystemWindows(false);
438 | // 设置 DrawerLayout 属性
439 | drawerLayout.setFitsSystemWindows(false);
440 | }
441 | }
442 |
443 | /**
444 | * 为头部是 ImageView 的界面设置状态栏全透明
445 | *
446 | * @param activity 需要设置的activity
447 | * @param needOffsetView 需要向下偏移的 View
448 | */
449 | public static void setTransparentForImageView(Activity activity, View needOffsetView) {
450 | setTranslucentForImageView(activity, 0, needOffsetView);
451 | }
452 |
453 | /**
454 | * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度)
455 | *
456 | * @param activity 需要设置的activity
457 | * @param needOffsetView 需要向下偏移的 View
458 | */
459 | public static void setTranslucentForImageView(Activity activity, View needOffsetView) {
460 | setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
461 | }
462 |
463 | /**
464 | * 为头部是 ImageView 的界面设置状态栏透明
465 | *
466 | * @param activity 需要设置的activity
467 | * @param statusBarAlpha 状态栏透明度
468 | * @param needOffsetView 需要向下偏移的 View
469 | */
470 | public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
471 | View needOffsetView) {
472 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
473 | return;
474 | }
475 | setTransparentForWindow(activity);
476 | addTranslucentView(activity, statusBarAlpha);
477 | if (needOffsetView != null) {
478 | Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);
479 | if (haveSetOffset != null && (Boolean) haveSetOffset) {
480 | return;
481 | }
482 | ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
483 | layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
484 | layoutParams.rightMargin, layoutParams.bottomMargin);
485 | needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
486 | }
487 | }
488 |
489 | /**
490 | * 为 fragment 头部是 ImageView 的设置状态栏透明
491 | *
492 | * @param activity fragment 对应的 activity
493 | * @param needOffsetView 需要向下偏移的 View
494 | */
495 | public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) {
496 | setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
497 | }
498 |
499 | /**
500 | * 为 fragment 头部是 ImageView 的设置状态栏透明
501 | *
502 | * @param activity fragment 对应的 activity
503 | * @param needOffsetView 需要向下偏移的 View
504 | */
505 | public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) {
506 | setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
507 | }
508 |
509 | /**
510 | * 为 fragment 头部是 ImageView 的设置状态栏透明
511 | *
512 | * @param activity fragment 对应的 activity
513 | * @param statusBarAlpha 状态栏透明度
514 | * @param needOffsetView 需要向下偏移的 View
515 | */
516 | public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
517 | View needOffsetView) {
518 | setTranslucentForImageView(activity, statusBarAlpha, needOffsetView);
519 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
520 | clearPreviousSetting(activity);
521 | }
522 | }
523 |
524 | /**
525 | * 隐藏伪状态栏 View
526 | *
527 | * @param activity 调用的 Activity
528 | */
529 | public static void hideFakeStatusBarView(Activity activity) {
530 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
531 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
532 | if (fakeStatusBarView != null) {
533 | fakeStatusBarView.setVisibility(View.GONE);
534 | }
535 | View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
536 | if (fakeTranslucentView != null) {
537 | fakeTranslucentView.setVisibility(View.GONE);
538 | }
539 | }
540 |
541 | @TargetApi(Build.VERSION_CODES.M)
542 | public static void setLightMode(Activity activity) {
543 | setMIUIStatusBarDarkIcon(activity, true);
544 | setMeizuStatusBarDarkIcon(activity, true);
545 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
546 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
547 | }
548 | }
549 |
550 | @TargetApi(Build.VERSION_CODES.M)
551 | public static void setDarkMode(Activity activity) {
552 | setMIUIStatusBarDarkIcon(activity, false);
553 | setMeizuStatusBarDarkIcon(activity, false);
554 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
555 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
556 | }
557 | }
558 |
559 | /**
560 | * 修改 MIUI V6 以上状态栏颜色
561 | */
562 | private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
563 | Class extends Window> clazz = activity.getWindow().getClass();
564 | try {
565 | Class> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
566 | Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
567 | int darkModeFlag = field.getInt(layoutParams);
568 | Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
569 | extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
570 | } catch (Exception e) {
571 | //e.printStackTrace();
572 | }
573 | }
574 |
575 | /**
576 | * 修改魅族状态栏字体颜色 Flyme 4.0
577 | */
578 | private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
579 | try {
580 | WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
581 | Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
582 | Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
583 | darkFlag.setAccessible(true);
584 | meizuFlags.setAccessible(true);
585 | int bit = darkFlag.getInt(null);
586 | int value = meizuFlags.getInt(lp);
587 | if (darkIcon) {
588 | value |= bit;
589 | } else {
590 | value &= ~bit;
591 | }
592 | meizuFlags.setInt(lp, value);
593 | activity.getWindow().setAttributes(lp);
594 | } catch (Exception e) {
595 | //e.printStackTrace();
596 | }
597 | }
598 |
599 | ///////////////////////////////////////////////////////////////////////////////////
600 |
601 | @TargetApi(Build.VERSION_CODES.KITKAT)
602 | private static void clearPreviousSetting(Activity activity) {
603 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
604 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
605 | if (fakeStatusBarView != null) {
606 | decorView.removeView(fakeStatusBarView);
607 | ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
608 | rootView.setPadding(0, 0, 0, 0);
609 | }
610 | }
611 |
612 | /**
613 | * 添加半透明矩形条
614 | *
615 | * @param activity 需要设置的 activity
616 | * @param statusBarAlpha 透明值
617 | */
618 | private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
619 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
620 | View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
621 | if (fakeTranslucentView != null) {
622 | if (fakeTranslucentView.getVisibility() == View.GONE) {
623 | fakeTranslucentView.setVisibility(View.VISIBLE);
624 | }
625 | fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
626 | } else {
627 | contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
628 | }
629 | }
630 |
631 | /**
632 | * 生成一个和状态栏大小相同的彩色矩形条
633 | *
634 | * @param activity 需要设置的 activity
635 | * @param color 状态栏颜色值
636 | * @return 状态栏矩形条
637 | */
638 | private static View createStatusBarView(Activity activity, @ColorInt int color) {
639 | return createStatusBarView(activity, color, 0);
640 | }
641 |
642 | /**
643 | * 生成一个和状态栏大小相同的半透明矩形条
644 | *
645 | * @param activity 需要设置的activity
646 | * @param color 状态栏颜色值
647 | * @param alpha 透明值
648 | * @return 状态栏矩形条
649 | */
650 | private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
651 | // 绘制一个和状态栏一样高的矩形
652 | View statusBarView = new View(activity);
653 | LinearLayout.LayoutParams params =
654 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
655 | statusBarView.setLayoutParams(params);
656 | statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
657 | statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
658 | return statusBarView;
659 | }
660 |
661 | /**
662 | * 设置根布局参数
663 | */
664 | private static void setRootView(Activity activity) {
665 | ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
666 | for (int i = 0, count = parent.getChildCount(); i < count; i++) {
667 | View childView = parent.getChildAt(i);
668 | if (childView instanceof ViewGroup) {
669 | childView.setFitsSystemWindows(true);
670 | ((ViewGroup) childView).setClipToPadding(true);
671 | }
672 | }
673 | }
674 |
675 | /**
676 | * 设置透明
677 | */
678 | private static void setTransparentForWindow(Activity activity) {
679 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
680 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
681 | activity.getWindow()
682 | .getDecorView()
683 | .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
684 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
685 | activity.getWindow()
686 | .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
687 | }
688 | }
689 |
690 | /**
691 | * 使状态栏透明
692 | */
693 | @TargetApi(Build.VERSION_CODES.KITKAT)
694 | private static void transparentStatusBar(Activity activity) {
695 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
696 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
697 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
698 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
699 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
700 | } else {
701 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
702 | }
703 | }
704 |
705 | /**
706 | * 创建半透明矩形 View
707 | *
708 | * @param alpha 透明值
709 | * @return 半透明 View
710 | */
711 | private static View createTranslucentStatusBarView(Activity activity, int alpha) {
712 | // 绘制一个和状态栏一样高的矩形
713 | View statusBarView = new View(activity);
714 | LinearLayout.LayoutParams params =
715 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
716 | statusBarView.setLayoutParams(params);
717 | statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
718 | statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
719 | return statusBarView;
720 | }
721 |
722 | /**
723 | * 获取状态栏高度
724 | *
725 | * @param context context
726 | * @return 状态栏高度
727 | */
728 | private static int getStatusBarHeight(Context context) {
729 | // 获得状态栏高度
730 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
731 | return context.getResources().getDimensionPixelSize(resourceId);
732 | }
733 |
734 | /**
735 | * 计算状态栏颜色
736 | *
737 | * @param color color值
738 | * @param alpha alpha值
739 | * @return 最终的状态栏颜色
740 | */
741 | private static int calculateStatusColor(@ColorInt int color, int alpha) {
742 | if (alpha == 0) {
743 | return color;
744 | }
745 | float a = 1 - alpha / 255f;
746 | int red = color >> 16 & 0xff;
747 | int green = color >> 8 & 0xff;
748 | int blue = color & 0xff;
749 | red = (int) (red * a + 0.5);
750 | green = (int) (green * a + 0.5);
751 | blue = (int) (blue * a + 0.5);
752 | return 0xff << 24 | red << 16 | green << 8 | blue;
753 | }
754 | }
755 |
--------------------------------------------------------------------------------
/app/src/main/res-dark/drawable/bg_item_dark.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 | -
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res-dark/mipmap-xxhdpi/img_avatar_dark.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res-dark/mipmap-xxhdpi/img_avatar_dark.png
--------------------------------------------------------------------------------
/app/src/main/res-dark/values/colors_dark.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #00574B
4 | #88D81B60
5 | #888888
6 | #444444
7 |
--------------------------------------------------------------------------------
/app/src/main/res-light/drawable/bg_item_light.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 | -
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res-light/mipmap-xxhdpi/img_avatar_light.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res-light/mipmap-xxhdpi/img_avatar_light.png
--------------------------------------------------------------------------------
/app/src/main/res-light/values/colors_light.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #89BFC7
4 | #10D81B60
5 | #333333
6 | #bbbbbb
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 | -
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
21 |
22 |
30 |
31 |
42 |
43 |
49 |
50 |
58 |
59 |
62 |
63 |
67 |
68 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
21 |
22 |
30 |
31 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/img_avatar.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xxhdpi/img_avatar.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #44D81B60
5 | #eeeeee
6 | #888888
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ChangeSkinHelper
3 | 这是很长的一段文字这是很长的一段文字这是很长的一段文字这是很长的一段文字这是很长的一段文字这是很长的一段文字这是很长的一段文字这是很长的一段文字
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | apply from: "config.gradle"
3 | buildscript {
4 | repositories {
5 | maven { url 'https://jitpack.io' }
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.4.1'
12 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | maven { url 'https://jitpack.io' }
22 | google()
23 | jcenter()
24 |
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
31 |
--------------------------------------------------------------------------------
/changeskinhelper/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/changeskinhelper/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.arvinljw'
5 |
6 | android {
7 | compileSdkVersion 28
8 | defaultConfig {
9 | minSdkVersion 16
10 | targetSdkVersion 28
11 | versionCode 2
12 | versionName "1.0.2"
13 |
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 |
22 | // build a jar with source files
23 | task sourcesJar(type: Jar) {
24 | from android.sourceSets.main.java.srcDirs
25 | classifier = 'sources'
26 | }
27 | task javadoc(type: Javadoc) {
28 | failOnError false
29 | source = android.sourceSets.main.java.sourceFiles
30 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
31 | classpath += configurations.compile
32 | }
33 | // build a jar with javadoc
34 | task javadocJar(type: Jar, dependsOn: javadoc) {
35 | classifier = 'javadoc'
36 | from javadoc.destinationDir
37 | }
38 | artifacts {
39 | archives sourcesJar
40 | archives javadocJar
41 | }
42 | }
43 |
44 | dependencies {
45 | implementation fileTree(dir: 'libs', include: ['*.jar'])
46 | compileOnly 'com.android.support:appcompat-v7:28.0.0'
47 | }
48 |
--------------------------------------------------------------------------------
/changeskinhelper/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/ChangeSkinHelper.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.Activity;
5 | import android.app.Application;
6 | import android.content.Context;
7 | import android.content.res.ColorStateList;
8 | import android.content.res.TypedArray;
9 | import android.graphics.Typeface;
10 | import android.graphics.drawable.ColorDrawable;
11 | import android.graphics.drawable.Drawable;
12 | import android.os.Build;
13 | import android.support.annotation.ColorRes;
14 | import android.support.v4.content.ContextCompat;
15 | import android.support.v7.app.ActionBar;
16 | import android.support.v7.app.AppCompatActivity;
17 | import android.util.AttributeSet;
18 | import android.util.Log;
19 | import android.view.LayoutInflater;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.ImageView;
23 | import android.widget.TextView;
24 |
25 | import net.arvin.changeskinhelper.core.ChangeCustomSkinListener;
26 | import net.arvin.changeskinhelper.core.ChangeSkinListener;
27 | import net.arvin.changeskinhelper.core.SkinResId;
28 | import net.arvin.changeskinhelper.core.SkinResourceProcessor;
29 |
30 | import java.lang.reflect.Constructor;
31 | import java.util.ArrayList;
32 | import java.util.HashMap;
33 | import java.util.List;
34 | import java.util.Map;
35 |
36 | /**
37 | * Created by arvinljw on 2019-08-02 16:18
38 | * Function:
39 | * Desc:
40 | * 2、自定义view的自定义属性
41 | */
42 | public class ChangeSkinHelper {
43 |
44 | public static String KEY_SKIN_PATH = "skin_path";
45 | public static String KEY_SKIN_SUFFIX = "skin_suffix";
46 | private static List changeSkinListeners = new ArrayList<>();
47 |
48 | private static int[] attrIds = {
49 | android.R.attr.background,
50 | android.R.attr.src,
51 | R.attr.csh_textColor,
52 | R.attr.csh_typeface,
53 | android.R.attr.windowBackground,
54 | };
55 |
56 | //todo 反射:这里使用反射创建View
57 | private static final Map> sConstructorMap = new HashMap<>();
58 | private static final Object[] mConstructorArgs = new Object[2];
59 | private static final Class>[] sConstructorSignature = new Class[]{Context.class, AttributeSet.class};
60 |
61 | public static void init(Application application) {
62 | SkinResourceProcessor.init(application);
63 | }
64 |
65 | public static void addListener(ChangeSkinListener listener) {
66 | changeSkinListeners.add(listener);
67 | }
68 |
69 | public static void notifyListener(final String skinPath, final String skinSuffix) {
70 | long startTime = System.currentTimeMillis();
71 | loadSkinResources(skinPath, skinSuffix);
72 | for (ChangeSkinListener listener : changeSkinListeners) {
73 | listener.changeSkin();
74 | }
75 | Log.e("ChangeSkin", "需要时间" + (System.currentTimeMillis() - startTime));
76 | }
77 |
78 | public static void removeListener(ChangeSkinListener listener) {
79 | changeSkinListeners.remove(listener);
80 | }
81 |
82 | public static View onCreateView(AppCompatActivity activity, LayoutInflater inflater, View parent, String name, Context context, AttributeSet attrs) {
83 | View view = activity.getDelegate().createView(parent, name, context, attrs);
84 | if (view == null) {
85 | view = createViewFromTag(context, name, attrs);
86 | }
87 | if (view != null) {
88 | setSkinTag(context, attrs, view);
89 | setSkin(view);
90 | }
91 |
92 | return view;
93 | }
94 |
95 | //不能使用反射调用inflater的onCreateView方法,有的Layout会创建失败
96 | private static View createViewFromTag(Context context, String name, AttributeSet attrs) {
97 | if (name.equals("view")) {
98 | name = attrs.getAttributeValue(null, "class");
99 | }
100 | try {
101 | mConstructorArgs[0] = context;
102 | mConstructorArgs[1] = attrs;
103 | if (-1 == name.indexOf('.')) {
104 | return createView(context, name, "android.widget.");
105 | } else {
106 | return createView(context, name, null);
107 | }
108 | } catch (Exception e) {
109 | return null;
110 | } finally {
111 | mConstructorArgs[0] = null;
112 | mConstructorArgs[1] = null;
113 | }
114 | }
115 |
116 | private static View createView(Context context, String name, String prefix) {
117 | Constructor extends View> constructor = sConstructorMap.get(name);
118 | try {
119 | if (constructor == null) {
120 | Class extends View> clazz = context.getClassLoader().loadClass(
121 | prefix != null ? (prefix + name) : name).asSubclass(View.class);
122 | constructor = clazz.getConstructor(sConstructorSignature);
123 | sConstructorMap.put(name, constructor);
124 | }
125 | constructor.setAccessible(true);
126 | return constructor.newInstance(mConstructorArgs);
127 | } catch (Exception e) {
128 | return null;
129 | }
130 | }
131 |
132 | @SuppressLint("ResourceType")
133 | private static void setSkinTag(Context context, AttributeSet attrs, View view) {
134 | TypedArray typedArray = context.obtainStyledAttributes(attrs, attrIds);
135 | SkinResId skin = new SkinResId();
136 | int defValue = -1;
137 | int background = typedArray.getResourceId(0, defValue);
138 | int windowBackground = typedArray.getResourceId(4, defValue);
139 | if (background == -1) {
140 | background = windowBackground;
141 | }
142 | skin.setBackground(background);
143 | skin.setSrc(typedArray.getResourceId(1, defValue));
144 | int textColorResId = typedArray.getResourceId(2, defValue);
145 | if (textColorResId == -1 && view instanceof TextView) {
146 | textColorResId = ChangeSkinHelper.getTextColorResId(attrs);
147 | }
148 | skin.setTextColor(textColorResId);
149 | skin.setTypeface(typedArray.getResourceId(3, defValue));
150 | typedArray.recycle();
151 | view.setTag(R.id.change_skin_tag, skin);
152 |
153 | if (view instanceof ChangeCustomSkinListener) {
154 | ChangeCustomSkinListener changeCustomSkinListener = (ChangeCustomSkinListener) view;
155 | changeCustomSkinListener.setCustomTag();
156 | }
157 | }
158 |
159 | public static void loadSkinResources(String skinPath, String skinSuffix) {
160 | SkinResourceProcessor.getInstance().loadSkinResources(skinPath, skinSuffix);
161 | }
162 |
163 | public static void applyViews(View view) {
164 | setSkin(view);
165 |
166 | if (view instanceof ViewGroup) {
167 | ViewGroup parent = (ViewGroup) view;
168 | for (int i = 0; i < parent.getChildCount(); i++) {
169 | applyViews(parent.getChildAt(i));
170 | }
171 | }
172 | }
173 |
174 | public static void setSkin(View view) {
175 | if (view == null) {
176 | return;
177 | }
178 | Object tag = view.getTag(R.id.change_skin_tag);
179 | if (tag != null) {
180 | SkinResId skin = (SkinResId) tag;
181 | int background = skin.getBackground();
182 | if (background != -1) {
183 | ChangeSkinHelper.setBackground(view, background);
184 | }
185 |
186 | int textColor = skin.getTextColor();
187 | if (textColor != -1) {
188 | if (view instanceof TextView) {
189 | ChangeSkinHelper.setTextColor((TextView) view, textColor);
190 | }
191 | }
192 | int src = skin.getSrc();
193 | if (src != -1) {
194 | if (view instanceof ImageView) {
195 | ChangeSkinHelper.setSrc((ImageView) view, src);
196 | }
197 | }
198 | int typeface = skin.getTypeface();
199 | if (typeface != -1) {
200 | if (view instanceof TextView) {
201 | ChangeSkinHelper.setTypeface((TextView) view, typeface);
202 | }
203 | }
204 | if (view instanceof ChangeCustomSkinListener) {
205 | ChangeCustomSkinListener changeCustomSkinListener = (ChangeCustomSkinListener) view;
206 | changeCustomSkinListener.changeCustomSkin();
207 | }
208 | }
209 | }
210 |
211 | /**
212 | * @param resId 长度为4,依次为background,src,textColor,typeface
213 | * 例如要设置textColor,background和src不设置则可使用0占位
214 | */
215 | public static void setViewTag(View view, int... resId) {
216 | if (view == null || resId == null || resId.length == 0) {
217 | return;
218 | }
219 | Object temp = view.getTag(R.id.change_skin_tag);
220 | SkinResId tag;
221 | if (temp instanceof SkinResId) {
222 | tag = (SkinResId) temp;
223 | } else {
224 | tag = new SkinResId();
225 | }
226 | tag.setBackground(resId[0]);
227 | if (resId.length > 1) {
228 | tag.setSrc(resId[1]);
229 | }
230 | if (resId.length > 2) {
231 | tag.setTextColor(resId[2]);
232 | }
233 | if (resId.length > 3) {
234 | tag.setTypeface(resId[3]);
235 | }
236 | view.setTag(R.id.change_skin_tag, tag);
237 | }
238 |
239 | public static void setViewBackground(View view, int background) {
240 | if (view == null) {
241 | return;
242 | }
243 | Object tag = view.getTag(R.id.change_skin_tag);
244 | if (tag instanceof SkinResId) {
245 | SkinResId skinResId = (SkinResId) tag;
246 | skinResId.setBackground(background);
247 | view.setTag(skinResId);
248 | }
249 | }
250 |
251 | public static void setViewSrc(ImageView imageView, int src) {
252 | if (imageView == null) {
253 | return;
254 | }
255 | Object tag = imageView.getTag(R.id.change_skin_tag);
256 | if (tag instanceof SkinResId) {
257 | SkinResId skinResId = (SkinResId) tag;
258 | skinResId.setSrc(src);
259 | imageView.setTag(skinResId);
260 | }
261 | }
262 |
263 | public static void setViewTextColor(TextView textView, int textColor) {
264 | if (textView == null) {
265 | return;
266 | }
267 | Object tag = textView.getTag(R.id.change_skin_tag);
268 | if (tag instanceof SkinResId) {
269 | SkinResId skinResId = (SkinResId) tag;
270 | skinResId.setTextColor(textColor);
271 | textView.setTag(skinResId);
272 | }
273 | }
274 |
275 | /**
276 | * @param typeface typeface的资源id,是一个string资源,内容是字体在assets中的路径
277 | */
278 | public static void setViewTypeface(TextView textView, int typeface) {
279 | if (textView == null) {
280 | return;
281 | }
282 | Object tag = textView.getTag(R.id.change_skin_tag);
283 | if (tag instanceof SkinResId) {
284 | SkinResId skinResId = (SkinResId) tag;
285 | skinResId.setTypeface(typeface);
286 | textView.setTag(skinResId);
287 | }
288 | }
289 |
290 | public static void setCustomResId(View view, int customResId) {
291 | Object temp = view.getTag(R.id.change_skin_tag);
292 | SkinResId tag;
293 | if (temp instanceof SkinResId) {
294 | tag = (SkinResId) temp;
295 | } else {
296 | tag = new SkinResId();
297 | }
298 | tag.setCustomResId(customResId);
299 | view.setTag(R.id.change_skin_tag, tag);
300 | }
301 |
302 | public static void setCustomResIds(View view, int... customResIds) {
303 | Object temp = view.getTag(R.id.change_skin_tag);
304 | SkinResId tag;
305 | if (temp instanceof SkinResId) {
306 | tag = (SkinResId) temp;
307 | } else {
308 | tag = new SkinResId();
309 | }
310 | List resIds = new ArrayList<>();
311 | for (int customResId : customResIds) {
312 | resIds.add(customResId);
313 | }
314 | tag.setCustomResIds(resIds);
315 | view.setTag(R.id.change_skin_tag, tag);
316 | }
317 |
318 | public static int getSkinCustomResId(View view) {
319 | if (view == null) {
320 | return 0;
321 | }
322 | Object temp = view.getTag(R.id.change_skin_tag);
323 | if (temp instanceof SkinResId) {
324 | SkinResId tag = (SkinResId) temp;
325 | return tag.getCustomResId();
326 | }
327 | return 0;
328 | }
329 |
330 | public static List getSkinCustomResIds(View view) {
331 | if (view == null) {
332 | return null;
333 | }
334 | Object temp = view.getTag(R.id.change_skin_tag);
335 | if (temp instanceof SkinResId) {
336 | SkinResId tag = (SkinResId) temp;
337 | return tag.getCustomResIds();
338 | }
339 | return null;
340 | }
341 |
342 | public static void setBackground(View view, int backgroundResourceId) {
343 | if (backgroundResourceId > 0) {
344 | // 是否默认皮肤
345 | SkinResourceProcessor skinResProcessor = SkinResourceProcessor.getInstance();
346 | if (skinResProcessor.usingDefaultSkin() && skinResProcessor.usingInnerAppSkin()) {
347 | // 兼容包转换
348 | Drawable drawable = ContextCompat.getDrawable(view.getContext(), backgroundResourceId);
349 | // 控件自带api,这里不用setBackgroundColor()因为在9.0测试不通过
350 | // setBackgroundDrawable本来过时了,但是兼容包重写了方法
351 | view.setBackgroundDrawable(drawable);
352 | } else {
353 | // 获取皮肤包资源
354 | Object skinResourceId = skinResProcessor.getBackgroundOrSrc(backgroundResourceId);
355 | // 兼容包转换
356 | if (skinResourceId instanceof Integer) {
357 | int color = (int) skinResourceId;
358 | view.setBackgroundColor(color);
359 | // setBackgroundResource(color); // 未做兼容测试
360 | } else {
361 | Drawable drawable = (Drawable) skinResourceId;
362 | view.setBackgroundDrawable(drawable);
363 | }
364 | }
365 | }
366 | }
367 |
368 | public static void setSrc(ImageView view, int srcResourceId) {
369 | if (srcResourceId > 0) {
370 | // 是否默认皮肤
371 | SkinResourceProcessor skinResProcessor = SkinResourceProcessor.getInstance();
372 | if (skinResProcessor.usingDefaultSkin() && skinResProcessor.usingInnerAppSkin()) {
373 | // 兼容包转换
374 | view.setImageResource(srcResourceId);
375 | Drawable drawable = ContextCompat.getDrawable(view.getContext(), srcResourceId);
376 | view.setImageDrawable(drawable);
377 | } else {
378 | // 获取皮肤包资源
379 | Object skinResourceId = skinResProcessor.getBackgroundOrSrc(srcResourceId);
380 | // 兼容包转换
381 | if (skinResourceId instanceof Integer) {
382 | int color = (int) skinResourceId;
383 | view.setImageResource(color);
384 | // setImageBitmap(); // Bitmap未添加
385 | } else {
386 | Drawable drawable = (Drawable) skinResourceId;
387 | view.setImageDrawable(drawable);
388 | }
389 | }
390 | }
391 | }
392 |
393 | public static void setTextColor(TextView view, int textColorResourceId) {
394 | if (textColorResourceId > 0) {
395 | SkinResourceProcessor skinResProcessor = SkinResourceProcessor.getInstance();
396 | if (skinResProcessor.usingDefaultSkin() && skinResProcessor.usingInnerAppSkin()) {
397 | ColorStateList color = ContextCompat.getColorStateList(view.getContext(), textColorResourceId);
398 | view.setTextColor(color);
399 | } else {
400 | ColorStateList color = skinResProcessor.getColorStateList(textColorResourceId);
401 | view.setTextColor(color);
402 | }
403 | }
404 | }
405 |
406 | public static void setTypeface(TextView view, int textTypefaceResourceId) {
407 | if (textTypefaceResourceId > 0) {
408 | SkinResourceProcessor skinResProcessor = SkinResourceProcessor.getInstance();
409 | if (skinResProcessor.usingDefaultSkin() && skinResProcessor.usingInnerAppSkin()) {
410 | view.setTypeface(Typeface.DEFAULT);
411 | } else {
412 | view.setTypeface(skinResProcessor.getTypeface(textTypefaceResourceId));
413 | }
414 | }
415 | }
416 |
417 | public static int getTextColorResId(AttributeSet attrs) {
418 | for (int i = 0; i < attrs.getAttributeCount(); i++) {
419 | String attrName = attrs.getAttributeName(i);
420 | String attrValue = attrs.getAttributeValue(i);
421 | if (!attrName.equals("textColor")) {
422 | continue;
423 | }
424 | if (attrValue.startsWith("@")) {
425 | return Integer.parseInt(attrValue.substring(1));
426 | }
427 | }
428 | return -1;
429 | }
430 |
431 | public static int getColor(int resourceId) {
432 | return SkinResourceProcessor.getInstance().getColor(resourceId);
433 | }
434 |
435 | public static ColorStateList getColorStateList(int resourceId) {
436 | return SkinResourceProcessor.getInstance().getColorStateList(resourceId);
437 | }
438 |
439 | public static Drawable getDrawableOrMipMap(int resourceId) {
440 | return SkinResourceProcessor.getInstance().getDrawableOrMipMap(resourceId);
441 | }
442 |
443 | public static String getString(int resourceId) {
444 | return SkinResourceProcessor.getInstance().getString(resourceId);
445 | }
446 |
447 | // 返回值特殊情况:可能是color / drawable / mipmap
448 | public static Object getBackgroundOrSrc(int resourceId) {
449 | return SkinResourceProcessor.getInstance().getBackgroundOrSrc(resourceId);
450 | }
451 |
452 | // 获得字体
453 | public static Typeface getTypeface(int resourceId) {
454 | return SkinResourceProcessor.getInstance().getTypeface(resourceId);
455 | }
456 |
457 | public static void setNavigation(Activity activity, @ColorRes int navigationBarColor) {
458 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
459 | activity.getWindow().setNavigationBarColor(getColor(navigationBarColor));
460 | }
461 | }
462 |
463 | public static void setActionBar(AppCompatActivity activity, @ColorRes int actionBarColor) {
464 | ActionBar actionBar = activity.getSupportActionBar();
465 | if (actionBar != null) {
466 | actionBar.setBackgroundDrawable(new ColorDrawable(getColor(actionBarColor)));
467 | }
468 | }
469 | }
470 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/ChangeCustomSkinListener.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | /**
4 | * Created by arvinljw on 2019-08-10 17:38
5 | * Function:
6 | * Desc:
7 | */
8 | public interface ChangeCustomSkinListener {
9 | void setCustomTag();
10 |
11 | void changeCustomSkin();
12 | }
13 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/ChangeSkinActivity.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.v4.view.LayoutInflaterCompat;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.AttributeSet;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 |
11 | import net.arvin.changeskinhelper.ChangeSkinHelper;
12 |
13 |
14 | /**
15 | * Created by arvinljw on 2019-08-02 16:21
16 | * Function:
17 | * Desc:LICENSE
18 | */
19 | public class ChangeSkinActivity extends AppCompatActivity implements ChangeSkinListener {
20 | private LayoutInflater inflater;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | if (isChangeSkin()) {
25 | ChangeSkinHelper.init(getApplication());
26 | inflater = LayoutInflater.from(this);
27 | LayoutInflaterCompat.setFactory2(inflater, this);
28 | ChangeSkinHelper.addListener(this);
29 | String skinPath = ChangeSkinPreferenceUtil.getString(getApplicationContext(), ChangeSkinHelper.KEY_SKIN_PATH);
30 | String suffix = ChangeSkinPreferenceUtil.getString(getApplicationContext(), ChangeSkinHelper.KEY_SKIN_SUFFIX);
31 | ChangeSkinHelper.loadSkinResources(skinPath, suffix);
32 | }
33 | super.onCreate(savedInstanceState);
34 | }
35 |
36 | @Override
37 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
38 | if (!isChangeSkin()) {
39 | return super.onCreateView(parent, name, context, attrs);
40 | }
41 | return ChangeSkinHelper.onCreateView(this, inflater, parent, name, context, attrs);
42 | }
43 |
44 | /**
45 | * @return 是否需要开启换肤,默认是false,true表示开启
46 | */
47 | protected boolean isChangeSkin() {
48 | return false;
49 | }
50 |
51 | protected void defaultSkin() {
52 | ChangeSkinHelper.notifyListener(null, "");
53 | }
54 |
55 | protected void dynamicSkin(String skinSuffix) {
56 | ChangeSkinHelper.notifyListener(null, skinSuffix);
57 | }
58 |
59 | protected void dynamicSkin(String skinPath, String skinSuffix) {
60 | ChangeSkinHelper.notifyListener(skinPath, skinSuffix);
61 | }
62 |
63 | @Override
64 | public void changeSkin() {
65 | ChangeSkinHelper.applyViews(getWindow().getDecorView());
66 | }
67 |
68 | @Override
69 | protected void onDestroy() {
70 | super.onDestroy();
71 | ChangeSkinHelper.removeListener(this);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/ChangeSkinListener.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | /**
4 | * Created by arvinljw on 2019-08-09 17:26
5 | * Function:
6 | * Desc:
7 | */
8 | public interface ChangeSkinListener {
9 | void changeSkin();
10 | }
11 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/ChangeSkinPreferenceUtil.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import java.util.Map;
7 |
8 | /**
9 | * Created by arvinljw on 2019-08-09 17:44
10 | * Function:
11 | * Desc:
12 | */
13 | public class ChangeSkinPreferenceUtil {
14 | public static String PREFERENCE_NAME = "net.arvin.changeskinhelper.core";
15 |
16 | public static boolean putString(Context context, String key, String value) {
17 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
18 | SharedPreferences.Editor editor = settings.edit();
19 | editor.putString(key, value);
20 | return editor.commit();
21 | }
22 |
23 | public static String getString(Context context, String key) {
24 | return getString(context, key, "");
25 | }
26 |
27 | public static String getString(Context context, String key, String defaultValue) {
28 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
29 | return settings.getString(key, defaultValue);
30 | }
31 |
32 | public static boolean putInt(Context context, String key, int value) {
33 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
34 | SharedPreferences.Editor editor = settings.edit();
35 | editor.putInt(key, value);
36 | return editor.commit();
37 | }
38 |
39 | public static int getInt(Context context, String key) {
40 | return getInt(context, key, -1);
41 | }
42 |
43 | public static int getInt(Context context, String key, int defaultValue) {
44 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
45 | return settings.getInt(key, defaultValue);
46 | }
47 |
48 | public static boolean putLong(Context context, String key, long value) {
49 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
50 | SharedPreferences.Editor editor = settings.edit();
51 | editor.putLong(key, value);
52 | return editor.commit();
53 | }
54 |
55 | public static long getLong(Context context, String key) {
56 | return getLong(context, key, -1);
57 | }
58 |
59 | public static long getLong(Context context, String key, long defaultValue) {
60 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
61 | return settings.getLong(key, defaultValue);
62 | }
63 |
64 | public static boolean putFloat(Context context, String key, float value) {
65 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
66 | SharedPreferences.Editor editor = settings.edit();
67 | editor.putFloat(key, value);
68 | return editor.commit();
69 | }
70 |
71 | public static float getFloat(Context context, String key) {
72 | return getFloat(context, key, -1);
73 | }
74 |
75 | public static float getFloat(Context context, String key, float defaultValue) {
76 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
77 | return settings.getFloat(key, defaultValue);
78 | }
79 |
80 | public static boolean putBoolean(Context context, String key, boolean value) {
81 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
82 | SharedPreferences.Editor editor = settings.edit();
83 | editor.putBoolean(key, value);
84 | return editor.commit();
85 | }
86 |
87 | public static boolean getBoolean(Context context, String key) {
88 | return getBoolean(context, key, false);
89 | }
90 |
91 | public static boolean getBoolean(Context context, String key, boolean defaultValue) {
92 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
93 | return settings.getBoolean(key, defaultValue);
94 | }
95 |
96 | public static Map getAll(Context context) {
97 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
98 | return settings.getAll();
99 | }
100 |
101 | public static boolean contains(Context context, String key) {
102 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
103 | return settings.contains(key);
104 | }
105 |
106 | public static boolean removeSomething(Context context, String... keys) {
107 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
108 | SharedPreferences.Editor editor = settings.edit();
109 | if (keys == null)
110 | return false;
111 | for (String k : keys) {
112 | editor.remove(k);
113 | }
114 | editor.commit();
115 | return true;
116 | }
117 |
118 | public static boolean clearSP(Context context) {
119 | SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
120 | SharedPreferences.Editor editor = settings.edit();
121 | editor.clear();
122 | editor.commit();
123 | return true;
124 | }
125 |
126 | public static void put(Context context, String key, Object object) {
127 | if (object instanceof String) {
128 | putString(context, key, (String) object);
129 | } else if (object instanceof Integer) {
130 | putInt(context, key, (Integer) object);
131 | } else if (object instanceof Boolean) {
132 | putBoolean(context, key, (Boolean) object);
133 | } else if (object instanceof Float) {
134 | putFloat(context, key, (Float) object);
135 | } else if (object instanceof Long) {
136 | putLong(context, key, (Long) object);
137 | } else {
138 | putString(context, key, object.toString());
139 | }
140 | }
141 |
142 | public static Object get(Context context, String key, Object defaultObject) {
143 | if (defaultObject instanceof String) {
144 | return getString(context, key, (String) defaultObject);
145 | } else if (defaultObject instanceof Integer) {
146 | return getInt(context, key, (Integer) defaultObject);
147 | } else if (defaultObject instanceof Boolean) {
148 | return getBoolean(context, key, (Boolean) defaultObject);
149 | } else if (defaultObject instanceof Float) {
150 | return getFloat(context, key, (Float) defaultObject);
151 | } else if (defaultObject instanceof Long) {
152 | return getLong(context, key, (Long) defaultObject);
153 | }
154 | return null;
155 | }
156 | }
157 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/SkinCache.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | import android.content.res.Resources;
4 |
5 | /**
6 | * Created by arvinljw on 2019-07-17 16:22
7 | * Function:
8 | * Desc:
9 | */
10 | public class SkinCache {
11 | private Resources skinResource;
12 | private String skinPackageName;
13 | private String skinSuffix;
14 |
15 | public SkinCache(Resources skinResource, String skinPackageName, String skinSuffix) {
16 | this.skinResource = skinResource;
17 | this.skinPackageName = skinPackageName;
18 | this.skinSuffix = skinSuffix;
19 | }
20 |
21 | public Resources getSkinResource() {
22 | return skinResource;
23 | }
24 |
25 | public String getSkinPackageName() {
26 | return skinPackageName;
27 | }
28 |
29 | public String getSkinSuffix() {
30 | return skinSuffix;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/SkinResId.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by arvinljw on 2019-08-02 16:39
7 | * Function:
8 | * Desc:
9 | */
10 | public class SkinResId {
11 | private int background;
12 | private int src;
13 | private int textColor;
14 | private int typeface;
15 |
16 | private int customResId;
17 | //不止一个时可以使用customResIds来存储
18 | private List customResIds;
19 |
20 | public int getBackground() {
21 | return background;
22 | }
23 |
24 | public void setBackground(int background) {
25 | this.background = background;
26 | }
27 |
28 | public int getSrc() {
29 | return src;
30 | }
31 |
32 | public void setSrc(int src) {
33 | this.src = src;
34 | }
35 |
36 | public int getTextColor() {
37 | return textColor;
38 | }
39 |
40 | public void setTextColor(int textColor) {
41 | this.textColor = textColor;
42 | }
43 |
44 | public int getTypeface() {
45 | return typeface;
46 | }
47 |
48 | public void setTypeface(int typeface) {
49 | this.typeface = typeface;
50 | }
51 |
52 | public int getCustomResId() {
53 | return customResId;
54 | }
55 |
56 | public void setCustomResId(int customResId) {
57 | this.customResId = customResId;
58 | }
59 |
60 | public List getCustomResIds() {
61 | return customResIds;
62 | }
63 |
64 | public void setCustomResIds(List customResIds) {
65 | this.customResIds = customResIds;
66 | }
67 |
68 | @Override
69 | public String toString() {
70 | return "SkinResId{" +
71 | "background=" + background +
72 | ", src=" + src +
73 | ", textColor=" + textColor +
74 | ", typeface=" + typeface +
75 | ", customResId=" + customResId +
76 | ", customResIds=" + customResIds +
77 | '}';
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/java/net/arvin/changeskinhelper/core/SkinResourceProcessor.java:
--------------------------------------------------------------------------------
1 | package net.arvin.changeskinhelper.core;
2 |
3 | import android.app.Application;
4 | import android.content.pm.PackageInfo;
5 | import android.content.pm.PackageManager;
6 | import android.content.res.AssetManager;
7 | import android.content.res.ColorStateList;
8 | import android.content.res.Resources;
9 | import android.graphics.Typeface;
10 | import android.graphics.drawable.Drawable;
11 | import android.text.TextUtils;
12 | import android.util.Log;
13 |
14 | import net.arvin.changeskinhelper.ChangeSkinHelper;
15 |
16 | import java.lang.reflect.Method;
17 | import java.util.HashMap;
18 | import java.util.Map;
19 |
20 | /**
21 | * Created by arvinljw on 2019-08-09 14:16
22 | * Function:
23 | * Desc:
24 | */
25 | public class SkinResourceProcessor {
26 | private static final String TAG = SkinResourceProcessor.class.getSimpleName();
27 | //todo 反射:这里用的反射加载外部资源
28 | private static final String METHOD_ADD_ASSETS_PATH = "addAssetPath";
29 | private static SkinResourceProcessor instance;
30 | private static Method addAssetsPath;
31 | private Application application;
32 | private Resources appResources;
33 | private Resources skinResources;
34 | private String skinPackageName;
35 | private String skinPath;
36 | /*是否使用默认资源或app内资源,根据是否包含skinSuffix确定是否跟换资源*/
37 | private boolean usingDefaultSkin = true;
38 | private String skinSuffix;
39 | private Map skinCacheMap;
40 |
41 | private SkinResourceProcessor(Application application) {
42 | this.application = application;
43 | appResources = application.getResources();
44 | skinCacheMap = new HashMap<>();
45 | }
46 |
47 | /**
48 | * 单例方法,目的是初始化app内置资源(越早越好,用户的操作可能是:换肤后的第2次冷启动)
49 | */
50 | public static void init(Application application) {
51 | if (instance == null) {
52 | synchronized (ChangeSkinHelper.class) {
53 | if (instance == null) {
54 | instance = new SkinResourceProcessor(application);
55 | }
56 | }
57 | }
58 | }
59 |
60 | public static SkinResourceProcessor getInstance() {
61 | return instance;
62 | }
63 |
64 | public boolean usingDefaultSkin() {
65 | return usingDefaultSkin;
66 | }
67 |
68 | public boolean usingInnerAppSkin() {
69 | return TextUtils.isEmpty(skinSuffix);
70 | }
71 |
72 | public void loadSkinResources(String skinPath, String skinSuffix) {
73 | ChangeSkinPreferenceUtil.putString(application, ChangeSkinHelper.KEY_SKIN_PATH, skinPath);
74 | ChangeSkinPreferenceUtil.putString(application, ChangeSkinHelper.KEY_SKIN_SUFFIX, skinSuffix);
75 | this.skinSuffix = skinSuffix;
76 | this.skinPath = skinPath;
77 | if (TextUtils.isEmpty(skinPath)) {
78 | usingDefaultSkin = true;
79 | return;
80 | }
81 | if (skinCacheMap.containsKey(skinPath)) {
82 | usingDefaultSkin = false;
83 | SkinCache skinCache = skinCacheMap.get(skinPath);
84 | if (skinCache == null) {
85 | usingDefaultSkin = true;
86 | return;
87 | }
88 | skinResources = skinCache.getSkinResource();
89 | skinPackageName = skinCache.getSkinPackageName();
90 | return;
91 | }
92 | try {
93 | AssetManager assetManager = AssetManager.class.newInstance();
94 | if (addAssetsPath == null) {
95 | addAssetsPath = assetManager.getClass().getDeclaredMethod(METHOD_ADD_ASSETS_PATH, String.class);
96 | addAssetsPath.setAccessible(true);
97 | }
98 | addAssetsPath.invoke(assetManager, skinPath);
99 |
100 | skinResources = new Resources(assetManager, appResources.getDisplayMetrics(), appResources.getConfiguration());
101 | PackageInfo packageInfo = application.getPackageManager()
102 | .getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES);
103 | skinPackageName = packageInfo.packageName;
104 | usingDefaultSkin = TextUtils.isEmpty(skinPackageName);
105 |
106 | if (!usingDefaultSkin) {
107 | skinCacheMap.put(skinPath, new SkinCache(skinResources, skinPackageName, skinSuffix));
108 | }
109 | } catch (Exception e) {
110 | e.printStackTrace();
111 | usingDefaultSkin = true;
112 | }
113 | }
114 |
115 | public int getSkinResourceId(int resourceId) {
116 | String resourceName = appResources.getResourceEntryName(resourceId);
117 | String resourceType = appResources.getResourceTypeName(resourceId);
118 | String skinResourceName = resourceName + skinSuffix;
119 | if (TextUtils.isEmpty(skinPath)) {
120 | if (!TextUtils.isEmpty(skinSuffix)) {
121 | int skinResourceId = appResources.getIdentifier(skinResourceName, resourceType, application.getPackageName());
122 | return skinResourceId == 0 ? resourceId : skinResourceId;
123 | }
124 | return resourceId;
125 | }
126 |
127 | int skinResourceId = skinResources.getIdentifier(skinResourceName, resourceType, skinPackageName);
128 | usingDefaultSkin = skinResourceId == 0;
129 | if (usingDefaultSkin) {
130 | Log.i(TAG, "皮肤包中资源:" + skinResourceName + " 未不到,将使用默认资源:" + resourceName);
131 | }
132 | return usingDefaultSkin ? resourceId : skinResourceId;
133 | }
134 |
135 | public int getColor(int resourceId) {
136 | int resId = getSkinResourceId(resourceId);
137 | return usingDefaultSkin ? appResources.getColor(resId) : skinResources.getColor(resId);
138 | }
139 |
140 | public ColorStateList getColorStateList(int resourceId) {
141 | int resId = getSkinResourceId(resourceId);
142 | return usingDefaultSkin ? appResources.getColorStateList(resId) : skinResources.getColorStateList(resId);
143 | }
144 |
145 | public Drawable getDrawableOrMipMap(int resourceId) {
146 | int resId = getSkinResourceId(resourceId);
147 | return usingDefaultSkin ? appResources.getDrawable(resId) : skinResources.getDrawable(resId);
148 | }
149 |
150 | public String getString(int resourceId) {
151 | int resId = getSkinResourceId(resourceId);
152 | return usingDefaultSkin ? appResources.getString(resId) : skinResources.getString(resId);
153 | }
154 |
155 | // 返回值特殊情况:可能是color / drawable / mipmap
156 | public Object getBackgroundOrSrc(int resourceId) {
157 | // 需要获取当前属性的类型名Resources.getResourceTypeName(resourceId)再判断
158 | String resourceType = appResources.getResourceTypeName(resourceId);
159 | switch (resourceType) {
160 | case "color":
161 | return getColor(resourceId);
162 | case "mipmap": // drawable / mipmap
163 | case "drawable":
164 | return getDrawableOrMipMap(resourceId);
165 | }
166 | return null;
167 | }
168 |
169 | // 获得字体
170 | public Typeface getTypeface(int resourceId) {
171 | // 通过资源ID获取资源path,参考:resources.arsc资源映射表
172 | String skinTypefacePath = getString(resourceId);
173 | // 路径为空,使用系统默认字体
174 | if (TextUtils.isEmpty(skinTypefacePath)) {
175 | return Typeface.DEFAULT;
176 | }
177 | return usingDefaultSkin ? Typeface.createFromAsset(appResources.getAssets(), skinTypefacePath)
178 | : Typeface.createFromAsset(skinResources.getAssets(), skinTypefacePath);
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/changeskinhelper/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ChangeSkinHelper
3 |
4 |
--------------------------------------------------------------------------------
/config.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 |
3 | androidId = [
4 | compileSdkVersion: 28,
5 | minSdkVersion : 16,
6 | targetVersion : 28,
7 | versionCode : 1,
8 | versionName : "1.0"
9 | ]
10 |
11 | appId = [
12 | "app": "net.arvin.changeskinhelper.sample",
13 | ]
14 |
15 | supportVersion = "28.0.0"
16 |
17 | thirdLibrary = [
18 | "appcompat" : "com.android.support:appcompat-v7:$supportVersion",
19 | "recyclerview" : "com.android.support:recyclerview-v7:$supportVersion",
20 | "constraint" : "com.android.support.constraint:constraint-layout:1.1.3",
21 | "permissionHelper": "com.github.arvinljw:PermissionHelper:v1.0.1"
22 | ]
23 | }
--------------------------------------------------------------------------------
/doc/RecyclerView换肤.md:
--------------------------------------------------------------------------------
1 | ## RecyclerView换肤
2 |
3 | 在readme中提到过RecyclerView换肤,因为RecyclerView的item的创建多半也是使用inflate创建的,所以其实它的item都是包含了可换肤的tag的,但是因为item复用原理,导致在执行换肤的时候只能给当前屏幕的itemView换肤,滑动之后上下的几个item因为从RecyclerView的复用池里获取的就没有换肤,还是之前的,就有点问题。
4 |
5 | 知道了问题,就可以解决了,方法也比较简单:
6 |
7 | * 1、在`onCreateViewHolder`,item创建之后获取到可换肤view之后使用,使用`ChangeSkinHelper.setSkin`;
8 | * 2、在`onBindViewHolder`拿到需要换肤的view,也设置一下`ChangeSkinHelper.setSkin`;
9 | * 3、最后在使用`ChangeSkinHelper.addListener`监听一下换肤回调,使用RecyclerView的adapter的`adapter.notifyDataSetChanged()`方法就解决了RecyclerView的换肤。
10 |
11 | 其实ListView等包含复用item的view,都是可以使用这种方式解决换肤的。具体的使用可以参考demo中的RecyclerView的使用(在app下MainFragment中)。
--------------------------------------------------------------------------------
/doc/手动创建View换肤.md:
--------------------------------------------------------------------------------
1 | ## 手动创建View换肤
2 |
3 | 这里的手动创建的View是指不通过inflate布局文件创建,例如直接new一个View,然后加到某个view里显示。
4 |
5 | 这时候就需要手动给该View设置一个换肤的tag,执行换肤时知道该View也是需要换肤的,例如:
6 |
7 | ```
8 | TextView textView = new TextView(this);
9 | textView.setText("我是手动创建的view");
10 | textView.setTextColor(ChangeSkinHelper.getColor(R.color.colorAccent));
11 | textView.setTypeface(ChangeSkinHelper.getTypeface(R.string.custom_typeface));
12 | ChangeSkinHelper.setViewTag(textView, -1, -1, R.color.colorAccent, R.string.custom_typeface);
13 | ```
14 |
15 | 这里需要注意的是,可以换肤的包括textColor和字体,所以在设置的时候需要调用`ChangeSkinHelper.getColor`和`ChangeSkinHelper.getTypeface`方法传入文字颜色和字体资源,返回对应的文字颜色和字体,因为可能这时候使用的不是默认皮肤,所以需要动态获取,而不能直接使用`getResources().getColor`之类的来获取,因为之前设置的皮肤有可能是插件包里的皮肤。
16 |
17 | 这里目前支持的获取皮肤资源的方法包含:
18 |
19 |
20 | * public static int getColor(int resourceId) 获取color颜色资源
21 |
22 | * public static ColorStateList getColorStateList(int resourceId) 获取ColorStateList颜色资源
23 |
24 | * public static Drawable getDrawableOrMipMap(int resourceId) 获取drawable或者是mipmap资源,可以是xml写的drawable或者是图片资源
25 |
26 | * public static String getString(int resourceId) 获取文字资源(主要用于字体)
27 |
28 | * public static Object getBackgroundOrSrc(int resourceId) 获取背景资源,有可能是drawable有可能是color
29 |
30 | * public static Typeface getTypeface(int resourceId) 获取字体资源
31 |
32 | 具体如何可参考demo中的例子(在app下的MainActivity的onCreate方法中)。
33 |
--------------------------------------------------------------------------------
/doc/自定义View换肤.md:
--------------------------------------------------------------------------------
1 | ## 自定义View换肤
2 |
3 | 自定义View的换肤,其实自定义View中支持的属性是可以换的,例如背景颜色,文字颜色,src,字体等,这里主要是解决自定义属性的换肤。
4 |
5 | 自定义View换肤需要:
6 |
7 | * 实现`ChangeCustomSkinListener`接口,实现`setCustomTag`和`changeCustomSkin`方法
8 | * `setCustomTag`方法就是将需要换肤的自定义属性资源放入到tag中,例如只有一个自定义属性需要设置,可以使用`ChangeSkinHelper.setCustomResId`方法,如果有多个自定义属性则使用`ChangeSkinHelper.setCustomResIds`方法,传入的就是自定义属性资源id即可。
9 | * `changeCustomSkin`方法就是换肤时的回调监听,这时候需要从设置在的tag中自定义属性资源获取出来,然后调用`ChangeSkinHelper`中获取对应皮肤资源方法,并设置给view即可。
10 |
11 | 获取对应皮肤资源的方法包括:
12 |
13 | * public static int getColor(int resourceId) 获取color颜色资源
14 |
15 | * public static ColorStateList getColorStateList(int resourceId) 获取ColorStateList颜色资源
16 |
17 | * public static Drawable getDrawableOrMipMap(int resourceId) 获取drawable或者是mipmap资源,可以是xml写的drawable或者是图片资源
18 |
19 | * public static String getString(int resourceId) 获取文字资源(主要用于字体)
20 |
21 | * public static Object getBackgroundOrSrc(int resourceId) 获取背景资源,有可能是drawable有可能是color
22 |
23 | * public static Typeface getTypeface(int resourceId) 获取字体资源
24 |
25 | 这样就基本实现了自定义View的自定义属性换肤,当然具体的内容还是查看demo中的CustomView实现即可。
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Aug 02 16:05:10 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':changeskinhelper', ':skinpackage'
2 |
--------------------------------------------------------------------------------
/skinpackage/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/skinpackage/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | def androidId = rootProject.ext.androidId
4 |
5 | android {
6 | compileSdkVersion androidId.compileSdkVersion
7 | defaultConfig {
8 | applicationId "net.arvin.changeskinhelper.skinpackage"
9 | minSdkVersion androidId.minSdkVersion
10 | targetSdkVersion androidId.targetSdkVersion
11 | versionCode androidId.versionCode
12 | versionName androidId.versionName
13 |
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(dir: 'libs', include: ['*.jar'])
25 | }
26 |
--------------------------------------------------------------------------------
/skinpackage/debug/output.json:
--------------------------------------------------------------------------------
1 | [{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"skinpackage-debug.apk","fullName":"debug","baseName":"debug"},"path":"skinpackage-debug.apk","properties":{}}]
--------------------------------------------------------------------------------
/skinpackage/debug/skinpackage-debug.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/skinpackage/debug/skinpackage-debug.apk
--------------------------------------------------------------------------------
/skinpackage/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/skinpackage/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/skinpackage/src/main/assets/font/yizhiqingshu.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/skinpackage/src/main/assets/font/yizhiqingshu.ttf
--------------------------------------------------------------------------------
/skinpackage/src/main/res/drawable/bg_item_blue.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 | -
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/skinpackage/src/main/res/mipmap-xxhdpi/img_avatar_blue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arvinljw/ChangeSkinHelper/c18bc537af5b6b65c9c79dcad843ebf4cd24a1c3/skinpackage/src/main/res/mipmap-xxhdpi/img_avatar_blue.png
--------------------------------------------------------------------------------
/skinpackage/src/main/res/values/colors_blue.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #1AA6FF
4 | #6F2C8A
5 | #ffffff
6 | #FDAB00
7 |
8 |
--------------------------------------------------------------------------------
/skinpackage/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SkinPackage
3 | font/yizhiqingshu.ttf
4 |
5 |
--------------------------------------------------------------------------------