How to mask Bitmap with LinearGradient shader properly?

android, bitmap, linear-gradients

Solution

Change your LinearGradient to this:

    LinearGradient shader = new LinearGradient(0,  h - GRADIENT_HEIGHT, 0, h, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.CLAMP);

Problem

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? 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!

Original source