WPF - 动画图像源更改 [英] WPF - animate image source change
问题描述
我对WPF很新,但我认为我需要做的事情相对简单。我需要创建一个图像动画,我每隔0.25秒更换一个图像源。
I'm pretty new to WPF but I think what I need to do is relatively simple. I need to create an image "animation", where I am changing an image source every .25 of a second.
我有一个名为animation的图像文件夹1到25 png live(名为1.png,2.png ... 25.png)。每个图像都与我动画的不同帧相关。
I have a folder named "animation" with images 1 to 25 png live (named 1.png, 2.png... 25.png). Each image correlate to different frame of my animation.
我想写xaml将图像从1更改为2,从2更改为3,每次更改为3到4等.25秒直到它到达第25个图像,然后它应该循环回到开始。
I want to write xaml to change the image from 1 to 2, from 2 to 3, for 3 to 4 etc every .25 seconds until it gets to the 25th image, then it should loop back to the start.
我很可能需要写一些c#来做到这一点。我希望它在可以与UI交互的线程上运行(如更新图像)但不阻止UI线程。
I most likely will need to write some c# to do this. I want it to run on a thread that can interact with the UI (as in update the image) but not block the UI thread.
提前致谢!
推荐答案
纯XAML解决方案可能看起来像这样,当然还有不同的图像和时间。
A pure XAML solution could look like this, of course with different images and timings.
<Image>
<Image.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
Duration="0:0:2">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:1">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
这篇关于WPF - 动画图像源更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!