8,735
社区成员
发帖
与我相关
我的任务
分享
Storyboard playStory = new Storyboard();
DoubleAnimation playAnimation = new DoubleAnimation();
Storyboard.SetTargetProperty(playAnimation, Opacity.ToString());
playAnimation.From = 1;
playAnimation.To = 0;
playAnimation.Duration = TimeSpan.FromSeconds(2);
playStory.RepeatBehavior = RepeatBehavior.Forever;
bool isNext = true;
foreach (var item in playList)
{
if (isNext)
{
isNext = false;
NextPicture.Source = item;
// Storyboard.SetTargetName(playAnimation, StartPicture.Name.ToString());
NextPicture.Opacity = 1;
}
else
{
isNext = true;
StartPicture.Source = item;
// Storyboard.SetTargetName(playAnimation, NextPicture.Name.ToString());
StartPicture.Opacity = 1;
}
}
<Page.Resources>
<Storyboard x:Name="playStoryboard">
<DoubleAnimation x:Name="hideAnimation" Storyboard.TargetProperty="Opacity"
From="1" To="0" Duration="0:0:2" BeginTime="0:0:0">
</DoubleAnimation>
<DoubleAnimation x:Name="showAnimation" Storyboard.TargetProperty="Opacity"
From="0" To="1" Duration="0:0:2" BeginTime="0:0:0">
</DoubleAnimation>
<DoubleAnimation x:Name="stateAnimation" Storyboard.TargetProperty="Opacity"
From="0" To="0" BeginTime="0:0:2" Duration="0:0:3">
</DoubleAnimation>
</Storyboard>
</Page.Resources>
使用到的Image控件
<Image Name="StatePicture" Visibility="Collapsed"></Image>
<Image Name="NextPicture"></Image>
<Image Name="StartPicture"></Image>
动画完成事件
playStoryboard.Completed += (s, e) =>
{
playStoryboard.Stop();
currIndex = (currIndex + 1) % playList.Count;
if (isNext)
{
// Storyboard停止后,会恢复到动画播放前状态,需在此“固定”动画结束状态
NextPicture.Opacity = 1;
StartPicture.Opacity = 0;
StartPicture.Source = playList[currIndex];
Storyboard.SetTarget(showAnimation, StartPicture);
Storyboard.SetTarget(hideAnimation, NextPicture);
Storyboard.SetTarget(stateAnimation, StatePicture);
isNext = false;
}
else
{
// Storyboard停止后,会恢复到动画播放前状态,需在此“固定”动画结束状态
StartPicture.Opacity = 1;
NextPicture.Opacity = 0;
NextPicture.Source = playList[currIndex];
Storyboard.SetTarget(showAnimation, NextPicture);
Storyboard.SetTarget(hideAnimation, StartPicture);
Storyboard.SetTarget(stateAnimation, StatePicture);
isNext = true;
}
playStoryboard.Begin();
};
动画开始暂停控制
private void playPauseBtn_Click(object sender, RoutedEventArgs e)
{
if (isPlaying)
{
// 按键图标
playPauseImg.Source = new BitmapImage(new Uri("ms-appx://" + "/Assets/Pause.png"));
isPlaying = false;
if(isPause)
{
playStoryboard.Resume();
}
else
{
NextPicture.Source = playList[currIndex];
Storyboard.SetTarget(showAnimation, NextPicture);
Storyboard.SetTarget(hideAnimation, StartPicture);
Storyboard.SetTarget(stateAnimation, StatePicture);
isNext = true;
playStoryboard.Begin();
}
}
else
{
// 按钮图标
playPauseImg.Source = new BitmapImage(new Uri("ms-appx://" + "/Assets/Play.png"));
isPlaying = true;
playStoryboard.Pause();
isPause = true;
}
}