Skip to content

Instantly share code, notes, and snippets.

@voghDev
Created September 6, 2017 12:16
Show Gist options
  • Select an option

  • Save voghDev/2ae0aa30069b14b22e3085624f6847ab to your computer and use it in GitHub Desktop.

Select an option

Save voghDev/2ae0aa30069b14b22e3085624f6847ab to your computer and use it in GitHub Desktop.
Picasso transformation that rounds up top-left and top-right corners. Rest of the image remains the same
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
public class TopRoundedTransformation implements com.squareup.picasso.Transformation {
@Override
public Bitmap transform(final Bitmap source) {
int width = source.getWidth();
int height = source.getHeight();
int x = 0;
int y = 0;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, width, height);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(width, height, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
paint.setColorFilter(new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.DST_OVER));
float r = 16f;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawRoundRect((float) x, (float) y, (float) width, (float) height, r, r, paint);
canvas.drawRect((float) x, r, (float) width, (float) height, paint);
} else {
canvas.drawRect(0f, r, width, height - r, paint);
canvas.drawRect(r, 0f, width - r, height, paint);
canvas.drawCircle(r, r, r, paint); // Upper-left
canvas.drawCircle(width - r, r, r, paint); // Upper-right
canvas.drawRect(0, height - r, r, height, paint); // Bottom-left
canvas.drawRect(width - r, height - r, width, height, paint); // Bottom-right
}
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "toprounded";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment