Last active
June 26, 2020 19:14
-
-
Save Sweekriti91/15194d102ee729c881c0c7bfcb5dae4f to your computer and use it in GitHub Desktop.
Black to Transparent Gradient
This file contains 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
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