I'd like to draw a Bitmap
on a Canvas
, with a (linear) alpha gradient applied. The important point is that I don't want to overlay the image with any other color; the background (coming from the View
s behind the View
that I'd be drawing this Canvas
to) should just "shine through". To illustrate, my goal would be something like this (the checkerboard pattern represents the View
behind)
One would think that I could do something like this:
Bitmap bitmap = ...;
Paint paint = new Paint();
paint.setShader(new LinearGradient(0, 0, 100, 0, FROM, TO, Shader.TileMode.CLAMP));
canvas.drawBitmap(bitmap, 0, 0, paint);
but LinearGradient
's FROM
and TO
arguments here would need to be colors, not alpha values; so there's no way that I see to specify that e.g. FROM
should be fully transparent and TO
should be fully opaque (without applying any color overlay).
Based on this answer about masking, I was able to do this using a secondary off-screen canvas:
use a
ComposeShader
, like this: