-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleImage.java
113 lines (101 loc) · 3.73 KB
/
CircleImage.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
package com.jocoo.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
/**
* Circle ImageView
* Created by Jocoo on 2016/12/10.
*/
public class CircleImage extends ImageView {
private BitmapShader mBitmapShader;
private Paint mPaint;
private Bitmap mBitmap;
private float mSideLength;
private Rect mCircleRect;
private Paint mBackgroudPaint;
private int mBgColor = 0xff434343;
public CircleImage(Context context) {
this(context, null);
}
public CircleImage(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.CENTER_CROP);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mSideLength = Math.min(w, h);
init();
}
private Bitmap getBitmap() {
Bitmap bitmap;
final Drawable drawable = getDrawable();
if (drawable == null) return null;
if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
bitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
}
return bitmap;
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBitmap = getBitmap();
if (mBitmap == null) return;
mBitmapShader = new BitmapShader(getBitmap(),
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mBitmapShader.setLocalMatrix(calcScaleMatrix());
mPaint.setShader(mBitmapShader);
mBackgroudPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroudPaint.setColor(mBgColor);
}
private Matrix calcScaleMatrix() {
int width = getWidth();
int height = getHeight();
int widthBitmap = mBitmap.getWidth();
int heightBitmap = mBitmap.getHeight();
Matrix matrix = new Matrix();
float scale = 0f;
float dx = 0f;
float dy = 0f;
if (getHeight() > getWidth()) {
mCircleRect = new Rect(0, (height - width) / 2, width, (height + width) / 2);
} else {
mCircleRect = new Rect((width - height) / 2, 0, (height + width) / 2, height);
}
// dx and dy is relative to ImageView's origin
if (widthBitmap < heightBitmap) {
scale = mSideLength / widthBitmap;
dy = mCircleRect.top + (mSideLength - heightBitmap * scale) / 2;
dx = mCircleRect.left;
} else {
scale = mSideLength / heightBitmap;
dx = mCircleRect.left + (mSideLength - widthBitmap * scale) / 2;
dy = mCircleRect.top;
}
Log.d("metrics", "dx: " + dx + " dy: " + dy + " scale: " + scale);
matrix.setScale(scale, scale);
matrix.postTranslate(dx, dy);
return matrix;
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mSideLength / 2, mBackgroudPaint);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mSideLength / 2, mPaint);
}
}