Last active
August 29, 2015 14:15
-
-
Save fedefernandez/60d9339983ad3f935059 to your computer and use it in GitHub Desktop.
Picasso transformation for circular crop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.Paint | |
import android.graphics.PorterDuff | |
import android.graphics.PorterDuffXfermode | |
import android.graphics.Rect | |
import com.squareup.picasso.Transformation | |
class CircularTransformation(size: Int) extends Transformation { | |
val radius = Math.ceil(size / 2).toInt | |
def transform(source: Bitmap): Bitmap = { | |
val output: Bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888) | |
val canvas: Canvas = new Canvas(output) | |
val color: Int = 0xff424242 | |
val paint: Paint = new Paint | |
val rect: Rect = new Rect(0, 0, source.getWidth, source.getHeight) | |
val target: Rect = new Rect(0, 0, size, size) | |
paint.setAntiAlias(true) | |
canvas.drawARGB(0, 0, 0, 0) | |
paint.setColor(color) | |
canvas.drawCircle(radius, radius, radius, paint) | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) | |
canvas.drawBitmap(source, rect, target, paint) | |
source.recycle() | |
output | |
} | |
def key: String = { | |
s"radius-$size" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment