/
CircularImageView.java
203 lines (171 loc) · 6.36 KB
/
CircularImageView.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package net.goeasyway.coverflow;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by lan on 16/12/2.
*/
public class CircularImageView extends ImageView {
private Paint paint;
private Paint paintBorder;
private int width;
private int height;
private int canvasSize;
private float shadowRadius;
private int shadowColor = 0xFF3f51b5;
private float borderWidth;
private Drawable drawable;
private Bitmap imageBitmap;
public CircularImageView(Context context) {
this(context, null);
}
public CircularImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
float density = context.getResources().getDisplayMetrics().density;
borderWidth = 4 * density;
shadowRadius = 8 * density;
paint = new Paint();
paint.setAntiAlias(true);
paintBorder = new Paint();
paint.setAntiAlias(true);
paintBorder.setColor(0xFF3f51b5);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
}
paintBorder.setShadowLayer(shadowRadius, 0, 0, shadowColor);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
canvasSize = w;
if (w > h) {
canvasSize = h;
}
if (imageBitmap != null) {
updateShader();
}
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
loadBitmap();
if (imageBitmap == null) {
return;
}
//canvas.translate(width / 2, height / 2);
int radius = (int) ((canvasSize - borderWidth * 2) / 2);
canvas.drawCircle(radius + borderWidth, radius + borderWidth, radius + borderWidth - (shadowRadius + shadowRadius / 2), paintBorder);
canvas.drawCircle(radius + borderWidth, radius + borderWidth, radius - (shadowRadius + shadowRadius / 2), paint);
}
@Override
public ScaleType getScaleType() {
return ScaleType.CENTER_CROP;
}
private void loadBitmap() {
if (drawable == getDrawable()) {
return;
}
drawable = getDrawable();
imageBitmap = drawableToBitmap(drawable);
updateShader();
}
private void updateShader() {
if (imageBitmap == null) {
return;
}
imageBitmap = cropBitmap(imageBitmap);
BitmapShader shader = new BitmapShader(imageBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setScale((float) canvasSize / (float) imageBitmap.getWidth(), (float) canvasSize / (float) imageBitmap.getHeight());
shader.setLocalMatrix(matrix);
paint.setShader(shader);
}
private Bitmap cropBitmap(Bitmap imageBitmap) {
Bitmap bitmap = null;
if (imageBitmap.getWidth() > imageBitmap.getHeight()) {
bitmap = Bitmap.createBitmap(imageBitmap, (imageBitmap.getWidth() / 2) - (imageBitmap.getHeight() / 2),
0, imageBitmap.getHeight(), imageBitmap.getHeight());
} else {
bitmap = Bitmap.createBitmap(imageBitmap, 0, (imageBitmap.getHeight() / 2) - (imageBitmap.getWidth() / 2),
imageBitmap.getWidth(), imageBitmap.getWidth());
}
return bitmap;
}
private Bitmap drawableToBitmap(Drawable d) {
if (d == null) {
return null;
} else if (d instanceof BitmapDrawable) {
return ((BitmapDrawable)d).getBitmap();
}
int intrinsicWidth = d.getIntrinsicWidth();
int intrinsicHeight = d.getIntrinsicHeight();
if (!(intrinsicHeight > 0 && intrinsicWidth > 0)) {
return null;
}
try {
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
d.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
return null;
}
}
//region Mesure Method
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
/*int imageSize = (width < height) ? width : height;
setMeasuredDimension(imageSize, imageSize);*/
setMeasuredDimension(width, height);
}
private int measureWidth(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// The parent has determined an exact size for the child.
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// The parent has not imposed any constraint on the child.
result = canvasSize;
}
return result;
}
private int measureHeight(int measureSpecHeight) {
int result;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
//endregion
}