Last active
January 12, 2024 17:30
-
-
Save Sefford/3df1da2911d17241cd1b to your computer and use it in GitHub Desktop.
Transformation for Picasso that rounds different edges of the screen
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 android.graphics.RectF; | |
import com.squareup.picasso.Transformation; | |
import java.util.Arrays; | |
/** | |
* Transformation for rounded corners | |
* | |
* @author Saul Diaz <[email protected]> | |
*/ | |
public class PartialRoundedBorderTransformation implements Transformation { | |
/** | |
* Pixels to round by corner | |
*/ | |
private final float pixels; | |
/** | |
* Edges to round rect being 0 - left, 1 - top, 2 - right, 3 - bottom | |
*/ | |
private final boolean[] edges; | |
/** | |
* Creates a new instance of RoundedBorderTransformation | |
* | |
* @param pixels Pixels to round by corner | |
* @param edges Edges to trim from the picture | |
*/ | |
public PartialRoundedBorderTransformation(float pixels, boolean[] edges) { | |
this.pixels = pixels; | |
this.edges = Arrays.copyOf(edges, edges.length); | |
} | |
@Override | |
public Bitmap transform(Bitmap source) { | |
Bitmap output = Bitmap.createBitmap(source.getWidth(), source | |
.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
final int color = 0xff424242; | |
final Paint paint = new Paint(); | |
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight()); | |
final RectF rectF = new RectF(rect); | |
final float roundPx = pixels; | |
paint.setAntiAlias(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(color); | |
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); | |
if (edges[0]) { | |
canvas.drawRect(new RectF(0, 0, pixels, source.getHeight()), paint); | |
} | |
if (edges[1]) { | |
canvas.drawRect(new RectF(0, 0, source.getWidth(), pixels), paint); | |
} | |
if (edges[2]) { | |
canvas.drawRect(new RectF(source.getWidth() - pixels, 0, source.getWidth(), source.getHeight()), paint); | |
} | |
if (edges[3]) { | |
canvas.drawRect(new RectF(0, source.getHeight() - pixels, source.getWidth(), source.getHeight()), paint); | |
} | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); | |
canvas.drawBitmap(source, rect, rect, paint); | |
source.recycle(); | |
return output; | |
} | |
@Override | |
public String key() { | |
return "rounded_"+this.edges+"_"+this.pixels; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment