圆角的实现方式
在Android项目中经常遇见圆角的图片的展示问题,但是很可惜,Android中的imageView并不是原生就支持圆角的,那么在Android中实现展示圆角图片的方式有几种呢?这里笔者大致总结一下分为以下几种
自定义ImageView实现圆角
通过Glide的BitmapTransformation实现圆角
通过fresco的SimpleDraweeView实现
通过外层嵌套一层CardView实现
等等…
以上,其中2/3是借助其他图片加载库实现,1是需要自行实现ImageView中圆角的绘制函数,4需要布局多一层嵌套。
按照最简单的做法这里推荐使用CardView的方式实现圆角。
但是有时候UI设计师的设计图总是不是很如开发的口味,会出现上边需要圆角,底部无圆角的设计需求,那么这是时候,直接使用自定义ImageView造一个轮子方能一劳永逸。
下面贴代码:
public class RadiusImageView extends AppCompatImageView {
private static final int DEFAULT_BORDER_COLOR = Color.GRAY;
private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
private static final int COLOR_DRAWABLE_DIMEN = 2;
private boolean mIsSelected = false;
private boolean mIsOval = false;
private boolean mIsCircle = false;
private int mBorderWidth;
private int mBorderColor;
private int mSelectedBorderWidth;
private int mSelectedBorderColor;
private int mSelectedMaskColor;
private boolean mIsTouchSelectModeEnabled = true;
private int mCornerRadius;
private boolean roundTopLeft = true,roundTopRight= true,
roundBottomRight= true,roundBottomLeft= true;
private Paint mBitmapPaint;
private Paint mBorderPaint;
private ColorFilter mColorFilter;
private ColorFilter mSelectedColorFilter;
private BitmapShader mBitmapShader;
private boolean mNeedResetShader = false;
private RectF mRectF = new RectF();
private RectF mDrawRectF = new RectF();
private Path path = null;
private Bitmap mBitmap;
private Matrix mMatrix;
private int mWidth;
private int mHeight;
private ScaleType mLastCalculateScaleType;
@IdRes
private int placeholderImage;
public RadiusImageView(Context context) {<!-- -->
this(context, null, R.attr.RadiusImageViewStyle);
}
public RadiusImageView(Context context, AttributeSet attrs) {<!-- -->
this(context, attrs, R.attr.RadiusImageViewStyle);
}
public RadiusImageView(Context context, AttributeSet attrs, int defStyleAttr) {<!-- -->
super(context, attrs, defStyleAttr);
mBorderPaint = new Paint();
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStyle(Paint.Style.STROKE);
mMatrix = new Matrix();
setScaleType(ScaleType.CENTER_CROP);
TypedArray array = context.obtainStyledAttributes(
attrs, R.styleable.RadiusImageView, defStyleAttr, 0);
mBorderWidth = array.getDimensionPixelSize(R.styleable.RadiusImageView_borderWidth, 0);
mBorderColor = array.getColor(R.styleable.RadiusImageView_borderColor, DEFAULT_BORDER_COLOR);
mSelectedBorderWidth = array.getDimensionPixelSize(
R.styleable.RadiusImageView_selectedBorderWidth, mBorderWidth);
mSelectedBorderColor = array.getColor(
R.styleable.RadiusImageView_selectedBorderColor, mBorderColor);
mSelectedMaskColor = array.getColor(
R.styleable.RadiusImageView_selectedMaskColor, Color.TRANSPARENT);
if (mSelectedMaskColor != Color.TRANSPARENT) {<!-- -->
mSelectedColorFilter = new PorterDuffColorFilter(mSelectedMaskColor, PorterDuff.Mode.DARKEN);
}
mIsTouchSelectModeEnabled = array.getBoolean(
R.styleable.RadiusImageView_isTouchSelectModeEnabled, true);
mIsCircle = array.getBoolean(R.styleable.RadiusImageView_isCircle, false);
if (!mIsCircle) {<!-- -->
mIsOval = array.getBoolean(R.styleable.RadiusImageView_isOval, false);
}
if (!mIsOval) {<!-- -->
mCornerRadius = array.getDimensionPixelSize(
R.styleable.RadiusImageView_cornerRadius, 0);
roundTopLeft = array.getBoolean(R.styleable.RadiusImageView_roundTopLeft,true);
roundTopRight = array.getBoolean(R.styleable.RadiusImageView_roundTopRight,true);
roundBottomRight = array.getBoolean(R.styleable.RadiusImageView_roundBottomRight,true);
roundBottomLeft = array.getBoolean(R.styleable.RadiusImageView_roundBottomLeft,true);
}
placeholderImage = array.getResourceId(R.styleable.RadiusImageView_placeholderImage,0);
array.recycle();
}
@Override
public void setAdjustViewBounds(boolean adjustViewBounds) {<!-- -->
if (adjustViewBounds) {<!-- -->
throw new IllegalArgumentException("不支持adjustViewBounds");
}
}
public void setBorderWidth(int borderWidth) {<!-- -->
if (mBorderWidth != borderWidth) {<!-- -->
mBorderWidth = borderWidth;
invalidate();
}
}
public void setBorderColor(@ColorInt int borderColor) {<!-- -->
if (mBorderColor != borderColor) {<!-- -->
mBorderColor = borderColor;
invalidate();
}
}
public void setCornerRadius(int cornerRadius) {<!-- -->
if (mCornerRadius != cornerRadius) {<!-- -->