Skip to content

Instantly share code, notes, and snippets.

@twocity
Created November 19, 2013 11:15
Show Gist options
  • Save twocity/7543887 to your computer and use it in GitHub Desktop.
Save twocity/7543887 to your computer and use it in GitHub Desktop.
public class RoundedDrawable extends Drawable {
private static final boolean USE_VIGNETTE = true;
private RectF mRect = new RectF();
private final Paint paint;
private final int mRadius;
private final BitmapShader bitmapShader;
public RoundedDrawable(Bitmap bitmap, int radius) {
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
mRadius = radius;
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRect.set(0, 0, bounds.width(), bounds.height());
if (USE_VIGNETTE) {
RadialGradient vignette = new RadialGradient(
mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f,
new int[]{0, 0, 0x7f000000}, new float[]{0.0f, 0.7f, 1.0f},
Shader.TileMode.CLAMP);
Matrix oval = new Matrix();
oval.setScale(1.0f, 0.7f);
vignette.setLocalMatrix(oval);
paint.setShader(
new ComposeShader(bitmapShader, vignette, PorterDuff.Mode.SRC_OVER));
}
}
@Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRect, mRadius, mRadius, paint);
// canvas.drawCircle(mRect.right / 2, mRect.bottom / 2, mRadius, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment