Skip to content

Instantly share code, notes, and snippets.

@nikartm
Created January 13, 2017 11:26
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 nikartm/21e2ac2a43e1c3b92386512e2f1f46d4 to your computer and use it in GitHub Desktop.
Save nikartm/21e2ac2a43e1c3b92386512e2f1f46d4 to your computer and use it in GitHub Desktop.
Get bitmap shader circle img
// Get bitmap shader circle img
public static Bitmap getCircleMaskedBitmapShader(Bitmap source, int radius) {
if (source == null) { return null; }
int diam = radius << 1;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap scaledBitmap = scaleTo(source, diam);
final Shader shader = new BitmapShader(scaledBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
Bitmap targetBitmap = Bitmap.createBitmap(diam, diam, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
canvas.drawCircle(radius, radius, radius, paint);
return targetBitmap;
}
private static Bitmap scaleTo(Bitmap source, int size) {
int destWidth = source.getWidth();
int destHeight = source.getHeight();
destHeight = destHeight * size / destWidth;
destWidth = size;
if (destHeight < size) {
destWidth = destWidth * size / destHeight;
destHeight = size;
}
Bitmap destBitmap = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(destBitmap);
canvas.drawBitmap(source, new Rect(0, 0, source.getWidth(), source.getHeight()), new Rect(0, 0, destWidth, destHeight), new Paint(Paint.ANTI_ALIAS_FLAG));
return destBitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment