admin
2021-07-08 1764c1784a4cf1a6afd25fcf1a0eef6187a84218
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.tejia.lijin.app.util;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
 
import com.app.hubert.guide.util.ScreenUtils;
 
/**
 * 左上右上圆角图片
 */
public class RoundedImageView extends androidx.appcompat.widget.AppCompatImageView {
 
    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/
//    private static float[] rids = new float[8];//= {10.0f, 10.0f, 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f,};
//    private float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f,};//四个角都圆角
    private static float rids = 10;
 
    public RoundedImageView(Context context) {
        this(context, null);
    }
 
    public RoundedImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        rids = ScreenUtils.dp2px(context, 10);
    }
 
 
    /**
     * 画图
     *
     * @param canvas
     */
    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), new float[]{rids, rids, rids, rids, 0.0f, 0.0f, 0.0f, 0.0f}, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
}