Skip to content

Instantly share code, notes, and snippets.

@tikurahul
Last active December 15, 2015 22:39
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 tikurahul/5334203 to your computer and use it in GitHub Desktop.
Save tikurahul/5334203 to your computer and use it in GitHub Desktop.
An implementation of -border-radius for Bitmaps in Android.
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.BitmapShader;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
public class RoundedRectBitmapDrawable extends BitmapDrawable {
private final BitmapShader bitmapShader;
private final Paint p;
private final RectF rect;
private final float borderRadius;
public RoundedRectBitmapDrawable(final Resources resources, final Bitmap bitmap) {
super(resources, bitmap);
bitmapShader = new BitmapShader(getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
final Bitmap b = getBitmap();
p = getPaint();
p.setAntiAlias(true);
p.setShader(bitmapShader);
final int w = b.getWidth();
final int h = b.getHeight();
rect = new RectF(0, 0, w, h);
borderRadius = 0.15f * Math.min(w, h);
}
@Override
public void draw(final Canvas canvas) {
canvas.drawRoundRect(rect, borderRadius, borderRadius, p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment