How to achieve plumage effect in Android?
1 answer
You can think of the "pen effect" as a gradient gradient, with the alpha fading from 100% to 0%.
Android offers the RadialGradient class for this purpose. You will want to use a constructor where you can specify control points for the ray, as you want the fade to start at the edge, not in the middle.
One problem with Android's RadialGradient class is that it only supports perfect circles, not ovals. To compensate for this, we simply draw a perfect circle and scale up afterwards.
Sample 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;
}
Sample image (this is the oval "moon" in the upper left corner):
Bonus points for anyone who recognizes this background image.
+9
source to share