支持圆角+描边的ImageView

1.效果图

2.自定义view代码

/**
 *
 * Created by wanghaibo on 2020/1/9.
 */
public class RoundImageView extends androidx.appcompat.widget.AppCompatImageView {

    Paint paint;
    /**
     * 圆角的半径,依次为左上角xy半径,右上角,右下角,左下角
     */
    private float[] rids;
    private float round, roundLeftTop, roundRightTop, roundLeftBottom, roundRightBottom;
    /**
     * 描边宽度
     */
    private float strokeWidth;
    /**
     * 描边宽度
     */
    private int strokeColor;

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

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

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundConstraintLayout);
        round = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_round, 0);
        roundLeftTop = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_leftTopRound, 0);
        roundRightTop = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_rightTopRound, 0);
        roundLeftBottom = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_leftBottomRound, 0);
        roundRightBottom = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_rightBottomRound, 0);
        strokeWidth = typedArray.getDimensionPixelSize(R.styleable.RoundConstraintLayout_strokeWidth, 0);
        if (roundLeftTop == 0) {
            roundLeftTop = round;
        }
        if (roundRightTop == 0) {
            roundRightTop = round;
        }
        if (roundLeftBottom == 0) {
            roundLeftBottom = round;
        }
        if (roundRightBottom == 0) {
            roundRightBottom = round;
        }
        strokeColor = typedArray.getColor(R.styleable.RoundConstraintLayout_strokeColor, Color.WHITE);
        rids = new float[]{roundLeftTop, roundLeftTop, roundRightTop, roundRightTop, roundLeftBottom, roundLeftBottom, roundRightBottom, roundRightBottom};
        paint = new Paint();
    }


    /**
     * 画图
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(0, 0, w, h), rids, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
        if (strokeWidth > 0) {
            float padding = strokeWidth / 2f;
            RectF rectF2 = null;
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(strokeColor);
            paint.setStrokeWidth(strokeWidth);
            if (round >= w / 2) {
                rectF2 = new RectF(padding, padding, w - padding, h - padding);
                canvas.drawArc(rectF2, 0, 360, true, paint);
            }
        }
    }
}

3.配置属性

在valus/attrs.xml中添加配置,这里有一个技巧,防止存在重名的配置可以这样写

<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <attr name="round" format="dimension" />
    <attr name="leftTopRound" format="dimension" />
    <attr name="rightTopRound" format="dimension" />
    <attr name="leftBottomRound" format="dimension" />
    <attr name="rightBottomRound" format="dimension" />
    <attr name="strokeWidth" format="dimension" />
    <attr name="strokeColor" format="color" />
    <attr name="ratio" format="float" />
    <attr name="widthFollowHeight" format="boolean" />

    <declare-styleable name="RoundImageView">
        <attr name="round" />
        <attr name="leftTopRound" />
        <attr name="rightTopRound" />
        <attr name="leftBottomRound" />
        <attr name="rightBottomRound" />
        <attr name="strokeWidth" />
        <attr name="strokeColor" />
        <attr name="ratio" />
        <attr name="widthFollowHeight" />
    </declare-styleable>
</resources>

4.使用

<com.test.view.RoundImageView
    android:id="@+id/iv_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="centerCrop"
    app:round="50dp"
    app:strokeColor="#EFDFD7"
    app:strokeWidth="1dp" />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值