Skip to content

Instantly share code, notes, and snippets.

@javipacheco
Created June 17, 2014 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javipacheco/8a1388ba570e3c67742f to your computer and use it in GitHub Desktop.
Save javipacheco/8a1388ba570e3c67742f to your computer and use it in GitHub Desktop.
RoundedCenterBitmapDisplayer for UniversalImage Library
public class RoundedCenterBitmapDisplayer implements BitmapDisplayer {
protected final int cornerRadius;
protected final int size;
public RoundedCenterBitmapDisplayer(Context context, int cornerRadiusPixels) {
this.cornerRadius = cornerRadiusPixels;
this.size = (int) context.getResources().getDimension(R.dimen.size_popup_icon);
}
@Override
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
if (!(imageAware instanceof ImageViewAware)) {
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
}
imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, size));
}
protected static class RoundedDrawable extends Drawable {
protected final float cornerRadius;
protected final RectF mRect = new RectF();
protected final BitmapShader bitmapShader;
protected final Paint paint;
RoundedDrawable(Bitmap bitmap, int cornerRadius, int size) {
this.cornerRadius = cornerRadius;
try {
bitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
} catch (OutOfMemoryError outOfMemoryError) {
Crashlytics.logException(outOfMemoryError);
} catch (Exception e) {
Crashlytics.logException(e);
}
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRect.set(0, 0, bounds.width(), bounds.height());
}
@Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment