admin
2021-06-11 ae4dc86b64bd8ef85bc832106741fb98e8d516da
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
50
51
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 RoundedImage2View 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 static float r = 0.0f;
    private float[] rids;
 
 
    public RoundedImage2View(Context context) {
        this(context, null);
    }
 
    public RoundedImage2View(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public RoundedImage2View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        r = ScreenUtils.dp2px(context, 10);
        rids = new float[]{r, r, r, r, r, r, r, r,};//四个角都圆角
    }
 
 
    /**
     * 画图
     *
     * @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), rids, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
}