服务器之家

服务器之家 > 正文

Android自定义圆角ImageView

时间:2021-05-07 15:58     来源/作者:whyrjj3

废话不多说了,直接给大家贴代码了。

java类如下:

?
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import android.content.context;
import android.content.res.typedarray;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.porterduff;
import android.graphics.porterduffxfermode;
import android.graphics.rectf;
import android.util.attributeset;
import android.widget.imageview;
import cn.dotcreate.tt.r;
public class roundangleimageview extends imageview {
private paint paint;
private int roundwidth = 5;
private int roundheight = 5;
private paint paint2;
public roundangleimageview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
init(context, attrs);
}
public roundangleimageview(context context, attributeset attrs) {
super(context, attrs);
init(context, attrs);
}
public roundangleimageview(context context) {
super(context);
init(context, null);
}
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();
}
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,
getheight()),
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);
}
}

定义一个attr.xml的文件,放在values目录下面,内容如下:

?
1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="roundangleimageview">
<attr name="roundwidth" format="dimension" />
<attr name="roundheight" format="dimension" />
</declare-styleable>
</resources>

使用示例如下:

先要声明属性的名字空间:

Android自定义圆角ImageView

然后再写跟一般定义view一样:

?
1
2
3
4
5
6
7
8
9
<cn.dotcreate.tt.ui.roundangleimageview
android:id="@+id/headiv"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centervertical="true"
android:layout_marginleft="2dp"
app:roundwidth="10dp"
app:roundheight="10dp"
android:src="@drawable/default_head_icon" />

效果如图:

Android自定义圆角ImageView

以上代码简单介绍了android自定义圆角imageview的相关知识,希望本文分享对大家有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部