mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-06-10 00:24:36 +08:00
Merge pull request #23 from kaelaela/feature/upgrade_rounded_transformation
Add round corner type
This commit is contained in:
commit
62f3ee6b4a
@ -31,19 +31,30 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
|||||||
|
|
||||||
public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
||||||
|
|
||||||
private BitmapPool mBitmapPool;
|
public enum CornerType {
|
||||||
|
ALL,
|
||||||
private int radius;
|
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
|
||||||
private int margin;
|
TOP, BOTTOM, LEFT, RIGHT,
|
||||||
|
OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
|
||||||
public RoundedCornersTransformation(Context context, int radius, int margin) {
|
DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
|
||||||
this(Glide.get(context).getBitmapPool(), radius, margin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RoundedCornersTransformation(BitmapPool pool, int radius, int margin) {
|
private BitmapPool mBitmapPool;
|
||||||
mBitmapPool = pool;
|
private int mRadius;
|
||||||
this.radius = radius;
|
private int mDiameter;
|
||||||
this.margin = margin;
|
private int mMargin;
|
||||||
|
private CornerType mCornerType;
|
||||||
|
|
||||||
|
public RoundedCornersTransformation(Context context, int radius, int margin) {
|
||||||
|
this(context, radius, margin, CornerType.ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoundedCornersTransformation(Context context, int radius, int margin, CornerType cornerType) {
|
||||||
|
mBitmapPool = Glide.get(context).getBitmapPool();
|
||||||
|
mRadius = radius;
|
||||||
|
mDiameter = mRadius * 2;
|
||||||
|
mMargin = margin;
|
||||||
|
mCornerType = cornerType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -62,13 +73,150 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
|||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
||||||
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin), radius, radius,
|
drawRoundRect(canvas, paint, width, height);
|
||||||
paint);
|
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getId() {
|
private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
|
||||||
return "RoundedTransformation(radius=" + radius + ", margin=" + margin + ")";
|
float right = width - mMargin;
|
||||||
|
float bottom = height - mMargin;
|
||||||
|
|
||||||
|
switch (mCornerType) {
|
||||||
|
case ALL:
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
|
||||||
|
break;
|
||||||
|
case TOP_LEFT:
|
||||||
|
drawTopLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case TOP_RIGHT:
|
||||||
|
drawTopRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case BOTTOM_LEFT:
|
||||||
|
drawBottomLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case BOTTOM_RIGHT:
|
||||||
|
drawBottomRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case TOP:
|
||||||
|
drawTopRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case BOTTOM:
|
||||||
|
drawBottomRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case LEFT:
|
||||||
|
drawLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
drawRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case OTHER_TOP_LEFT:
|
||||||
|
drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case OTHER_TOP_RIGHT:
|
||||||
|
drawOtherTopRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case OTHER_BOTTOM_LEFT:
|
||||||
|
drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case OTHER_BOTTOM_RIGHT:
|
||||||
|
drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case DIAGONAL_FROM_TOP_LEFT:
|
||||||
|
drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
case DIAGONAL_FROM_TOP_RIGHT:
|
||||||
|
drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
|
||||||
|
canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
|
||||||
|
canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
|
||||||
|
canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom), mRadius, mRadius, paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
|
||||||
|
canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getId() {
|
||||||
|
return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user