-
-
Save javipacheco/8a1388ba570e3c67742f to your computer and use it in GitHub Desktop.
RoundedCenterBitmapDisplayer for UniversalImage Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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