Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active March 24, 2017 22:22
Show Gist options
  • Save kaushikgopal/aac4d0f9ed0d03cc0291 to your computer and use it in GitHub Desktop.
Save kaushikgopal/aac4d0f9ed0d03cc0291 to your computer and use it in GitHub Desktop.
A set of Picasso Transformation Examples
package co.kaush.mystarterapp.myappmodule.ui.transformations;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import com.squareup.picasso.Transformation;
public class CircleTransformation
implements Transformation {
@Override
public Bitmap transform(Bitmap sourceBitmap) {
int smallerOfHeightAndWidth = Math.min(sourceBitmap.getHeight(), sourceBitmap.getWidth());
int x_firstSourcePixel = (sourceBitmap.getWidth() - smallerOfHeightAndWidth) / 2;
int y_firstSourcePixel = (sourceBitmap.getHeight() - smallerOfHeightAndWidth) / 2;
float radius = smallerOfHeightAndWidth / 2f;
Bitmap sourceBitmapSquare = Bitmap.createBitmap(sourceBitmap,
x_firstSourcePixel,
y_firstSourcePixel,
smallerOfHeightAndWidth,
smallerOfHeightAndWidth);
// recycle original bitmap if it's not the same as we no longer need it
if (sourceBitmapSquare != sourceBitmap) {
sourceBitmap.recycle();
}
// create the output bitmap structure
Bitmap outputBitmap = Bitmap.createBitmap(smallerOfHeightAndWidth,
smallerOfHeightAndWidth,
sourceBitmap.getConfig());
// prepare canvas + paint with all ingredients
BitmapShader shader = new BitmapShader(sourceBitmapSquare,
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
Canvas canvas = new Canvas(outputBitmap);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
canvas.drawCircle(radius, radius, radius, paint);
// recycle sourceBitmapSquare as we no longer need it
sourceBitmapSquare.recycle();
return outputBitmap;
}
@Override
public String key() {
return "co.kaush.mystarterapp.myappmodule.ui.transformations.CircleTransformation";
}
}
package co.kaush.mystarterapp.myappmodule.ui.transformations;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import com.squareup.picasso.Transformation;
public class VignetteTransformation
implements Transformation {
private final TYPE _type;
public enum TYPE {BOTTOM, TOP}
public VignetteTransformation(TYPE type) {
_type = type;
}
@Override
public Bitmap transform(Bitmap source) {
// create the output bitmap structure
Bitmap outputBitmap = Bitmap.createBitmap(source.getWidth(),
source.getHeight(),
source.getConfig());
// prepare canvas + paint with all ingredients
ComposeShader composeShader = _getVignetteShader(source);
Canvas canvas = new Canvas(outputBitmap);
Paint paint = new Paint();
paint.setShader(composeShader);
paint.setAntiAlias(true);
// paint on canvas which holds to be rendered bitmap
canvas.drawPaint(paint);
// recycle the original bitmap which we no longer require
source.recycle();
return outputBitmap;
}
@Override
public String key() {
return "co.kaush.mystarterapp.myappmodule.ui.transformations.VignetteTransformation";
}
private ComposeShader _getVignetteShader(Bitmap source) {// create the bitmap shader
// create a bitmap shader
Shader bitmapShader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
// create the linear gradient shader
Shader linearGradientShader = _getLinearGradient(source);
// create a shader that combines both effects
return new ComposeShader(bitmapShader, linearGradientShader, PorterDuff.Mode.DST_OUT);
}
private LinearGradient _getLinearGradient(Bitmap source) {
int x0 = 0, y0 = 0,
x1 = 0, y1 = source.getHeight(),
// The colors here are actually irrelevant
fromColor = Color.RED, // This translates to the image showing
toColor = Color.TRANSPARENT; // This translates to background color
switch (_type) {
case BOTTOM:
fromColor = Color.TRANSPARENT;
toColor = Color.RED;
break;
}
return new LinearGradient(x0, y0, x1, y1, fromColor, toColor, Shader.TileMode.MIRROR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment