|
17 | 17 | package me.zhanghai.android.fastscroll; |
18 | 18 |
|
19 | 19 | import android.content.Context; |
| 20 | +import android.content.res.ColorStateList; |
20 | 21 | import android.content.res.TypedArray; |
21 | | -import android.util.DisplayMetrics; |
22 | | -import android.util.TypedValue; |
23 | 22 |
|
24 | 23 | import androidx.annotation.AttrRes; |
25 | 24 | import androidx.annotation.ColorInt; |
26 | 25 | import androidx.annotation.NonNull; |
| 26 | +import androidx.annotation.Nullable; |
27 | 27 | import androidx.appcompat.content.res.AppCompatResources; |
28 | 28 |
|
29 | 29 | class Utils { |
30 | 30 |
|
31 | 31 | @ColorInt |
32 | 32 | public static int getColorFromAttrRes(@AttrRes int attrRes, @NonNull Context context) { |
| 33 | + ColorStateList colorStateList = getColorStateListFromAttrRes(attrRes, context); |
| 34 | + return colorStateList != null ? colorStateList.getDefaultColor() : 0; |
| 35 | + } |
| 36 | + |
| 37 | + @Nullable |
| 38 | + public static ColorStateList getColorStateListFromAttrRes(@AttrRes int attrRes, |
| 39 | + @NonNull Context context) { |
33 | 40 | TypedArray a = context.obtainStyledAttributes(new int[] { attrRes }); |
34 | | - int type = a.getType(0); |
35 | | - if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) { |
36 | | - return a.getColor(0, 0); |
37 | | - } |
38 | 41 | int resId; |
39 | 42 | try { |
40 | 43 | resId = a.getResourceId(0, 0); |
| 44 | + if (resId != 0) { |
| 45 | + return AppCompatResources.getColorStateList(context, resId); |
| 46 | + } |
| 47 | + return a.getColorStateList(0); |
41 | 48 | } finally { |
42 | 49 | a.recycle(); |
43 | 50 | } |
44 | | - if (resId == 0) { |
45 | | - return 0; |
46 | | - } |
47 | | - return AppCompatResources.getColorStateList(context, resId).getDefaultColor(); |
48 | 51 | } |
49 | 52 | } |
0 commit comments