I am trying to create an Oval Bitmap and I need to get a feather effect around the margins,
Does anyone have any idea How I can achieve this ?
Thanks.
You could consider the 'feather effect' as a gradial gradient, with the alpha fading from 100% to 0%.
Android offers the RadialGradient class for this purpose. You'll want to use the constructor where you can specify the control points for the radient, as you'll want the fading to start near the edge, not in the middle.
The one problem with Android's RadialGradient class is that it only supports perfect circles, not for ovals. To compensate for this, we'll just draw a perfect circle and scale afterwards.
Example code:
private Bitmap makeFeatheredOval(int width, int height) {
// Determine largest dimension, use for rectangle.
int size = Math.max( width, height);
RadialGradient gradient = new RadialGradient(size / 2, size / 2, size / 2,
new int[] {0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF},
new float[] {0.0f, 0.8f, 1.0f},
android.graphics.Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(gradient);
Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(size / 2, size / 2, size / 2, paint);
// Scale the bitmap, creating an oval
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
return bitmap;
}
Example image (it's the oval "moon" in the upper left corner):
Bonus points for everyone who recognizes that backdrop image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With