Skip to content

Commit d8f0a58

Browse files
committed
Improved CircleBitmapDisplayer
1 parent 53dee27 commit d8f0a58

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

library/src/main/java/com/nostra13/universalimageloader/core/display/CircleBitmapDisplayer.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright 2011-2014 Sergey Tarasevich
2+
* Copyright 2015 Sergey Tarasevich
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,25 +35,29 @@
3535
* Can display bitmap cropped by a circle. This implementation works only with ImageViews wrapped
3636
* in ImageViewAware.
3737
* <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 />
4338
* If this implementation doesn't meet your needs then consider
4439
* <a href="https://github.com/vinc3m1/RoundedImageView">RoundedImageView</a> or
4540
* <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
4644
*/
4745
public class CircleBitmapDisplayer implements BitmapDisplayer {
4846

4947
protected final Integer strokeColor;
48+
protected final float strokeWidth;
5049

5150
public CircleBitmapDisplayer() {
5251
this(null);
5352
}
5453

5554
public CircleBitmapDisplayer(Integer strokeColor) {
55+
this(strokeColor, 0);
56+
}
57+
58+
public CircleBitmapDisplayer(Integer strokeColor, float strokeWidth) {
5659
this.strokeColor = strokeColor;
60+
this.strokeWidth = strokeWidth;
5761
}
5862

5963
@Override
@@ -62,46 +66,53 @@ public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom)
6266
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
6367
}
6468

65-
imageAware.setImageDrawable(new RoundedDrawable(bitmap, strokeColor));
69+
imageAware.setImageDrawable(new CircleDrawable(bitmap, strokeColor, strokeWidth));
6670
}
6771

68-
public static class RoundedDrawable extends Drawable {
72+
public static class CircleDrawable extends Drawable {
6973

7074
protected float radius;
7175

72-
protected final RectF mRect = new RectF(),
73-
mBitmapRect;
76+
protected final RectF mRect = new RectF();
77+
protected final RectF mBitmapRect;
7478
protected final BitmapShader bitmapShader;
7579
protected final Paint paint;
7680
protected final Paint strokePaint;
81+
protected final float strokeWidth;
82+
protected float strokeRadius;
7783

78-
public RoundedDrawable(Bitmap bitmap, Integer strokeColor) {
84+
public CircleDrawable(Bitmap bitmap, Integer strokeColor, float strokeWidth) {
7985
radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;
8086

8187
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+
8490
paint = new Paint();
8591
paint.setAntiAlias(true);
8692
paint.setShader(bitmapShader);
8793
paint.setFilterBitmap(true);
8894
paint.setDither(true);
89-
if (strokeColor == null) strokePaint = null;
90-
else {
95+
96+
if (strokeColor == null) {
97+
strokePaint = null;
98+
} else {
9199
strokePaint = new Paint();
92100
strokePaint.setStyle(Paint.Style.STROKE);
93101
strokePaint.setColor(strokeColor);
94-
strokePaint.setStrokeWidth(0.0F);
102+
strokePaint.setStrokeWidth(strokeWidth);
95103
strokePaint.setAntiAlias(true);
96104
}
105+
this.strokeWidth = strokeWidth;
106+
strokeRadius = radius - strokeWidth / 2;
97107
}
98108

99109
@Override
100110
protected void onBoundsChange(Rect bounds) {
101111
super.onBoundsChange(bounds);
102112
mRect.set(0, 0, bounds.width(), bounds.height());
103113
radius = Math.min(bounds.width(), bounds.height()) / 2;
104-
114+
strokeRadius = radius - strokeWidth / 2;
115+
105116
// Resize the original bitmap to fit the new bound
106117
Matrix shaderMatrix = new Matrix();
107118
shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
@@ -111,7 +122,9 @@ protected void onBoundsChange(Rect bounds) {
111122
@Override
112123
public void draw(Canvas canvas) {
113124
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+
}
115128
}
116129

117130
@Override

sample/src/main/java/com/nostra13/universalimageloader/sample/fragment/ImageGalleryFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import android.widget.BaseAdapter;
2828
import android.widget.Gallery;
2929
import android.widget.ImageView;
30+
3031
import com.nostra13.universalimageloader.core.DisplayImageOptions;
3132
import com.nostra13.universalimageloader.core.ImageLoader;
33+
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
3234
import com.nostra13.universalimageloader.sample.Constants;
3335
import com.nostra13.universalimageloader.sample.R;
3436
import com.nostra13.universalimageloader.sample.activity.SimpleImageActivity;
@@ -82,6 +84,7 @@ private static class ImageAdapter extends BaseAdapter {
8284
.cacheOnDisk(true)
8385
.considerExifParams(true)
8486
.bitmapConfig(Bitmap.Config.RGB_565)
87+
.displayer(new RoundedBitmapDisplayer(20))
8588
.build();
8689
}
8790

@@ -110,4 +113,4 @@ public View getView(int position, View convertView, ViewGroup parent) {
110113
return imageView;
111114
}
112115
}
113-
}
116+
}

sample/src/main/java/com/nostra13/universalimageloader/sample/fragment/ImageListFragment.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.content.Context;
1919
import android.graphics.Bitmap;
20+
import android.graphics.Color;
2021
import android.os.Bundle;
2122
import android.view.LayoutInflater;
2223
import android.view.View;
@@ -27,10 +28,11 @@
2728
import android.widget.ImageView;
2829
import android.widget.ListView;
2930
import android.widget.TextView;
31+
3032
import com.nostra13.universalimageloader.core.DisplayImageOptions;
3133
import com.nostra13.universalimageloader.core.ImageLoader;
34+
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
3235
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
33-
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
3436
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
3537
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
3638
import com.nostra13.universalimageloader.sample.Constants;
@@ -86,7 +88,8 @@ private static class ImageAdapter extends BaseAdapter {
8688
.cacheInMemory(true)
8789
.cacheOnDisk(true)
8890
.considerExifParams(true)
89-
.displayer(new RoundedBitmapDisplayer(20)).build();
91+
.displayer(new CircleBitmapDisplayer(Color.WHITE, 5))
92+
.build();
9093
}
9194

9295
@Override
@@ -147,4 +150,4 @@ public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
147150
}
148151
}
149152
}
150-
}
153+
}

0 commit comments

Comments
 (0)