C# Xamarin上的FadeIn/FadeOut背景色动画
我试图使一个帧在其背景色上具有无限的FadeIn/FadeOut循环效果。发生的情况是,只播放并重复第一个动画,而忽略第二个动画 我想要的是: Alpha:0.5衰减为1,然后从1衰减为0.5,然后重复所有过程 正在发生的事情: Alpha:0.5逐渐变为1,然后重复C# Xamarin上的FadeIn/FadeOut背景色动画,c#,xamarin,animation,C#,Xamarin,Animation,我试图使一个帧在其背景色上具有无限的FadeIn/FadeOut循环效果。发生的情况是,只播放并重复第一个动画,而忽略第二个动画 我想要的是: Alpha:0.5衰减为1,然后从1衰减为0.5,然后重复所有过程 正在发生的事情: Alpha:0.5逐渐变为1,然后重复 new Animation(callback: v => BackgroundColor = Color.FromRgba(183, 226, 241, v), start: 0.5, end: 1). Commit(thi
new Animation(callback: v => BackgroundColor = Color.FromRgba(183, 226, 241, v), start: 0.5, end: 1).
Commit(this, "Animation", 16, 4000, Easing.Linear, (v, c) =>
{
new Animation(callback: d => BackgroundColor = Color.FromRgba(183, 226, 241, d), start: 1, end: 0.5).
Commit(this, "Animation2", 16, 4000, Easing.Linear, (d, x) =>
BackgroundColor = Color.FromRgba(183, 226, 241, d),() => false);
},()=>true);
我已经阅读了有关.Commit的文档,但它有点混乱。我在我身边使用您的代码,但我对页面没有任何影响,如果您想更改背景颜色并重复,我建议您可以从中使用ViewExtensions
我在我身边使用您的代码,但我对页面没有任何影响,如果您想更改背景颜色并重复,我建议您可以从中使用ViewExtensions
我看不到循环此最后一个参数的代码
()=>true
告诉动画在完成时重复。我看不到循环此最后一个参数的代码()=>true
告诉动画在完成时重复。
public static class ViewExtensions
{
public static Task<bool> ColorTo(this VisualElement self, Color fromColor, Color toColor, Action<Color> callback, uint length = 250, Easing easing = null)
{
Func<double, Color> transform = (t) =>
Color.FromRgba(fromColor.R + t * (toColor.R - fromColor.R),
fromColor.G + t * (toColor.G - fromColor.G),
fromColor.B + t * (toColor.B - fromColor.B),
fromColor.A + t * (toColor.A - fromColor.A));
return ColorAnimation(self, "ColorTo", transform, callback, length, easing);
}
public static void CancelAnimation(this VisualElement self)
{
self.AbortAnimation("ColorTo");
}
static Task<bool> ColorAnimation(VisualElement element, string name, Func<double, Color> transform, Action<Color> callback, uint length, Easing easing)
{
easing = easing ?? Easing.Linear;
var taskCompletionSource = new TaskCompletionSource<bool>();
element.Animate<Color>(name, transform, callback, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
return taskCompletionSource.Task;
}
}
while(true)
{
await this.ColorTo(Color.FromRgb(0, 0, 0), Color.FromRgb(255, 255, 255), c => BackgroundColor = c, 5000);
await this.ColorTo(Color.FromRgb(255, 255, 255), Color.FromRgb(0, 0, 0), c => BackgroundColor = c, 5000);
}