如何向 Xamarin Forms 中页面标题下方的边框线添加渐变?
我想要实现的一个例子是here。
如何向 Xamarin Forms 中页面标题下方的边框线添加渐变?
我创建自定义渲染来创建渐变边框。
首先,您需要定义渲染:
public class GradientViewRender : View
{
public static readonly BindableProperty GradientColorsProperty = BindableProperty.Create<GradientViewRender, Color[]>(p => p.GradientColors, new Color[]{Color.White} );
public Color[] GradientColors
{
get { return (Color[])base.GetValue(GradientColorsProperty); }
set { base.SetValue(GradientColorsProperty, value); }
}
public static readonly BindableProperty ViewHeightProperty = BindableProperty.Create<GradientViewRender, double>(p => p.ViewHeight, 0);
public double ViewHeight
{
get { return (double)base.GetValue(ViewHeightProperty); }
set { base.SetValue(ViewHeightProperty, value); }
}
public static readonly BindableProperty LeftToRightProperty = BindableProperty.Create<GradientViewRender, bool>(p => p.LeftToRight, true);
public bool LeftToRight
{
get { return (bool)base.GetValue(LeftToRightProperty); }
set { base.SetValue(LeftToRightProperty, value); }
}
}
此渲染采用一组颜色,这样您就可以根据渐变提供尽可能多的颜色。
然后你可以在 android 中实现这个渲染。
public class GradientViewRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<GradientTest.GradientViewRender, View>
{
LinearLayout layout;
Xamarin.Forms.Color[] gradientColors;
double viewHeight;
protected override void OnElementChanged(ElementChangedEventArgs<GradientTest.GradientViewRender> e)
{
base.OnElementChanged(e);
if (Control == null)
{
layout = new LinearLayout(Application.Context);
layout.SetBackgroundColor(Color.White);
gradientColors = (Xamarin.Forms.Color[])e.NewElement.GradientColors;
viewHeight = (double)e.NewElement.ViewHeight;
CreateLayout();
}
if (e.OldElement != null)
{
// Unsubscribe from event handlers and cleanup any resources
}
if (e.NewElement != null)
{
// Configure the control and subscribe to event handlers
gradientColors = (Xamarin.Forms.Color[])e.NewElement.GradientColors;
viewHeight = (double)e.NewElement.ViewHeight;
CreateLayout();
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == GradientViewRender.ViewHeightProperty.PropertyName)
{
this.viewHeight = (double)this.Element.ViewHeight;
CreateLayout();
}
else if (e.PropertyName == GradientViewRender.GradientColorsProperty.PropertyName)
{
this.gradientColors = (Xamarin.Forms.Color[])this.Element.GradientColors;
CreateLayout();
}
}
private void CreateLayout()
{
layout.SetMinimumWidth((int)viewWidth);
layout.SetMinimumHeight((int)viewHeight);
CreateGradient();
SetNativeControl(layout);
}
public void CreateGradient()
{
//Need to convert the colors to Android Color objects
int[] androidColors = new int[gradientColors.Count()];
for (int i = 0; i < gradientColors.Count(); i++)
{
Xamarin.Forms.Color temp = gradientColors[i];
androidColors[i] = temp.ToAndroid();
}
GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors);
if (roundCorners)
gradient.SetCornerRadii(new float[] { cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius });
layout.SetBackground(gradient);
}
}
关于更详细的信息,你可以看看:
https://baglabs.com/2017/07/14/creating-gradients-xamarin-forms/
您可以从:
https://github.com/baileysh9/xamarin_forms_gradient
最后,你可以像这样得到这个渐变边框:
您可以通过 GradientColors 更改不同的颜色。
如果我的回复对您有帮助,请记得将我的回复标记为回复,谢谢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句