Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Forked from KlausLoeffelmann/ProjectSymbol.cs
Created August 17, 2020 13:03
Show Gist options
  • Save stephensmitchell/db22b670e4bcd6b142c523e6f24a43f1 to your computer and use it in GitHub Desktop.
Save stephensmitchell/db22b670e4bcd6b142c523e6f24a43f1 to your computer and use it in GitHub Desktop.
Demo for SkiaSharp based Custom Control in Xamarin.Forms
using SkiaSharp;
using SkiaSharp.Views.Forms;
using System;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
namespace Legatro.Cross.Controls
{
class ProjectSymbol : SKCanvasView
{
public static readonly BindableProperty CircleColorProperty = BindableProperty.Create(nameof(CircleColor),
typeof(Color), typeof(ProjectSymbol), Color.Blue,
propertyChanging: (currentControl, oldValue, newValue) =>
{
var thisControl = currentControl as ProjectSymbol;
thisControl.CircleColor = (Color)newValue;
});
public static readonly BindableProperty CircleBackgroundColorProperty = BindableProperty.Create(nameof(CircleBackgroundColor),
typeof(Color), typeof(ProjectSymbol), Color.LightGray,
propertyChanging: (currentControl, oldValue, newValue) =>
{
var thisControl = currentControl as ProjectSymbol;
thisControl.CircleBackgroundColor = (Color)newValue;
});
public static readonly BindableProperty CircleStrokeWidthProperty = BindableProperty.Create(nameof(CircleStrokeWidth),
typeof(float), typeof(ProjectSymbol), (float)4,
propertyChanging: (currentControl, oldValue, newValue) =>
{
var thisControl = currentControl as ProjectSymbol;
thisControl.CircleStrokeWidth = (float)newValue;
});
private SKColor myCircleColor;
private SKColor myCircleBackgroundColor;
private SKColor myBackgroundColor;
private float myCircleStrokeWidth;
public ProjectSymbol()
{
BackgroundColor = myBackgroundColor.ToFormsColor();
CircleBackgroundColor = Color.LightGray;
CircleColor = Color.Blue;
CircleStrokeWidth = 4;
}
public Color CircleColor
{
get { return (Color) GetValue(CircleColorProperty); }
set
{
SetValue(CircleColorProperty, value);
myCircleColor = value.ToSKColor();
InvalidateSurface();
}
}
public float CircleStrokeWidth
{
get { return (float)GetValue(CircleStrokeWidthProperty); }
set
{
SetValue(CircleStrokeWidthProperty, value);
myCircleStrokeWidth = value;
InvalidateSurface();
}
}
public Color CircleBackgroundColor
{
get { return (Color)GetValue(CircleBackgroundColorProperty); }
set
{
SetValue(CircleBackgroundColorProperty, value);
myCircleBackgroundColor = value.ToSKColor();
InvalidateSurface();
}
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName==nameof(BackgroundColor))
{
myBackgroundColor = BackgroundColor.ToSKColor();
}
}
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
base.OnPaintSurface(e);
SKImageInfo info = e.Info;
SKSurface surface = e.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear(myBackgroundColor);
SKPaint paint = new SKPaint
{
Style = SKPaintStyle.Fill,
TextSize = 48,
StrokeWidth = myCircleStrokeWidth,
IsAntialias=true
};
var circleRadius = Math.Min(info.Width - myCircleStrokeWidth, info.Height - myCircleStrokeWidth) / 2;
var circleMiddle = circleRadius + myCircleStrokeWidth / 2;
paint.Color = myCircleBackgroundColor;
canvas.DrawCircle(circleMiddle, circleMiddle, circleRadius, paint);
paint.Style = SKPaintStyle.Stroke;
paint.Color = myCircleColor;
canvas.DrawCircle(circleMiddle, circleMiddle, circleRadius, paint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment