我是靠谱客的博主 年轻黑猫,这篇文章主要介绍Android实现圆角图片的方法,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Android实现圆角图片的具体代码,供大家参考,具体内容如下

效果图

创建类CustomRoundAngleImageView

复制代码
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
public class CustomRoundAngleImageView extends AppCompatImageView { float width, height; public CustomRoundAngleImageView(Context context) { this(context, null); init(context, null); } public CustomRoundAngleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); init(context, attrs); } public CustomRoundAngleImageView(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) { if (width >= 20 && height > 20) { Path path = new Path(); //四个圆角 path.moveTo(20, 0); path.lineTo(width - 20, 0); path.quadTo(width, 0, width, 20); path.lineTo(width, height - 20); path.quadTo(width, height, width - 20, height); path.lineTo(20, height); path.quadTo(0, height, 0, height - 20); path.lineTo(0, 20); path.quadTo(0, 0, 20, 0); canvas.clipPath(path); } super.onDraw(canvas); } }

布局代码

复制代码
1
2
3
4
5
6
7
8
9
10
<com.example.myapplication.CustomRoundAngleImageView android:id="@+id/we_image" android:layout_width="42dp" android:layout_height="42dp" android:layout_marginLeft="16dp" android:layout_marginTop="10dp" android:src="@mipmap/ic_launcher" android:layout_marginBottom="10dp" android:layout_gravity="center" android:scaleType="centerCrop" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是年轻黑猫最近收集整理的关于Android实现圆角图片的方法的全部内容,更多相关Android实现圆角图片内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(171)

评论列表共有 0 条评论

立即
投稿
返回
顶部