您的位置:首页 > 移动开发 > Android开发

android自定义一圆角ImageView

2016-03-16 14:09 399 查看
原文链接:http://blog.csdn.net/whyrjj3/article/details/7975480#

JAVA 类:

<span style="font-family:KaiTi_GB2312;font-size:18px;">01.import android.content.Context;
02.import android.content.res.TypedArray;
03.import android.graphics.Bitmap;
04.import android.graphics.Bitmap.Config;
05.import android.graphics.Canvas;
06.import android.graphics.Color;
07.import android.graphics.Paint;
08.import android.graphics.Path;
09.import android.graphics.PorterDuff;
10.import android.graphics.PorterDuffXfermode;
11.import android.graphics.RectF;
12.import android.util.AttributeSet;
13.import android.widget.ImageView;
14.import cn.dotcreate.tt.R; //替换为你的R文件所在路径
15.
16.public class RoundAngleImageView extends ImageView {
17.
18. private Paint paint;
19. private int roundWidth = 5;
20. private int roundHeight = 5;
21. private Paint paint2;
22.
23. public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {
24. super(context, attrs, defStyle);
25. init(context, attrs);
26. }
27.
28. public RoundAngleImageView(Context context, AttributeSet attrs) {
29. super(context, attrs);
30. init(context, attrs);
31. }
32.
33. public RoundAngleImageView(Context context) {
34. super(context);
35. init(context, null);
36. }
37.
38. private void init(Context context, AttributeSet attrs) {
39.
40. if(attrs != null) {
41. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
42. roundWidth= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
43. roundHeight= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
44. }else {
45. float density = context.getResources().getDisplayMetrics().density;
46. roundWidth = (int) (roundWidth*density);
47. roundHeight = (int) (roundHeight*density);
48. }
49.
50. paint = new Paint();
51. paint.setColor(Color.WHITE);
52. paint.setAntiAlias(true);
53. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
54.
55. paint2 = new Paint();
56. paint2.setXfermode(null);
57. }
58.
59. @Override
60. public void draw(Canvas canvas) {
61. Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
62. Canvas canvas2 = new Canvas(bitmap);
63. super.draw(canvas2);
64. drawLeftUp(canvas2);
65. drawRightUp(canvas2);
66. drawLeftDown(canvas2);
67. drawRightDown(canvas2);
68. canvas.drawBitmap(bitmap, 0, 0, paint2);
69. bitmap.recycle();
70. }
71.
72. private void drawLeftUp(Canvas canvas) {
73. Path path = new Path();
74. path.moveTo(0, roundHeight);
75. path.lineTo(0, 0);
76. path.lineTo(roundWidth, 0);
77. path.arcTo(new RectF(
78. 0,
79. 0,
80. roundWidth*2,
81. roundHeight*2),
82. -90,
83. -90);
84. path.close();
85. canvas.drawPath(path, paint);
86. }
87.
88. private void drawLeftDown(Canvas canvas) {
89. Path path = new Path();
90. path.moveTo(0, getHeight()-roundHeight);
91. path.lineTo(0, getHeight());
92. path.lineTo(roundWidth, getHeight());
93. path.arcTo(new RectF(
94. 0,
95. getHeight()-roundHeight*2,
96. 0+roundWidth*2,
97. getHeight()),
98. 90,
99. 90);
100. path.close();
101. canvas.drawPath(path, paint);
102. }
103.
104. private void drawRightDown(Canvas canvas) {
105. Path path = new Path();
106. path.moveTo(getWidth()-roundWidth, getHeight());
107. path.lineTo(getWidth(), getHeight());
108. path.lineTo(getWidth(), getHeight()-roundHeight);
109. path.arcTo(new RectF(
110. getWidth()-roundWidth*2,
111. getHeight()-roundHeight*2,
112. getWidth(),
113. getHeight()), 0, 90);
114. path.close();
115. canvas.drawPath(path, paint);
116. }
117.
118. private void drawRightUp(Canvas canvas) {
119. Path path = new Path();
120. path.moveTo(getWidth(), roundHeight);
121. path.lineTo(getWidth(), 0);
122. path.lineTo(getWidth()-roundWidth, 0);
123. path.arcTo(new RectF(
124. getWidth()-roundWidth*2,
125. 0,
126. getWidth(),
127. 0+roundHeight*2),
128. -90,
129. 90);
130. path.close();
131. canvas.drawPath(path, paint);
132. }
133.
134.} </span>定义一个attr.xml的文件,放在values目录下面,内容如下:
<?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>


使用示例如下:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res/你的包名"

然后再写跟一般定义View一样:
<xxx.xxx.xxx.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" />

效果图就不粘了,在此基础上可以定义的更为灵活,比如左上角,左下角,右上角,右下角。这样就可以想倒那个角就倒那个角。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: