安卓ImageView实现上面两个圆角下面两个直角

效果图:

 代码:

/**
 * 图片上左上右圆角
 */
public class RoundImageView extends AppCompatImageView {
    float width, height;

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

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

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //width是图片的宽,height是图片的高
        if (width >= 50 && height > 50) {
            Path path = new Path();

            path.moveTo(30, 0);//起始位置50是左角的圆
            path.lineTo(width - 30, 0);//画直线,这2句代码是在图片的最上方画一条留了左右圆角半径的直线
            path.quadTo(width, 0, width, 30);//画右上圆角
            path.lineTo(width, height);//画 ‘画右上圆角’后的直线一直到右下角的边界
            path.lineTo(0, height);//从右下角的边界一直画到左下角的直线
            path.lineTo(0, 30);//从左下角一直画到左上角的画圆的开始位置
            path.quadTo(0, 0, 30, 0);//画左上角的圆

            canvas.clipPath(path);
        }
        super.onDraw(canvas);
    }
}

 在xml中引用:

<com.example.shopcode.views.RoundImageView
   android:id="@+id/iv_spike"
   android:layout_width="match_parent"
   android:layout_height="152dp"
   android:scaleType="fitXY"/>

猜你喜欢

转载自blog.csdn.net/L73748196_/article/details/127659163