Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mask Bitmap with LinearGradient shader properly?

I'm trying to mask Bitmap with gradient alpha at bottom. Gradient size are fixed and independed of Bitmap size. But it draws incorrect: bottom of gradient at top, than top. What's wrong?

enter image description here

There is sample code:

final int GRADIENT_HEIGHT = 32;

public Bitmap addGradient(Bitmap src) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap overlay = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);

    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, 0, 0, GRADIENT_HEIGHT, 0xFFFFFFFF, 0x00FFFFFF, TileMode.REPEAT);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, h - GRADIENT_HEIGHT, w, h, paint);

    return overlay;
}

Thanks!

like image 729
Ganster41 Avatar asked May 14 '14 14:05

Ganster41


1 Answers

Change your LinearGradient to this:

    LinearGradient shader = new LinearGradient(0,  h - GRADIENT_HEIGHT, 0, h, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.CLAMP);
like image 136
Jens Zalzala Avatar answered Sep 17 '22 04:09

Jens Zalzala