Created
May 30, 2019 17:26
-
-
Save ufukhawk/3607d10f72a7c4beaef013698c0afba2 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
using System; | |
using GradientColorDemo.Droid; | |
using GradientColorDemo.CustomControls; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
[assembly: ExportRenderer(typeof(GradientColorStack), typeof(GradientColorStackRenderer))] | |
namespaceDroid { | |
public class GradientColorStackRenderer: VisualElementRenderer < StackLayout > { | |
private Color StartColor { | |
get; | |
set; | |
} | |
private Color EndColor { | |
get; | |
set; | |
} | |
protected override void DispatchDraw(global::Android.Graphics.Canvas canvas) | |
{ | |
#region for Vertical Gradient | |
//var gradient = new Android.Graphics.LinearGradient(0, 0, 0, Height, | |
# endregion | |
# region for Horizontal Gradient | |
var gradient = newGraphics.LinearGradient(0, 0, Width, 0, | |
#endregion | |
this.StartColor.ToAndroid(), | |
this.EndColor.ToAndroid(), | |
Android.Graphics.Shader.TileMode.Mirror); | |
var paint = new Android.Graphics.Paint() { | |
Dither = true, | |
}; | |
paint.SetShader(gradient); | |
canvas.DrawPaint(paint); | |
base.DispatchDraw(canvas); | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e) { | |
base.OnElementChanged(e); | |
if (e.OldElement != null || Element == null) { | |
return; | |
} | |
try { | |
var stack = e.NewElement as GradientColorStack; | |
this.StartColor = stack.StartColor; | |
this.EndColor = stack.EndColor; | |
} catch (Exception ex) { | |
Syatem.Diagnostics.Debug.WriteLine(@ "ERROR:", ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment