Created
February 20, 2017 09:15
-
-
Save farukcankaya/6296653acb8d68c4e68ed2c5001109c7 to your computer and use it in GitHub Desktop.
A Glide Transformation to create colored rounded border
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
/** | |
* Created by farukcankaya on 17/02/2017. | |
* powered: https://github.com/bumptech/glide/wiki/Transformations#bitmap-re-use | |
*/ | |
public class RoundedColoredCornerTransformation implements Transformation<Bitmap> { | |
private Context mContext; | |
private BitmapPool mBitmapPool; | |
private int mRadius; | |
private int mMargin; | |
private int mPadding; | |
private int mStrokeSize; | |
private int mStrokeColor; | |
private int mWidth; | |
private int mHeight; | |
private int mBackground; | |
public RoundedColoredCornerTransformation(Context context, Configuration configuration) { | |
this(context, Glide.get(context).getBitmapPool(), configuration); | |
} | |
public RoundedColoredCornerTransformation(Context context, BitmapPool pool, Configuration configuration) { | |
mContext = context; | |
mBitmapPool = pool; | |
mRadius = configuration.mRadiusRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mRadiusRes); | |
mMargin = configuration.mMarginRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mMarginRes); | |
mPadding = configuration.mPaddingRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mPaddingRes); | |
mStrokeSize = configuration.mStrokeSizeRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mStrokeSizeRes); | |
mStrokeColor = configuration.mStrokeColorRes == 0 ? 0 : ContextCompat.getColor(mContext, configuration.mStrokeColorRes); | |
mWidth = configuration.mWidthRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mWidthRes); | |
mHeight = configuration.mHeightRes == 0 ? 0 : (int) mContext.getResources().getDimension(configuration.mHeightRes); | |
mBackground = configuration.mBackgroundRes == 0 ? 0 : ContextCompat.getColor(mContext, configuration.mBackgroundRes); | |
} | |
@Override | |
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { | |
Bitmap source = resource.get(); | |
/** | |
* If width or height are greater than zero, we are assuming that image should be resized. | |
* Resize image to given dimensions. | |
*/ | |
if (mWidth > 0 || mHeight > 0) { | |
source = Bitmap.createScaledBitmap(resource.get(), mWidth, mHeight, false); | |
} | |
int width = source.getWidth(); | |
int height = source.getHeight(); | |
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, Bitmap.Config.ARGB_8888); | |
if (bitmap == null) { | |
bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); | |
} | |
Canvas canvas = new Canvas(bitmap); | |
// border | |
if (mStrokeSize > 0) { | |
Paint stroke = new Paint(); | |
stroke.setColor(mStrokeColor); | |
stroke.setStrokeWidth(mStrokeSize); | |
canvas.drawRoundRect(new RectF(mMargin, mMargin, width - mMargin, height - mMargin), mRadius, mRadius, stroke); | |
} | |
// Background | |
if (mBackground != 0) { | |
Paint background = new Paint(); | |
background.setColor(mBackground); | |
canvas.drawRoundRect(new RectF(mMargin + mStrokeSize, mMargin + mStrokeSize, | |
width - mMargin - mStrokeSize, height - mMargin - mStrokeSize), | |
mRadius, mRadius, background); | |
} | |
// image | |
float left = mMargin + mPadding + mStrokeSize; | |
float top = mMargin + mPadding + mStrokeSize; | |
float right = width - mMargin - mPadding - mStrokeSize; | |
float bottom = height - mMargin - mPadding - mStrokeSize; | |
Paint paint = new Paint(); | |
paint.setAntiAlias(true); | |
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); | |
canvas.drawRoundRect(new RectF(left, top, right, bottom), mRadius, mRadius, paint); | |
return BitmapResource.obtain(bitmap, mBitmapPool); | |
} | |
@Override | |
public String getId() { | |
return "RoundedColoredCornerTransformation{" + | |
"mRadiusRes=" + mRadius + | |
", mMarginRes=" + mMargin + | |
", mStrokeSizeRes=" + mStrokeSize + | |
", mWidthRes=" + mWidth + | |
", mHeightRes=" + mHeight + | |
'}'; | |
} | |
public static class Configuration { | |
private int mRadiusRes; | |
private int mMarginRes; | |
private int mPaddingRes; | |
private int mStrokeSizeRes; | |
private int mStrokeColorRes; | |
private int mWidthRes; | |
private int mHeightRes; | |
private int mBackgroundRes; | |
/** | |
* @param radius The desired resource identifier for radius | |
*/ | |
public Configuration(@DimenRes int radius) { | |
this.mRadiusRes = radius; | |
} | |
/** | |
* @param margin The desired resource identifier for margin | |
*/ | |
public Configuration setMargin(@DimenRes int margin) { | |
this.mMarginRes = margin; | |
return this; | |
} | |
/** | |
* @param padding The desired resource identifier for padding | |
*/ | |
public Configuration setPadding(@DimenRes int padding) { | |
this.mPaddingRes = padding; | |
return this; | |
} | |
/** | |
* @param strokeSize The desired resource identifier for stroke width | |
* @param strokeColor The desired resource identifier for color of stroke | |
*/ | |
public Configuration setStroke(@DimenRes int strokeSize, @ColorRes int strokeColor) { | |
this.mStrokeSizeRes = strokeSize; | |
this.mStrokeColorRes = strokeColor; | |
return this; | |
} | |
/** | |
* @param width The desired resource identifier for width | |
* @param height The desired resource identifier for height | |
*/ | |
public Configuration resize(@DimenRes int width, @DimenRes int height) { | |
this.mWidthRes = width; | |
this.mHeightRes = height; | |
return this; | |
} | |
/** | |
* @param backgroundColor The desired resource identifier for color of background | |
*/ | |
public Configuration setBackground(@ColorRes int backgroundColor) { | |
this.mBackgroundRes = backgroundColor; | |
return this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also give padding, margin and background to image.
Examples:

