Last active
December 15, 2015 22:39
-
-
Save tikurahul/5334203 to your computer and use it in GitHub Desktop.
An implementation of -border-radius for Bitmaps in Android.
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
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