1 public class RoundAngleImageView
extends ImageView {
2 //cn.dpocket.moplusand.uinew是app的包名 manifest文件里面
3 private static final String NAMESPACE = "http://schemas.android.com/apk/res/cn.dpocket.moplusand.uinew"
;
4 private Paint paint;
5 //如果不在xml属性里面设置自定义属性 默认值为5dp
6 private int roundWidth = 5
;
7 private int roundHeight = 5
;
8 private Paint paint2;
9 public RoundAngleImageView(Context context) {
10 super(context);
11 init(context,
null);
12 }
13
14 public RoundAngleImageView(Context context, AttributeSet attrs) {
15 super(context, attrs);
16 init(context,attrs);
17 }
18
19 private void init(Context context, AttributeSet attrs) {
20
21 if(attrs !=
null) {
22 roundWidth= attrs.getAttributeIntValue(NAMESPACE,"roundWidth",0
);
23 roundHeight= attrs.getAttributeIntValue(NAMESPACE, "roundHeight", 0
);
24 }
else {
25 float density =
context.getResources().getDisplayMetrics().density;
26 roundWidth = (
int) (roundWidth*
density);
27 roundHeight = (
int) (roundHeight*
density);
28 }
29
30 paint =
new Paint();
31 paint.setColor(Color.WHITE);
32 paint.setAntiAlias(
true);
33 paint.setXfermode(
new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
34
35 paint2 =
new Paint();
36 paint2.setXfermode(
null);
37 }
38
39 @Override
40 public void draw(Canvas canvas) {
41 Bitmap bitmap =
Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
42 Canvas canvas2 =
new Canvas(bitmap);
43 super.draw(canvas2);
44 drawLiftUp(canvas2);
45 drawRightUp(canvas2);
46 drawLiftDown(canvas2);
47 drawRightDown(canvas2);
48 canvas.drawBitmap(bitmap, 0, 0
, paint2);
49 bitmap.recycle();
50 }
51
52 private void drawLiftUp(Canvas canvas) {
53 Path path =
new Path();
54 path.moveTo(0
, roundHeight);
55 path.lineTo(0, 0
);
56 path.lineTo(roundWidth, 0
);
57 path.arcTo(
new RectF(
58 0
,
59 0
,
60 roundWidth*2
,
61 roundHeight*2
),
62 -90
,
63 -90
);
64 path.close();
65 canvas.drawPath(path, paint);
66 }
67
68 private void drawLiftDown(Canvas canvas) {
69 Path path =
new Path();
70 path.moveTo(0, getHeight()-
roundHeight);
71 path.lineTo(0
, getHeight());
72 path.lineTo(roundWidth, getHeight());
73 path.arcTo(
new RectF(
74 0
,
75 getHeight()-roundHeight*2
,
76 0+roundWidth*2
,
77 getHeight()),
78 90
,
79 90
);
80 path.close();
81 canvas.drawPath(path, paint);
82 }
83
84 private void drawRightDown(Canvas canvas) {
85 Path path =
new Path();
86 path.moveTo(getWidth()-
roundWidth, getHeight());
87 path.lineTo(getWidth(), getHeight());
88 path.lineTo(getWidth(), getHeight()-
roundHeight);
89 path.arcTo(
new RectF(
90 getWidth()-roundWidth*2
,
91 getHeight()-roundHeight*2
,
92 getWidth(),
93 getHeight()), 0, 90
);
94 path.close();
95 canvas.drawPath(path, paint);
96 }
97
98 private void drawRightUp(Canvas canvas) {
99 Path path =
new Path();
100 path.moveTo(getWidth(), roundHeight);
101 path.lineTo(getWidth(), 0
);
102 path.lineTo(getWidth()-roundWidth, 0
);
103 path.arcTo(
new RectF(
104 getWidth()-roundWidth*2
,
105 0
,
106 getWidth(),
107 0+roundHeight*2
),
108 -90
,
109 90
);
110 path.close();
111 canvas.drawPath(path, paint);
112 }
113 }
圆角imageView
定义一个attr.xml的文件,放在values目录下面,内容如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="RoundAngleImageView">
4 <attr name="roundWidth" format="integer" />
5 <attr name="roundHeight" format="integer" />
6 </declare-styleable>
7 </resources>
自定义属性
在布局文件的跟布局中 加入如下代码:
//cn.dpocket.moplusand.uinew是app的包名 manifest文件中
xmlns:app="http://schemas.android.com/apk/res/cn.dpocket.moplusand.uinew"
1 <cn.dpocket.moplusand.uinew.widget.RoundAngleImageView
2 android:id="@+id/iv_head1"
3 android:layout_width="68dp"
4 android:layout_height="68dp"
5 app:roundWidth="8"
6 app:roundHeight="8"
7 ></cn.dpocket.moplusand.uinew.widget.RoundAngleImageView>
自定义view的全路径
转载于:https://www.cnblogs.com/YyuTtian/p/5396111.html