Skip to content

Commit

Permalink
Support Android studio preview
Browse files Browse the repository at this point in the history
  • Loading branch information
RuffianZhong committed Feb 21, 2023
1 parent c6fe684 commit 85f19f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
33 changes: 21 additions & 12 deletions library/src/main/java/com/ruffian/library/widget/RImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,7 +41,6 @@ public class RImageView extends AppCompatImageView {
//是否圆形
private boolean mIsCircle = false;


private Drawable mDrawable;
private ScaleType mScaleType;
private int mResource;
Expand All @@ -52,16 +53,18 @@ public RImageView(Context context) {

public RImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttributeSet(attrs);
initAttributeSet(context, 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);
Expand All @@ -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();
Expand Down Expand Up @@ -128,19 +138,19 @@ 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);
this.mTintMode = tintMode;
setColorFilter();
}


public void setColorFilter() {
if (mColorFilter != null && mDrawable != null) {
//ColorFilter filter = new PorterDuffColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.SRC_ATOP);
Expand Down Expand Up @@ -187,7 +197,6 @@ private void updateAttrs(Drawable drawable, ScaleType scaleType) {
}
}


/**
* 绘制空Bitmap
*/
Expand Down

0 comments on commit 85f19f5

Please sign in to comment.