1
1
/*******************************************************************************
2
- * Copyright 2011-2014 Sergey Tarasevich
2
+ * Copyright 2015 Sergey Tarasevich
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
* Can display bitmap cropped by a circle. This implementation works only with ImageViews wrapped
36
36
* in ImageViewAware.
37
37
* <br />
38
- * This implementation is inspired by
39
- * <a href="http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/">
40
- * Romain Guy's article</a>. It rounds images using custom drawable drawing. Original bitmap isn't changed.
41
- * <br />
42
- * <br />
43
38
* If this implementation doesn't meet your needs then consider
44
39
* <a href="https://github.com/vinc3m1/RoundedImageView">RoundedImageView</a> or
45
40
* <a href="https://github.com/Pkmmte/CircularImageView">CircularImageView</a> projects for usage.
41
+ *
42
+ * @author Qualtagh, Sergey Tarasevich (nostra13[at]gmail[dot]com)
43
+ * @since 1.9.5
46
44
*/
47
45
public class CircleBitmapDisplayer implements BitmapDisplayer {
48
46
49
47
protected final Integer strokeColor ;
48
+ protected final float strokeWidth ;
50
49
51
50
public CircleBitmapDisplayer () {
52
51
this (null );
53
52
}
54
53
55
54
public CircleBitmapDisplayer (Integer strokeColor ) {
55
+ this (strokeColor , 0 );
56
+ }
57
+
58
+ public CircleBitmapDisplayer (Integer strokeColor , float strokeWidth ) {
56
59
this .strokeColor = strokeColor ;
60
+ this .strokeWidth = strokeWidth ;
57
61
}
58
62
59
63
@ Override
@@ -62,46 +66,53 @@ public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom)
62
66
throw new IllegalArgumentException ("ImageAware should wrap ImageView. ImageViewAware is expected." );
63
67
}
64
68
65
- imageAware .setImageDrawable (new RoundedDrawable (bitmap , strokeColor ));
69
+ imageAware .setImageDrawable (new CircleDrawable (bitmap , strokeColor , strokeWidth ));
66
70
}
67
71
68
- public static class RoundedDrawable extends Drawable {
72
+ public static class CircleDrawable extends Drawable {
69
73
70
74
protected float radius ;
71
75
72
- protected final RectF mRect = new RectF (),
73
- mBitmapRect ;
76
+ protected final RectF mRect = new RectF ();
77
+ protected final RectF mBitmapRect ;
74
78
protected final BitmapShader bitmapShader ;
75
79
protected final Paint paint ;
76
80
protected final Paint strokePaint ;
81
+ protected final float strokeWidth ;
82
+ protected float strokeRadius ;
77
83
78
- public RoundedDrawable (Bitmap bitmap , Integer strokeColor ) {
84
+ public CircleDrawable (Bitmap bitmap , Integer strokeColor , float strokeWidth ) {
79
85
radius = Math .min (bitmap .getWidth (), bitmap .getHeight ()) / 2 ;
80
86
81
87
bitmapShader = new BitmapShader (bitmap , Shader .TileMode .CLAMP , Shader .TileMode .CLAMP );
82
- mBitmapRect = new RectF (0 , 0 , bitmap .getWidth (), bitmap .getHeight ());
83
-
88
+ mBitmapRect = new RectF (0 , 0 , bitmap .getWidth (), bitmap .getHeight ());
89
+
84
90
paint = new Paint ();
85
91
paint .setAntiAlias (true );
86
92
paint .setShader (bitmapShader );
87
93
paint .setFilterBitmap (true );
88
94
paint .setDither (true );
89
- if (strokeColor == null ) strokePaint = null ;
90
- else {
95
+
96
+ if (strokeColor == null ) {
97
+ strokePaint = null ;
98
+ } else {
91
99
strokePaint = new Paint ();
92
100
strokePaint .setStyle (Paint .Style .STROKE );
93
101
strokePaint .setColor (strokeColor );
94
- strokePaint .setStrokeWidth (0.0F );
102
+ strokePaint .setStrokeWidth (strokeWidth );
95
103
strokePaint .setAntiAlias (true );
96
104
}
105
+ this .strokeWidth = strokeWidth ;
106
+ strokeRadius = radius - strokeWidth / 2 ;
97
107
}
98
108
99
109
@ Override
100
110
protected void onBoundsChange (Rect bounds ) {
101
111
super .onBoundsChange (bounds );
102
112
mRect .set (0 , 0 , bounds .width (), bounds .height ());
103
113
radius = Math .min (bounds .width (), bounds .height ()) / 2 ;
104
-
114
+ strokeRadius = radius - strokeWidth / 2 ;
115
+
105
116
// Resize the original bitmap to fit the new bound
106
117
Matrix shaderMatrix = new Matrix ();
107
118
shaderMatrix .setRectToRect (mBitmapRect , mRect , Matrix .ScaleToFit .FILL );
@@ -111,7 +122,9 @@ protected void onBoundsChange(Rect bounds) {
111
122
@ Override
112
123
public void draw (Canvas canvas ) {
113
124
canvas .drawCircle (radius , radius , radius , paint );
114
- if (strokePaint != null ) canvas .drawCircle (radius , radius , radius , strokePaint );
125
+ if (strokePaint != null ) {
126
+ canvas .drawCircle (radius , radius , strokeRadius , strokePaint );
127
+ }
115
128
}
116
129
117
130
@ Override
0 commit comments