Animation Xamarin窗体动画不会重复
我正在尝试用Xamarin.Forms(版本2.3.3.168)为图像设置动画。动画正在工作,但不会重复Animation Xamarin窗体动画不会重复,animation,xamarin,xamarin.forms,Animation,Xamarin,Xamarin.forms,我正在尝试用Xamarin.Forms(版本2.3.3.168)为图像设置动画。动画正在工作,但不会重复 public class WelcomePage : ContentPage { Image img; public WelcomePage() { img = new Image { Source = "img.png", HorizontalOptions = LayoutOptio
public class WelcomePage : ContentPage
{
Image img;
public WelcomePage()
{
img = new Image
{
Source = "img.png",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
img
}
};
}
protected override void OnAppearing()
{
base.OnAppearing();
var a = new Animation();
a.Add(0, 0.5, new Animation((v) =>
{
img.Scale = v;
}, 1.0, 1.2, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); }));
a.Add(0.5, 1, new Animation((v) =>
{
img.Scale = v;
}, 1.2, 1.0, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); }));
a.Commit(img, "animation", 16, 2000, Easing.Linear, (d, f) => img.Scale = 1.0, () =>
{
System.Diagnostics.Debug.WriteLine("ANIMATION ALL");
return true;
});
}
}
运行应用程序几秒钟后,将打印以下调试输出:
ANIMATION A
ANIMATION B
ANIMATION ALL
ANIMATION ALL
ANIMATION ALL
我将此作为UWP进行测试。根据,其他人似乎也有同样的问题。它似乎与在每个子动画中设置的私有属性有关
解决方法是在每次动画链完成时重新创建动画链:
public class WelcomePage : ContentPage
{
Image img;
public WelcomePage()
{
img = new Image
{
Source = "circle_plus.png",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
img
}
};
}
protected override void OnAppearing()
{
base.OnAppearing();
animate();
}
void animate()
{
var a = new Animation();
a.Add(0, 0.5, new Animation((v) =>
{
img.Scale = v;
}, 1.0, 1.2, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); }));
a.Add(0.5, 1, new Animation((v) =>
{
img.Scale = v;
}, 1.2, 1.0, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); }));
a.Commit(img, "animation", 16, 2000, null, (d, f) =>
{
img.Scale = 1.0;
System.Diagnostics.Debug.WriteLine("ANIMATION ALL");
animate();
});
}
}