Skip to content

Instantly share code, notes, and snippets.

@Sweekriti91
Last active June 26, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sweekriti91/15194d102ee729c881c0c7bfcb5dae4f to your computer and use it in GitHub Desktop.
Save Sweekriti91/15194d102ee729c881c0c7bfcb5dae4f to your computer and use it in GitHub Desktop.
Black to Transparent Gradient
using System;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;
namespace XamCAT.Forms.Controls
{
public class GradientBack : ContentView
{
public GradientBack()
{
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();
using (SKPaint paint = new SKPaint())
{
// Create 300-pixel square centered rectangle
float x = 0;
float y = 0;
SKRect rect = new SKRect(x, y, info.Width, info.Height);
// Create linear gradient from upper-left to lower-right
paint.Shader = SKShader.CreateLinearGradient(
new SKPoint(rect.MidX, rect.Top),
new SKPoint(rect.MidX, rect.Bottom),
new SKColor[] { SKColors.Black.WithAlpha(0), SKColors.Black },
new float[] { 0, .8f },
SKShaderTileMode.Clamp);
// Draw the gradient on the rectangle
canvas.DrawRect(rect, paint);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment