Skip to content

Commit 53dee27

Browse files
committed
Merge pull request nostra13#1061 from Qualtagh/master
Better rendering of scaled images.
2 parents 0554b8b + 5f7455c commit 53dee27

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*******************************************************************************
2+
* Copyright 2011-2014 Sergey Tarasevich
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
package com.nostra13.universalimageloader.core.display;
17+
18+
import android.graphics.Bitmap;
19+
import android.graphics.BitmapShader;
20+
import android.graphics.Canvas;
21+
import android.graphics.ColorFilter;
22+
import android.graphics.Matrix;
23+
import android.graphics.Paint;
24+
import android.graphics.PixelFormat;
25+
import android.graphics.Rect;
26+
import android.graphics.RectF;
27+
import android.graphics.Shader;
28+
import android.graphics.drawable.Drawable;
29+
30+
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
31+
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
32+
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
33+
34+
/**
35+
* Can display bitmap cropped by a circle. This implementation works only with ImageViews wrapped
36+
* in ImageViewAware.
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+
* If this implementation doesn't meet your needs then consider
44+
* <a href="https://github.com/vinc3m1/RoundedImageView">RoundedImageView</a> or
45+
* <a href="https://github.com/Pkmmte/CircularImageView">CircularImageView</a> projects for usage.
46+
*/
47+
public class CircleBitmapDisplayer implements BitmapDisplayer {
48+
49+
protected final Integer strokeColor;
50+
51+
public CircleBitmapDisplayer() {
52+
this(null);
53+
}
54+
55+
public CircleBitmapDisplayer(Integer strokeColor) {
56+
this.strokeColor = strokeColor;
57+
}
58+
59+
@Override
60+
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
61+
if (!(imageAware instanceof ImageViewAware)) {
62+
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
63+
}
64+
65+
imageAware.setImageDrawable(new RoundedDrawable(bitmap, strokeColor));
66+
}
67+
68+
public static class RoundedDrawable extends Drawable {
69+
70+
protected float radius;
71+
72+
protected final RectF mRect = new RectF(),
73+
mBitmapRect;
74+
protected final BitmapShader bitmapShader;
75+
protected final Paint paint;
76+
protected final Paint strokePaint;
77+
78+
public RoundedDrawable(Bitmap bitmap, Integer strokeColor) {
79+
radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;
80+
81+
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
82+
mBitmapRect = new RectF (0, 0, bitmap.getWidth(), bitmap.getHeight());
83+
84+
paint = new Paint();
85+
paint.setAntiAlias(true);
86+
paint.setShader(bitmapShader);
87+
paint.setFilterBitmap(true);
88+
paint.setDither(true);
89+
if (strokeColor == null) strokePaint = null;
90+
else {
91+
strokePaint = new Paint();
92+
strokePaint.setStyle(Paint.Style.STROKE);
93+
strokePaint.setColor(strokeColor);
94+
strokePaint.setStrokeWidth(0.0F);
95+
strokePaint.setAntiAlias(true);
96+
}
97+
}
98+
99+
@Override
100+
protected void onBoundsChange(Rect bounds) {
101+
super.onBoundsChange(bounds);
102+
mRect.set(0, 0, bounds.width(), bounds.height());
103+
radius = Math.min(bounds.width(), bounds.height()) / 2;
104+
105+
// Resize the original bitmap to fit the new bound
106+
Matrix shaderMatrix = new Matrix();
107+
shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
108+
bitmapShader.setLocalMatrix(shaderMatrix);
109+
}
110+
111+
@Override
112+
public void draw(Canvas canvas) {
113+
canvas.drawCircle(radius, radius, radius, paint);
114+
if (strokePaint != null) canvas.drawCircle(radius, radius, radius, strokePaint);
115+
}
116+
117+
@Override
118+
public int getOpacity() {
119+
return PixelFormat.TRANSLUCENT;
120+
}
121+
122+
@Override
123+
public void setAlpha(int alpha) {
124+
paint.setAlpha(alpha);
125+
}
126+
127+
@Override
128+
public void setColorFilter(ColorFilter cf) {
129+
paint.setColorFilter(cf);
130+
}
131+
}
132+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
8181
paint = new Paint();
8282
paint.setAntiAlias(true);
8383
paint.setShader(bitmapShader);
84+
paint.setFilterBitmap(true);
85+
paint.setDither(true);
8486
}
8587

8688
@Override

0 commit comments

Comments
 (0)