Android自定义ImageView圆角

本文介绍了在Android中实现ImageView显示圆角图片的多种方法,包括自定义ImageView、使用Glide的BitmapTransformation、通过Fresco的SimpleDraweeView以及利用CardView。推荐使用CardView简单实现圆角,但面对复杂的圆角需求时,建议自定义ImageView以满足定制化需求。代码示例展示了如何创建一个自定义的RadiusImageView,支持圆、椭圆、圆角以及边距、颜色等特性。
摘要由CSDN通过智能技术生成

圆角的实现方式
在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) {<!-- -->
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值