diff --git a/library/build.gradle b/library/build.gradle index 9ab1345..ea6b666 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 30 + compileSdkVersion 31 defaultConfig { minSdkVersion 14 - targetSdkVersion 30 - versionCode 36 - versionName "support.v0.0.12" + targetSdkVersion 31 + versionCode 37 + versionName "support.v0.0.13" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/library/src/main/java/com/ruffian/library/widget/RImageView.java b/library/src/main/java/com/ruffian/library/widget/RImageView.java index a7170c4..a4f32ea 100644 --- a/library/src/main/java/com/ruffian/library/widget/RImageView.java +++ b/library/src/main/java/com/ruffian/library/widget/RImageView.java @@ -12,11 +12,13 @@ import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; +import android.os.Build; +import android.util.AttributeSet; + import android.support.annotation.ColorInt; import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatImageView; -import android.util.AttributeSet; import com.ruffian.library.widget.rounded.RoundDrawable; @@ -39,7 +41,6 @@ public class RImageView extends AppCompatImageView { //是否圆形 private boolean mIsCircle = false; - private Drawable mDrawable; private ScaleType mScaleType; private int mResource; @@ -52,7 +53,7 @@ public RImageView(Context context) { public RImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); - initAttributeSet(attrs); + initAttributeSet(context, attrs); } /** @@ -60,8 +61,10 @@ public RImageView(Context context, @Nullable AttributeSet attrs) { * * @param attrs */ - private void initAttributeSet(AttributeSet attrs) { - TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RImageView); + private void initAttributeSet(Context context, AttributeSet attrs) { + if (context == null || attrs == null) return; + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RImageView); mIsCircle = a.getBoolean(R.styleable.RImageView_is_circle, false); mCorner = a.getDimensionPixelSize(R.styleable.RImageView_corner_radius, -1); mCornerTopLeft = a.getDimensionPixelSize(R.styleable.RImageView_corner_radius_top_left, 0); @@ -70,19 +73,26 @@ private void initAttributeSet(AttributeSet attrs) { mCornerBottomRight = a.getDimensionPixelSize(R.styleable.RImageView_corner_radius_bottom_right, 0); mBorderWidth = a.getDimensionPixelSize(R.styleable.RImageView_border_width, 0); mBorderColor = a.getColor(R.styleable.RImageView_border_color, Color.BLACK); + //get system attrs String namespace = "http://schemas.android.com/apk/res/android";//android的命名空间 int tintColor = attrs.getAttributeResourceValue(namespace, "tint", 0); - if (tintColor != 0) - mColorFilter = new PorterDuffColorFilter(getResources().getColor(tintColor), mTintMode); + int tintMode = attrs.getAttributeIntValue(namespace, "tintMode", 0); if (tintMode != 0) mTintMode = wrapTintMode(tintMode); + //android studio 预览:需要区分版本或者使用 isInEditMode() 判断区分 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setImageTintList(getImageTintList()); + } else { + if (tintColor != 0) { + mColorFilter = new PorterDuffColorFilter(getResources().getColor(tintColor), mTintMode); + } + } a.recycle(); updateDrawableAttrs(); } - @Override protected void drawableStateChanged() { super.drawableStateChanged(); @@ -128,11 +138,12 @@ public void setImageResource(@DrawableRes int resId) { @Override public void setImageTintList(@Nullable ColorStateList tint) { super.setImageTintList(tint); - this.mColorFilter = new PorterDuffColorFilter(tint.getDefaultColor(), mTintMode); + if (tint != null) { + this.mColorFilter = new PorterDuffColorFilter(tint.getDefaultColor(), mTintMode); + } setColorFilter(); } - @Override public void setImageTintMode(@Nullable PorterDuff.Mode tintMode) { super.setImageTintMode(tintMode); @@ -140,7 +151,6 @@ public void setImageTintMode(@Nullable PorterDuff.Mode tintMode) { setColorFilter(); } - public void setColorFilter() { if (mColorFilter != null && mDrawable != null) { //ColorFilter filter = new PorterDuffColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.SRC_ATOP); @@ -187,7 +197,6 @@ private void updateAttrs(Drawable drawable, ScaleType scaleType) { } } - /** * 绘制空Bitmap */