Created
July 23, 2017 23:14
-
-
Save programmation/53ec8e7720010d825f81a6863516f9df to your computer and use it in GitHub Desktop.
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
// from https://forums.xamarin.com/discussion/comment/279890/#Comment_279890 | |
public partial class GradientView : ContentView | |
{ | |
public Color StartColor { get; set; } = Color.Transparent; | |
public Color EndColor { get; set; } = Color.Transparent; | |
public bool Horizontal { get; set; } = false; | |
public GradientView() | |
{ | |
InitializeComponent(); | |
SKCanvasView canvasView = new SKCanvasView(); | |
canvasView.PaintSurface += OnCanvasViewPaintSurface; | |
Content = canvasView; | |
} | |
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) | |
{ | |
SKImageInfo info = args.Info; | |
SKSurface surface = args.Surface; | |
SKCanvas canvas = surface.Canvas; | |
canvas.Clear(); | |
var colors = new SKColor[] { StartColor.ToSKColor(), EndColor.ToSKColor()}; | |
SKPoint startPoint = new SKPoint(0,0); | |
SKPoint endPoint = Horizontal ? new SKPoint(info.Width, 0) : new SKPoint(0, info.Height); | |
var shader = SKShader.CreateLinearGradient(startPoint, endPoint, colors, null, SKShaderTileMode.Clamp); | |
SKPaint paint = new SKPaint | |
{ | |
Style = SKPaintStyle.Fill, | |
Shader = shader | |
}; | |
canvas.DrawRect(new SKRect(0, 0, info.Width, info.Height), paint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment