How to put an alpha gradient on ImageView with Picasso

0

I'm trying to put a real transparency on an ImageView where the image is loaded with Picasso from the Internet. I want the picture to be 100% transparent on its bottom and 0% transparency on its top with a linear gradient. I tried this with the "Transformation" from Picasso and Canvas with the following code:

In the Main Activity:

    Picasso.get()
            .load("https://i.pinimg.com/originals/df/cb/10/dfcb108d48df5e526f5c1c019a07258d.jpg")
            .transform(GradientTransformation())
            .into(imageview)

This is the GradientTransformation:

public class GradientTransformation implements Transformation {

    @Override public Bitmap transform(Bitmap source) {

    int x = source.getWidth();
    int y = source.getHeight();

    Canvas offscreen = new Canvas(mask);
    offscreen.drawBitmap(source, 0, 0, null);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    paint.setShader(new LinearGradient(0, 0, 100, 0, 0x00000000, 0xFF000000, Shader.TileMode.CLAMP));
    offscreen.drawRect(0, 0, x, y, paint);
    return Canvas.drawBitmap(mask, 0, 0, null);
    }

 @Override public String key() {
    return "Gradient";
 }
}

Is the Code ok? How do I setup the mask (as I couldn't figure out how to set it up the right way)?

android
canvas
bitmap
gradient
picasso
asked on Stack Overflow Dec 25, 2019 by Los Kayos

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0