Skip to content

Instantly share code, notes, and snippets.

@tomredman
Last active April 13, 2016 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomredman/05c4a20942cf35c3cf33fdf41bcee4fb to your computer and use it in GitHub Desktop.
Save tomredman/05c4a20942cf35c3cf33fdf41bcee4fb to your computer and use it in GitHub Desktop.
ColorFadeTransform for Picasso
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.ColorInt;
import com.squareup.picasso.Transformation;
/**
* -----------------------
* ColorFadeTransform
* -----------------------
*
* @author Tom Redman
* 2016-04-12
*
* @description Will apply a colored fade to an image loaded with Picasso.
* The color will fade from the bottom of the image upward to the percentage
* specificed in fadeHeightPercentage.
*
* @usage Picasso.with(context).load(imageUrl).transform(new ColorFadeTransform(Color.RED, 25)).into(imageView);
*/
public class ColorFadeTransform implements Transformation {
private int fadeColor = 0xFF000000;
private int fadeColorAlpha = 0x000000;
private int fadeHeightPercentage = 50;
public ColorFadeTransform() {
this(Color.BLACK, 50);
}
public ColorFadeTransform(@ColorInt int fadeColor, int fadeHeightPercentage) {
int red = (fadeColor >> 16) & 0xFF;
int green = (fadeColor >> 8) & 0xFF;
int blue = fadeColor & 0xFF;
this.fadeColor = Color.argb(255, red, green, blue);
this.fadeColorAlpha = Color.argb(0, red, green, blue);
this.fadeHeightPercentage = fadeHeightPercentage;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap resultBitmap = Bitmap.createBitmap(source).copy(Bitmap.Config.ARGB_8888, true);
source.recycle();
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, null);
int gradientHeight = (int)(resultBitmap.getHeight() / (100.0f / (double)fadeHeightPercentage));
Paint paint = new Paint();
LinearGradient linearGradient = new LinearGradient(0, resultBitmap.getHeight() - gradientHeight, 0, resultBitmap.getHeight(), fadeColorAlpha, fadeColor, Shader.TileMode.CLAMP);
paint.setShader(linearGradient);
canvas.drawRect(0, resultBitmap.getHeight() - gradientHeight, resultBitmap.getWidth(), resultBitmap.getHeight(), paint);
return resultBitmap;
}
@Override
public String key() {
return "ColorFadeTransform";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment