UWP: Better way to animate images?
The better way is make Storyboard
in the code-behind, and do TranslateTransform.X
and TranslateTransform.Y
animation randomly. Please note the better way is place your image in the Canvas.
For example
private void BTAnimation()
{
Storyboard storyboard = new Storyboard();
DoubleAnimation translateYAnimation = new DoubleAnimation();
translateYAnimation.From = 0;
translateYAnimation.To = ActualHeight;
translateYAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.5));
Storyboard.SetTarget(translateYAnimation, MyImage);
Storyboard.SetTargetProperty(translateYAnimation, "(UIElement.RenderTransform).(TranslateTransform.Y)");
storyboard.Children.Add(translateYAnimation);
DoubleAnimation OpacityAnimation = new DoubleAnimation();
OpacityAnimation.From = 1;
OpacityAnimation.To = 0;
OpacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.5));
Storyboard.SetTarget(OpacityAnimation, MyImage);
Storyboard.SetTargetProperty(OpacityAnimation, "Opacity");
storyboard.Children.Add(OpacityAnimation);
storyboard.Begin();
}
private void LRAnimation()
{
Storyboard storyboard = new Storyboard();
DoubleAnimation translateYAnimation = new DoubleAnimation();
translateYAnimation.From = 0;
translateYAnimation.To = ActualWidth;
translateYAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.5));
Storyboard.SetTarget(translateYAnimation, MyImage);
Storyboard.SetTargetProperty(translateYAnimation, "(UIElement.RenderTransform).(TranslateTransform.X)");
storyboard.Children.Add(translateYAnimation);
DoubleAnimation OpacityAnimation = new DoubleAnimation();
OpacityAnimation.From = 1;
OpacityAnimation.To = 0;
OpacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.5));
Storyboard.SetTarget(OpacityAnimation, MyImage);
Storyboard.SetTargetProperty(OpacityAnimation, "Opacity");
storyboard.Children.Add(OpacityAnimation);
storyboard.Begin();
}
Xaml
<Canvas x:Name="RootCanvas">
<Image
x:Name="MyImage"
Width="100"
Height="100"
AutomationProperties.AccessibilityView="Raw"
Loaded="OnHeartLoaded"
RenderTransformOrigin="0.5,0.5"
Source="/Assets/logo.jpg"
Stretch="Fill">
<Image.RenderTransform>
<TranslateTransform />
</Image.RenderTransform>
</Image>
</Canvas>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…