Skip to content

Instantly share code, notes, and snippets.

@miao1007
Last active October 7, 2023 05:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save miao1007/3f7e9b5f011fc188eba6 to your computer and use it in GitHub Desktop.
Save miao1007/3f7e9b5f011fc188eba6 to your computer and use it in GitHub Desktop.
Gradient Transformation For Picasso
/**
* Created by Miao1007 on 2/2/15.
*
* Original Code: https://gist.github.com/miao1007/3f7e9b5f011fc188eba6
*/
public class GradientTransformation implements Transformation {
int startColor = Color.argb(240,0,0,0);
int endColor = Color.TRANSPARENT;
@Override public Bitmap transform(Bitmap source) {
int x = source.getWidth();
int y = source.getHeight();
Bitmap grandientBitmap = source.copy(source.getConfig(), true);
Canvas canvas = new Canvas(grandientBitmap);
//left-top == (0,0) , right-bottom(x,y);
LinearGradient grad =
new LinearGradient(x/2, y, x/2, y/2, startColor, endColor, Shader.TileMode.CLAMP);
Paint p = new Paint(Paint.DITHER_FLAG);
p.setShader(null);
p.setDither(true);
p.setFilterBitmap(true);
p.setShader(grad);
canvas.drawPaint(p);
source.recycle();
return grandientBitmap;
}
@Override public String key() {
return "Gradient";
}
}
Picasso.with(context)
.load(url)
.transform(new GradientTransformation())
.into(imageview);
@troZee
Copy link

troZee commented Aug 11, 2016

This gradient color is not smooth

@erudonja1
Copy link

This is good one, thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment