|
| 1 | +package com.rae.cnblogs.widget; |
| 2 | + |
| 3 | + |
| 4 | +import android.content.Context; |
| 5 | +import android.support.annotation.DrawableRes; |
| 6 | +import android.util.AttributeSet; |
| 7 | +import android.widget.ImageView; |
| 8 | + |
| 9 | +import com.rae.cnblogs.ThemeCompat; |
| 10 | + |
| 11 | +import skin.support.widget.SkinCompatBackgroundHelper; |
| 12 | +import skin.support.widget.SkinCompatImageHelper; |
| 13 | +import skin.support.widget.SkinCompatSupportable; |
| 14 | + |
| 15 | +/** |
| 16 | + * 兼容4.4.4 问题 |
| 17 | + */ |
| 18 | +public class RaeSkinImageViewV4 extends ImageView implements SkinCompatSupportable { |
| 19 | + |
| 20 | + private SkinCompatBackgroundHelper mBackgroundTintHelper; |
| 21 | + private SkinCompatImageHelper mImageHelper; |
| 22 | + |
| 23 | + public RaeSkinImageViewV4(Context context) { |
| 24 | + this(context, null); |
| 25 | + init(); |
| 26 | + } |
| 27 | + |
| 28 | + public RaeSkinImageViewV4(Context context, AttributeSet attrs) { |
| 29 | + this(context, attrs, 0); |
| 30 | + init(); |
| 31 | + } |
| 32 | + |
| 33 | + public RaeSkinImageViewV4(Context context, AttributeSet attrs, int defStyleAttr) { |
| 34 | + super(context, attrs, defStyleAttr); |
| 35 | + this.mBackgroundTintHelper = new SkinCompatBackgroundHelper(this); |
| 36 | + this.mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); |
| 37 | + this.mImageHelper = new SkinCompatImageHelper(this); |
| 38 | + this.mImageHelper.loadFromAttributes(attrs, defStyleAttr); |
| 39 | + init(); |
| 40 | + } |
| 41 | + |
| 42 | + private void init() { |
| 43 | + // 初始化的时候不用取反 |
| 44 | + setAlpha(ThemeCompat.isNight() ? 0.3f : 1f); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void applySkin() { |
| 49 | + setAlpha(isNight() ? 0.3f : 1f); |
| 50 | + if (this.mBackgroundTintHelper != null) { |
| 51 | + this.mBackgroundTintHelper.applySkin(); |
| 52 | + } |
| 53 | + |
| 54 | + if (this.mImageHelper != null) { |
| 55 | + this.mImageHelper.applySkin(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public boolean isNight() { |
| 60 | + // 因为是先应用主题之后才会设置主题名称,所以这里取反。 |
| 61 | + return !ThemeCompat.isNight(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public void setBackgroundResource(@DrawableRes int resId) { |
| 66 | + super.setBackgroundResource(resId); |
| 67 | + if (this.mBackgroundTintHelper != null) { |
| 68 | + this.mBackgroundTintHelper.onSetBackgroundResource(resId); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + public void setImageResource(@DrawableRes int resId) { |
| 74 | + if (this.mImageHelper != null) { |
| 75 | + this.mImageHelper.setImageResource(resId); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments