Android 自定义圆角图片的ImageView

每次写圆角都要去网上找,在这里做个记录:  圆角的imageview

/**
 * 圆角的ImageView
 */
public class MyCircleImageView extends AppCompatImageView {
    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;
    private boolean isCircle = true;
    private float defRadius = 0;

    private Paint roundPaint;
    private Paint imagePaint;
    private int borderColor;
    private float borderWidth;
    private Paint borderPaint;

    public MyCircleImageView(Context context) {
        this(context, null);
    }

    public MyCircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        super.setScaleType(ScaleType.CENTER_CROP);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCircleImageView);
            float radius = ta.getDimension(R.styleable.MyCircleImageView_radius, 0);
            topLeftRadius = ta.getDimension(R.styleable.MyCircleImageView_topLeftRadius, radius);
            topRightRadius = ta.getDimension(R.styleable.MyCircleImageView_topRightRadius, radius);
            bottomLeftRadius = ta.getDimension(R.styleable.MyCircleImageView_bottomLeftRadius, radius);
            bottomRightRadius = ta.getDimension(R.styleable.MyCircleImageView_bottomRightRadius, radius);
            isCircle = ta.getBoolean(R.styleable.MyCircleImageView_isCircle, true);
            borderColor = ta.getColor(R.styleable.MyCircleImageView_circleBorderColor, Color.WHITE);
            borderWidth = ta.getDimension(R.styleable.MyCircleImageView_circleBorderWidth, 0);
            ta.recycle();
        }
        roundPaint = new Paint();
        roundPaint.setColor(Color.WHITE);
        roundPaint.setAntiAlias(true);
        roundPaint.setDither(true);
        roundPaint.setStyle(Paint.Style.FILL);
        roundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        imagePaint = new Paint();
        imagePaint.setXfermode(null);

        if (borderWidth > 0) {
            setBorderPaint();
        }
    }

    private void setBorderPaint() {
        if (borderPaint == null){
            borderPaint = new Paint();
            borderPaint.setColor(borderColor);
            borderPaint.setAntiAlias(true);
            borderPaint.setDither(true);
            borderPaint.setStyle(Paint.Style.FILL);
            borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        }
    }

    public vo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值