Created
May 22, 2014 19:52
-
-
Save Sefford/1be809708ca2fb64353a to your computer and use it in GitHub Desktop.
Clips an image by the diagonal, creating just the right-bottom corner
This file contains hidden or 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.Path; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.Rect; | |
import com.squareup.picasso.Transformation; | |
/** | |
* Clips an image by the diagonal, creating just the right-bottom corner | |
* | |
* @author Saul Diaz <[email protected]> | |
*/ | |
public class TriangleClipTransformation implements Transformation { | |
@Override | |
public Bitmap transform(Bitmap source) { | |
int width = source.getWidth(); | |
int height = source.getHeight(); | |
Bitmap output = Bitmap.createBitmap(width, height, 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, width, height); | |
final Path path = new Path(); | |
path.moveTo(0, height); | |
path.lineTo(width, height); | |
path.lineTo(width, 0); | |
path.moveTo(0, height); | |
paint.setAntiAlias(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(color); | |
paint.setStyle(Paint.Style.FILL); | |
canvas.drawPath(path, paint); | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); | |
canvas.drawBitmap(source, rect, rect, paint); | |
source.recycle(); | |
return output; | |
} | |
@Override | |
public String key() { | |
return "_clipped"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment