Created
August 3, 2015 05:21
-
-
Save voghDev/63ad04106284fa354632 to your computer and use it in GitHub Desktop.
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.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; | |
| import com.squareup.picasso.Transformation; | |
| public class RectRoundedBordersTransformation implements Transformation{ | |
| float radius = 10f; | |
| public RectRoundedBordersTransformation(float radius) { | |
| this.radius = radius; | |
| } | |
| @Override | |
| public Bitmap transform(final Bitmap source) { | |
| int size = Math.min(source.getWidth(), source.getHeight()); | |
| int w = source.getWidth() / 2; | |
| int h = source.getHeight() / 2; | |
| Bitmap squaredBitmap = Bitmap.createBitmap(source, 0, 0, w, h); | |
| if (squaredBitmap != source) { | |
| source.recycle(); | |
| } | |
| Bitmap bitmap = Bitmap.createBitmap(w, h, 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 sf = (float)size; | |
| if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ | |
| canvas.drawRoundRect(0f, 0f, w, h, radius, radius, paint); | |
| }else{ | |
| canvas.drawRect(0f, radius, w, h-radius, paint ); | |
| canvas.drawRect(radius, 0f, w-radius, h, paint ); | |
| canvas.drawCircle(radius, radius, radius, paint); // Upper-left | |
| canvas.drawCircle(w-radius, radius, radius, paint); // Upper-right | |
| canvas.drawCircle(radius, h-radius, radius, paint); // Bottom-left | |
| canvas.drawCircle(w-radius, h-radius, radius, paint); // Bottom-right | |
| } | |
| squaredBitmap.recycle(); | |
| return bitmap; | |
| } | |
| @Override | |
| public String key() { | |
| return "rect-rounded(radius="+radius+")"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment