Skip to content

Instantly share code, notes, and snippets.

@jaganjan
Created February 11, 2016 07:50
Show Gist options
  • Save jaganjan/826aef64930c099a32e9 to your computer and use it in GitHub Desktop.
Save jaganjan/826aef64930c099a32e9 to your computer and use it in GitHub Desktop.
picasso rounded corner transformation for xamarin android
public class RoundedCornersTransformation : Java.Lang.Object, ITransformation
{
public enum CornerType
{
All,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Top,
Bottom,
Left,
Right,
OtherTopLeft,
OtherTopRight,
OtherBottomLeft,
OtherBottomRight,
DiagonalFromTopLeft,
DiagonalFromTopRight
}
private int _mRadius;
private int _mDiameter;
private int _mMargin;
private CornerType _mCornerType;
public RoundedCornersTransformation(int radius, int margin) : this(radius, margin, CornerType.All)
{
}
public RoundedCornersTransformation(int radius, int margin, CornerType cornerType)
{
_mRadius = radius;
_mDiameter = radius*2;
_mMargin = margin;
_mCornerType = cornerType;
}
public Bitmap Transform(Bitmap source)
{
int width = source.Width;
int height = source.Height;
Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint {AntiAlias = true};
paint.SetShader(new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp));
DrawRoundRect(canvas, paint, width, height);
// source.Recycle();
if (source != bitmap)
{
source.Recycle();
}
return bitmap;
}
private void DrawRoundRect(Canvas canvas, Paint paint, float width, float height)
{
float right = width - _mMargin;
float bottom = height - _mMargin;
switch (_mCornerType)
{
case CornerType.All:
canvas.DrawRoundRect(new RectF(_mMargin, _mMargin, right, bottom), _mRadius, _mRadius, paint);
break;
case CornerType.TopLeft:
DrawTopLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.TopRight:
DrawTopRightRoundRect(canvas, paint, right, bottom);
break;
case CornerType.BottomLeft:
DrawBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.BottomRight:
DrawBottomRightRoundRect(canvas, paint, right, bottom);
break;
case CornerType.Top:
DrawTopRoundRect(canvas, paint, right, bottom);
break;
case CornerType.Bottom:
DrawBottomRoundRect(canvas, paint, right, bottom);
break;
case CornerType.Left:
DrawLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.Right:
DrawRightRoundRect(canvas, paint, right, bottom);
break;
case CornerType.OtherTopLeft:
DrawOtherTopLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.OtherTopRight:
DrawOtherTopRightRoundRect(canvas, paint, right, bottom);
break;
case CornerType.OtherBottomLeft:
DrawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.OtherBottomRight:
DrawOtherBottomRightRoundRect(canvas, paint, right, bottom);
break;
case CornerType.DiagonalFromTopLeft:
DrawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
break;
case CornerType.DiagonalFromTopRight:
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);
}
public string Key => "RoundedTransformation(radius=" + _mRadius + ", margin=" + _mMargin + ", diameter="
+ _mDiameter + ", cornerType=" + _mCornerType.ToString() + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment