Android 圆角 ImageView类可设置弧度(代码简单)

时间:2022-05-11 07:19:02

废话不多说了,直接给大家贴代码了,具体代码如下所示:

?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class roundimageview extends imageview {
private paint paint;
private int roundwidth = 50;
private int roundheight = 50;
private paint paint2;
public roundimageview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
init(context, attrs);
}
public roundimageview(context context, attributeset attrs) {
super(context, attrs);
init(context, attrs);
}
public void setroundvalue(float roundvalue) {
roundwidth = (int) roundvalue;
roundheight = (int) roundvalue;
}
public roundimageview(context context) {
super(context);
init(context, null);
}
@suppresslint("recycle")
private void init(context context, attributeset attrs) {
if (attrs != null) {
typedarray a = context.obtainstyledattributes(attrs, r.styleable.roundangleimageview);
roundwidth = a.getdimensionpixelsize(r.styleable.roundangleimageview_roundwidth, roundwidth);
roundheight = a.getdimensionpixelsize(r.styleable.roundangleimageview_roundheight, roundheight);
} else {
float density = context.getresources().getdisplaymetrics().density;
roundwidth = (int) (roundwidth * density);
roundheight = (int) (roundheight * density);
}
paint = new paint();
paint.setcolor(color.white);
paint.setantialias(true);
paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out));
paint2 = new paint();
paint2.setxfermode(null);
}
@override
public void draw(canvas canvas) {
bitmap bitmap = bitmap.createbitmap(getwidth(), getheight(), config.argb_8888);
canvas canvas2 = new canvas(bitmap);
super.draw(canvas2);
drawliftup(canvas2);
drawrightup(canvas2);
drawliftdown(canvas2);
drawrightdown(canvas2);
canvas.drawbitmap(bitmap, 0, 0, paint2);
bitmap.recycle();
bitmap = null;
}
private void drawliftup(canvas canvas) {
path path = new path();
path.moveto(0, roundheight);
path.lineto(0, 0);
path.lineto(roundwidth, 0);
path.arcto(new rectf(0, 0, roundwidth * 2, roundheight * 2), -90, -90);
path.close();
canvas.drawpath(path, paint);
}
private void drawliftdown(canvas canvas) {
path path = new path();
path.moveto(0, getheight() - roundheight);
path.lineto(0, getheight());
path.lineto(roundwidth, getheight());
path.arcto(new rectf(0, getheight() - roundheight * 2, 0 + roundwidth * 2, getwidth()), 90, 90);
path.close();
canvas.drawpath(path, paint);
}
private void drawrightdown(canvas canvas) {
path path = new path();
path.moveto(getwidth() - roundwidth, getheight());
path.lineto(getwidth(), getheight());
path.lineto(getwidth(), getheight() - roundheight);
path.arcto(new rectf(getwidth() - roundwidth * 2, getheight() - roundheight * 2, getwidth(), getheight()), 0,
90);
path.close();
canvas.drawpath(path, paint);
}
private void drawrightup(canvas canvas) {
path path = new path();
path.moveto(getwidth(), roundheight);
path.lineto(getwidth(), 0);
path.lineto(getwidth() - roundwidth, 0);
path.arcto(new rectf(getwidth() - roundwidth * 2, 0, getwidth(), 0 + roundheight * 2), -90, 90);
path.close();
canvas.drawpath(path, paint);
}
}

好了,有关android 圆角 imageview类可设置弧度的内容小编就给大家介绍到这里,希望对大家有所帮助!