Silverlight中的动画(Animation)与视图状态管理(Visual State Manager) 结合使用是非常常见的,动画用于管理对象在某段事件段内执行的动画动作,视图状态管理则用于控制对象在多个不同的视觉状态之间切换、导航。本篇主要介绍动画(Animation)与视图状态管理(Visual State Manager)的结合应用,关于视图状态管理的详细内容请大家查看相关资料。
要实现这个简单示例,可以通过《
Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)》里介绍的偏移动画特性去实现,为了效果上能够更加美观,可以增加一些其他的变换,比如在面板向下滑动出现的过程中动态的改变面板的透明度,从完全透明到不透明效果。当我们清楚了需求接下来就可以在Blend中进行设计了,首先布局界面(Button,Border[TextBlock])如下图所示:
添加Storyboard并选中第一秒时间线,设置border对象的Transform.Y为-296(这个值可以在设计中直接通过鼠标拖动Border对象确定),然后移动时间线到第三秒位置,设置Transfrom.Y为0。通过还可以在第一秒的时候设置border不透明度值为0%,第三秒的时候设置不透明度为100%,这样在动画过程中的效果会更好。
通过上图中布局的Button去触发动画开发,编译运行程序就可以查看到其效果,当点击按钮的时候就会看到一个面板从顶部已模糊到清晰的动画载入到界面上。另外可以将Border对象默认设置为不显示(Visibility="Collapsed"),当启动动画的时候将其设置为显示(Visibile)。
<
Storyboard
x:Name
="Storyboard1"
>
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="-296" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0.3" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
< discreteObjectKeyFrame KeyTime ="00:00:01" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
</ Storyboard >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="-296" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0.3" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
< discreteObjectKeyFrame KeyTime ="00:00:01" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
</ Storyboard >
以上的整个动画实现采用的是Border的位置变换实现的,接下来要介绍的就是如何通过视图状态管理(Visual State Manager)去实现和上面同样的功能。首先将界面设计为如下图所示界面,Border不透明度为30%,默认Y的位置为-298。
“In”状态用于完成面板从外动态的进入主界面,实际上也就是完成一个位置变换动画,详细如下截图所示:
“Out”状态与“In”状态相反,用于完成面板从主界面以动画的形式推出主界面,详细设计图下截图所示:
<
visualstatemanager.VisualStateGroups
>
< VisualStateGroup x:Name ="VisualStateGroup" >
< VisualState x:Name ="In" >
< Storyboard >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Duration ="00:00:00.0010000" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="-296" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0.3" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
< VisualState x:Name ="Out" >
< Storyboard >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Duration ="00:00:00.0010000" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="-298" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="1" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0.3" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
</ VisualStateGroup >
</ visualstatemanager.VisualStateGroups >
< VisualStateGroup x:Name ="VisualStateGroup" >
< VisualState x:Name ="In" >
< Storyboard >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Duration ="00:00:00.0010000" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="-296" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0.3" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
< VisualState x:Name ="Out" >
< Storyboard >
< ObjectAnimationUsingKeyFrames BeginTime ="00:00:00" Duration ="00:00:00.0010000" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Visibility)" >
< discreteObjectKeyFrame KeyTime ="00:00:00" >
< discreteObjectKeyFrame.Value >
< Visibility > Visible </ Visibility >
</ discreteObjectKeyFrame.Value >
</ discreteObjectKeyFrame >
</ ObjectAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="0" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="-298" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.TargetName ="border"
Storyboard.TargetProperty ="(UIElement.Opacity)" >
< EasingDoubleKeyFrame KeyTime ="00:00:00" Value ="1" />
< EasingDoubleKeyFrame KeyTime ="00:00:01" Value ="0.3" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
</ VisualStateGroup >
</ visualstatemanager.VisualStateGroups >
private
void
onInClick(
object
sender, System.Windows.RoutedEventArgs e)
{
visualstatemanager.GoToState( this , " In " , false );
}
private void onOutClick( object sender, " Out " , false );
}
{
visualstatemanager.GoToState( this , " In " , false );
}
private void onOutClick( object sender, " Out " , false );
}
推荐资源:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。