Skip to content

Instantly share code, notes, and snippets.

@nodoid
Created December 18, 2015 07:52
Show Gist options
  • Save nodoid/092d3d31bb965d558b27 to your computer and use it in GitHub Desktop.
Save nodoid/092d3d31bb965d558b27 to your computer and use it in GitHub Desktop.
public class CustomImageRenderer : ImageRenderer
{
MyRect imgBorderRect;
private Rect mBounds;
private Paint mBorderPaint;
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var img = e.NewElement as CustomImage;
imgBorderRect = img.BorderThickness;
mBounds = new Rect(Control.Left, Control.Top, Control.Right, Control.Bottom);
mBorderPaint = new Paint();
mBorderPaint.StrokeWidth = (float)imgBorderRect.Left;
mBorderPaint.Color = img.BorderColor.ToAndroidGraphicsColor();
}
}
public override void Draw(Canvas canvas)
{
if (imgBorderRect.Left != 0)
canvas.DrawLine((float)(mBounds.Left - imgBorderRect.Left / 2), mBounds.Top,
(float)(mBounds.Left - imgBorderRect.Left / 2), mBounds.Bottom, mBorderPaint);
if (imgBorderRect.Top != 0)
canvas.DrawLine(mBounds.Left, (float)(mBounds.Top - imgBorderRect.Top / 2), mBounds.Right,
(float)(mBounds.Top - imgBorderRect.Top / 2), mBorderPaint);
if (imgBorderRect.Right != 0)
canvas.DrawLine((float)(mBounds.Right + imgBorderRect.Right / 2), mBounds.Top,
(float)(mBounds.Right + imgBorderRect.Right / 2), mBounds.Bottom, mBorderPaint);
if (imgBorderRect.Bottom != 0)
canvas.DrawLine(mBounds.Left, (float)(mBounds.Bottom + imgBorderRect.Bottom / 2),
mBounds.Right, (float)(mBounds.Bottom + imgBorderRect.Bottom / 2), mBorderPaint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment