- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCircularImageView.java
More file actions
Latest commit
139 lines (114 loc) · 3.63 KB
/
Copy pathCircularImageView.java
File metadata and controls
139 lines (114 loc) · 3.63 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
packagecom.wisemandesigns.android.widgets;
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapShader;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Shader;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.util.AttributeSet;
importandroid.widget.ImageView;
publicclassCircularImageViewextendsImageView {
privateintborderWidth = 5;
privateintviewWidth;
privateintviewHeight;
privateBitmapimage;
privatePaintpaint;
privatePaintpaintBorder;
privateBitmapShadershader;
publicCircularImageView(Contextcontext) {
super(context);
setup();
}
publicCircularImageView(Contextcontext, AttributeSetattrs) {
super(context, attrs);
setup();
}
publicCircularImageView(Contextcontext, AttributeSetattrs, intdefStyle) {
super(context, attrs, defStyle);
setup();
}
privatevoidsetup()
{
// init paint
paint = newPaint();
paint.setAntiAlias(true);
paintBorder = newPaint();
setBorderColor(Color.BLUE);
paintBorder.setAntiAlias(true);
}
publicvoidsetBorderWidth(intborderWidth)
{
this.borderWidth = borderWidth;
this.invalidate();
}
publicvoidsetBorderColor(intborderColor)
{
if(paintBorder != null)
paintBorder.setColor(borderColor);
this.invalidate();
}
privatevoidloadBitmap()
{
BitmapDrawablebitmapDrawable = (BitmapDrawable) this.getDrawable();
if(bitmapDrawable != null)
image = bitmapDrawable.getBitmap();
}
@SuppressLint("DrawAllocation")
@Override
publicvoidonDraw(Canvascanvas)
{
//load the bitmap
loadBitmap();
// init shader
if(image !=null)
{
shader = newBitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
intcircleCenter = viewWidth / 2;
// circleCenter is the x or y of the view's center
// radius is the radius in pixels of the cirle to be drawn
// paint contains the shader that will texture the shape
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder);
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint);
}
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec, intheightMeasureSpec)
{
intwidth = measureWidth(widthMeasureSpec);
intheight = measureHeight(heightMeasureSpec, widthMeasureSpec);
viewWidth = width - (borderWidth *2);
viewHeight = height - (borderWidth*2);
setMeasuredDimension(width, height);
}
privateintmeasureWidth(intmeasureSpec)
{
intresult = 0;
intspecMode = MeasureSpec.getMode(measureSpec);
intspecSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = viewWidth;
}
returnresult;
}
privateintmeasureHeight(intmeasureSpecHeight, intmeasureSpecWidth) {
intresult = 0;
intspecMode = MeasureSpec.getMode(measureSpecHeight);
intspecSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = viewHeight;
}
returnresult;
}
}