博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 自定义圆角图片的ImageView
阅读量:4204 次
发布时间:2019-05-26

本文共 11911 字,大约阅读时间需要 39 分钟。

每次写圆角都要去网上找,在这里做个记录:  圆角的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 void setCircle(boolean circle) {        isCircle = circle;    }    public void setRadius(float topLeft,float topRight,float bottomRight,float bottomLeft){        topLeftRadius = topLeft;        topRightRadius = topRight;        bottomRightRadius = bottomRight;        bottomLeftRadius = bottomLeft;    }    @Override    public void setScaleType(ScaleType scaleType) {        super.setScaleType(scaleType);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        defRadius = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2.0f;    }    public void setBorder(int borderColor,float borderWidth) {        this.borderColor = borderColor;        this.borderWidth = borderWidth;        if (borderWidth > 0) {            setBorderPaint();        }    }    @Override    protected void onDraw(Canvas canvas) {        int sc = canvas.saveLayer(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()), imagePaint, Canvas.ALL_SAVE_FLAG);        super.onDraw(canvas);        if (isCircle) {            drawTopLeft(canvas, defRadius);            drawTopRight(canvas, defRadius);            drawBottomLeft(canvas, defRadius);            drawBottomRight(canvas, defRadius);        } else {            drawTopLeft(canvas, topLeftRadius);            drawTopRight(canvas, topRightRadius);            drawBottomLeft(canvas, bottomLeftRadius);            drawBottomRight(canvas, bottomRightRadius);        }        canvas.restoreToCount(sc);    }    private void drawTopLeft(Canvas canvas, float topLeftRadius) {        if (topLeftRadius > 0) {            Path path = new Path();            path.moveTo(0, topLeftRadius);            path.lineTo(-5, -5);            path.lineTo(topLeftRadius, 0);            path.arcTo(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2),                    -90, -90);            path.close();            canvas.drawPath(path, roundPaint);        }        if (borderWidth > 0) {            Path path = new Path();            int width = getWidth();            int height = getHeight();            if (isCircle) {                path.moveTo(0, topLeftRadius);                path.arcTo(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2),                        -180, 90);                path.lineTo(topLeftRadius,borderWidth);                path.arcTo(new RectF(borderWidth,borderWidth,topLeftRadius * 2-borderWidth, topLeftRadius * 2-borderWidth),                        -90,-90);                path.close();            } else {                path.moveTo(width / 2, 0);                path.lineTo( topLeftRadius,0);                path.arcTo(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2),                        -90, -90);                path.lineTo(0, height / 2);                path.lineTo(borderWidth,height/2);                path.lineTo(borderWidth,topLeftRadius);                path.arcTo(new RectF(borderWidth, borderWidth, topLeftRadius * 2-borderWidth, topLeftRadius * 2-borderWidth),                        -180, 90);                path.lineTo(width/2,borderWidth);                path.close();            }            canvas.drawPath(path, borderPaint);        }    }    private void drawTopRight(Canvas canvas, float topRightRadius) {        if (topRightRadius > 0) {            int width = getWidth();            Path path = new Path();            path.moveTo(width - topRightRadius, 0);            path.lineTo(width + 5, -5);            path.lineTo(width, topRightRadius);            path.arcTo(new RectF(width - 2 * topRightRadius, 0, width,                    topRightRadius * 2), 0, -90);            path.close();            canvas.drawPath(path, roundPaint);        }        if (borderWidth > 0) {            Path path = new Path();            int width = getWidth();            int height = getHeight();            if (isCircle) {                path.moveTo(width - topRightRadius, 0);                path.arcTo(new RectF(width - 2 * topRightRadius, 0, width, topRightRadius * 2),                        -90, 90);                path.lineTo(width-borderWidth,topRightRadius);                path.arcTo(new RectF(width - 2 * topRightRadius+borderWidth, borderWidth, width-borderWidth, topRightRadius * 2-borderWidth),                        0,-90);                path.close();            } else {                path.moveTo(width / 2, 0);                path.lineTo(width - topRightRadius,0);                path.arcTo(new RectF(width - 2 * topRightRadius, 0, width, topRightRadius * 2),                        -90, 90);                path.lineTo(width, height / 2);                path.lineTo(width - borderWidth,height/2);                path.lineTo(width - borderWidth,topRightRadius);                path.arcTo(new RectF(width - 2 * topRightRadius+borderWidth, borderWidth, width-borderWidth, topRightRadius * 2-borderWidth),                        0,-90);                path.lineTo(width/2,borderWidth);                path.close();            }            canvas.drawPath(path, borderPaint);        }    }    private void drawBottomLeft(Canvas canvas, float bottomLeftRadius) {        if (bottomLeftRadius > 0) {            int height = getHeight();            Path path = new Path();            path.moveTo(0, height - bottomLeftRadius);            path.lineTo(-5, height + 5);            path.lineTo(bottomLeftRadius, height);            path.arcTo(new RectF(0, height - 2 * bottomLeftRadius,                    bottomLeftRadius * 2, height), 90, 90);            path.close();            canvas.drawPath(path, roundPaint);        }        if (borderWidth > 0) {            Path path = new Path();            int height = getHeight();            int width = getWidth();            if (isCircle) {                path.moveTo(0, height - bottomLeftRadius);                path.arcTo(new RectF(0, height - 2 * bottomLeftRadius, bottomLeftRadius * 2, height),                        -180, -90);                path.lineTo(bottomLeftRadius,height-borderWidth);                path.arcTo(new RectF(borderWidth, height - 2 * bottomLeftRadius+borderWidth, bottomLeftRadius * 2-borderWidth, height-borderWidth),                        90, 90);                path.close();            } else {                path.moveTo(0, height/2);                path.lineTo( 0,bottomLeftRadius);                path.arcTo(new RectF(0, height - 2 * bottomLeftRadius, bottomLeftRadius * 2, height),                        -180, -90);                path.lineTo(width/2, height);                path.lineTo(width/2,height-borderWidth);                path.lineTo(bottomLeftRadius,height-borderWidth);                path.arcTo(new RectF(borderWidth, height - 2 * bottomLeftRadius + borderWidth, bottomLeftRadius * 2-borderWidth, height-borderWidth),                        90, 90);                path.lineTo(borderWidth,height/2);                path.close();            }            canvas.drawPath(path, borderPaint);        }    }    private void drawBottomRight(Canvas canvas, float bottomRightRadius) {        if (bottomRightRadius > 0) {            int height = getHeight();            int width = getWidth();            Path path = new Path();            path.moveTo(width - bottomRightRadius, height);            path.lineTo(width + 5, height + 5);            path.lineTo(width, height - bottomRightRadius);            path.arcTo(new RectF(width - 2 * bottomRightRadius, height - 2                    * bottomRightRadius, width, height), 0, 90);            path.close();            canvas.drawPath(path, roundPaint);        }        if (borderWidth > 0) {            Path path = new Path();            int width = getWidth();            int height = getHeight();            if (isCircle) {                path.moveTo(width - bottomRightRadius, height);                path.arcTo(new RectF(width - 2 * bottomRightRadius, height - 2                                * bottomRightRadius, width, height),                        -270, -90);                path.lineTo(width-borderWidth,bottomRightRadius);                path.arcTo(new RectF(width - 2 * bottomRightRadius+borderWidth, height - 2                                * bottomRightRadius+borderWidth, width-borderWidth, height-borderWidth),                        0,90);                path.close();            } else {                path.moveTo(width/2 , height);                path.lineTo( width - bottomRightRadius, height);                path.arcTo(new RectF(width - 2 * bottomRightRadius, height - 2                                * bottomRightRadius, width, height),                        -270, -90);                path.lineTo(width, height/2);                path.lineTo(width-borderWidth,height/2);                path.lineTo(width-borderWidth ,height- bottomRightRadius);                path.arcTo(new RectF(width - 2 * bottomRightRadius+borderWidth, height - 2                                * bottomRightRadius + borderWidth, width-borderWidth, height-borderWidth),                        0,90);                path.lineTo(width/2,height-borderWidth);                path.close();            }            canvas.drawPath(path, borderPaint);        }    }}
布局引用 

以上是自定类还要在 styles 文件里面添加style   

 

 

以上完成图片圆角设置了圆角的程度在布局设置就行了

转载地址:http://putli.baihongyu.com/

你可能感兴趣的文章