01
02
03
04
05
06
07
08
09
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
|
/**
* ImageView圆形遮罩处理
* background设置遮罩颜色
* tag设置圆的边框色
* padding设置圆形线宽
* @author planet
*
*/
public
class
CircleImageView
extends
ImageView {
public
CircleImageView(Context context) {
super
(context);
initColor(
null
);
}
public
CircleImageView(Context context, AttributeSet attrs) {
super
(context, attrs);
initColor(attrs);
}
public
CircleImageView(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
initColor(attrs);
}
private
Paint paint;
private
Path path;
private
boolean
init =
false
;
private
int
background = Color.WHITE;
private
int
circleLineWidth =
6
;
private
int
circleColor = Color.RED;
private
void
initColor(AttributeSet attrs){
if
(attrs !=
null
){
String v = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android"
,
"background"
);
if
(v !=
null
){
if
(v.startsWith(
"#"
)){
background = Color.parseColor(v);
}
else
{
background = getResources().getColor(Integer.parseInt(v.replaceAll(
"@"
,
""
)));
}
}
}
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
if
(!init){
initPaint();
}
}
private
void
initPaint(){
circleLineWidth = getPaddingLeft();
setPadding(
0
,
0
,
0
,
0
);
paint =
new
Paint();
paint.setStyle(Style.FILL);
paint.setColor(background);
//paint.setColor(Color.TRANSPARENT);
paint.setAntiAlias(
true
);
//paint.setStrokeWidth(2);
//paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
//Log.i(VIEW_LOG_TAG, "22>>>>>>>>>>>>>>>>>>>>3getPaddingBottom()="+getPaddingBottom());
int
width = getMeasuredWidth();
float
radius = width/(
float
)
2
;
path =
new
Path();
Log.i(VIEW_LOG_TAG,
"moveto 0,"
+radius);
path.moveTo(
0
, radius);
Log.i(VIEW_LOG_TAG,
"lineto 0,0"
);
path.lineTo(
0
,
0
);
Log.i(VIEW_LOG_TAG,
"lineto "
+width+
",0"
);
path.lineTo(width,
0
);
Log.i(VIEW_LOG_TAG,
"lineto "
+width+
","
+width);
path.lineTo(width, width);
Log.i(VIEW_LOG_TAG,
"lineto 0,"
+width);
path.lineTo(
0
, width);
Log.i(VIEW_LOG_TAG,
"lineto 0,"
+radius);
path.lineTo(
0
, radius);
//圆弧左边中间起点是180,旋转360度
//Log.i(VIEW_LOG_TAG, "arcto 0,0,"+width+","+width);
path.arcTo(
new
RectF(
0
,
0
, width, width),
180
, -
359
,
true
);
//path.addCircle(radius, radius, radius, Direction.CW);
path.close();
try
{
circleColor = Color.parseColor((String) getTag());
}
catch
(Exception e) {
e.printStackTrace();
}
init =
true
;
}
@Override
protected
void
onDraw(Canvas canvas) {
super
.onDraw(canvas);
paint.setColor(background);
paint.setStyle(Style.FILL);
canvas.drawPath(path, paint);
paint.setColor(circleColor);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(circleLineWidth);
int
width = getMeasuredHeight();
canvas.drawCircle(width/
2
, width/
2
, (
float
) (width/
2
-circleLineWidth*.
5
), paint);
}
}
|
01
02
03
04
05
06
07
08
09
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
|
/**
* ImageView圆形切割处理(感觉比较浪费内存,一般情况下使用CircleImageView足够了)
* padding代表圆形边框线粗,background代表圆形边框的颜色
* @author planet
*
*/
public
class
ClipCircleImageView
extends
ImageView {
public
ClipCircleImageView(Context context) {
super
(context);
}
public
ClipCircleImageView(Context context, AttributeSet attrs) {
super
(context, attrs);
initColor(attrs);
}
public
ClipCircleImageView(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
initColor(attrs);
}
private
int
background = Color.WHITE;
private
Paint paint;
private
boolean
set =
false
;
private
int
padding =
0
;
private
void
initColor(
final
AttributeSet attrs){
if
(attrs !=
null
){
String v = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android"
,
"background"
);
if
(v !=
null
){
if
(v.startsWith(
"#"
)){
background = Color.parseColor(v);
}
else
{
background = getResources().getColor(Integer.parseInt(v.replaceAll(
"@"
,
""
)));
}
}
}
setBackgroundResource(android.R.color.transparent);
paint =
new
Paint();
paint.setColor(background);
paint.setAntiAlias(
true
);
paint.setStyle(Style.STROKE);
padding = getPaddingLeft();
setPadding(
0
,
0
,
0
,
0
);
}
@Override
public
void
setImageBitmap(
final
Bitmap bm) {
post(
new
Runnable() {
@Override
public
void
run() {
set =
true
;
ClipCircleImageView.
super
.setImageBitmap(getCroppedBitmap(bm));
}
});
}
@Override
protected
void
onDraw(Canvas canvas) {
if
(!set){
set =
true
;
setImageBitmap(getCroppedBitmap(((BitmapDrawable) getDrawable()).getBitmap()));
}
super
.onDraw(canvas);
paint.setStrokeWidth(padding);
canvas.drawCircle(getMeasuredWidth()/
2
, getMeasuredWidth()/
2
, (
float
) (getMeasuredWidth()*.
5
-padding*.
5
), paint);
}
public
Bitmap getCroppedBitmap(Bitmap bitmap) {
int
width = getMeasuredWidth();
//int height = getMeasuredHeight();
Bitmap output = Bitmap.createBitmap(width,
width, Config.ARGB_8888);
Canvas canvas =
new
Canvas(output);
final
int
color =
0xff424242
;
final
Paint paint =
new
Paint();
final
Rect rect =
new
Rect(
0
,
0
, width, width);
paint.setAntiAlias(
true
);
canvas.drawARGB(
0
,
0
,
0
,
0
);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(width /
2
, width /
2
,
width /
2
, paint);
paint.setXfermode(
new
PorterDuffXfermode(Mode.SRC_IN));
//bitmap = ImageTool.scale(width, bitmap);
canvas.drawBitmap(bitmap, rect, rect, paint);
return
output;
}
}
|