EventampTrigger之三Animation.docx
《EventampTrigger之三Animation.docx》由会员分享,可在线阅读,更多相关《EventampTrigger之三Animation.docx(12页珍藏版)》请在冰豆网上搜索。
EventampTrigger之三Animation
ImplementingAnimation
动画(Animation)是WPF的一个特性。
可根据时间变化设置不同属性,例如:
视图的变化,颜色的逐渐变化,随时间改变属性。
其中Storyboard对象能够控制动画的反馈。
1.1.UsingAnimations
Animation类是一个巨大的类群。
有42个类群,从下面namespace中出来
using System.Windows.Media.Animationnamespace;
最后可以归为3个基础:
●∙Linearanimations(Overview);
线形的属性变化,命名规范为Animation,eg:
DoubleAnimation
●∙keyframe–basedanimations(Key-Frame);
从一个起点到多个点(waypoints)进行属性变化,命名规范为AnimationUsingKeyFrames,其中为动态变化的类型,eg:
StringAnimationUsingKeyFrames
●∙path-basedanimations(Path-Based);
使用预设的PathGeometry对象进行属性变化,通常用于很复杂的视图属性变化,命名规范:
AnimationUsingPatheg:
PointAnimationUsingPath,DoubleAnimationUsingPath
3types:
PointAnimationUsingPath,DoubleAnimationUsingPath,andMatrixAnimationUsingPath
seeEasingFunction(frompracticequestionthattrippedmeup)
seeVisualStateManager
ImportantpropertiesoftheAnimationandStoryboardclass
●∙Notethatifyoucreateananimationincode,youneedtoexecutetheFrameworkContentElement.RegisterNamemethod,toregistertheelementwithinthenamespace,asshowninthisexample.
SeethispageforexamplesofdifferentpossiblevaluesfortheRepeatBehaviorproperty.
1.2.ImportantPropertiesofAnimations
尽管有很多的Animation类,但是很多类都是有共同的属性的,特别是大多数amimation都靠Storyboard来组织animation对象。
属性
描述
AccelerationRatio
GetsorsetsavaluespecifyingthepercentageoftheDurationpropertyoftheanimationthatisspentacceleratingthepassageoftimefromzerotoitsmaximumrate
AutoReverse
Getsorsetsavaluethatindicateswhethertheanimationplaysinreverseafteritcompletesaforwarditeration
BeginTime
Getsorsetsthetimeatwhichtheanimationshouldbegin,relativetothetimeitisexecuted.eg,ananimationwithBeginTimesetto0:
0:
5exhibitsa5-seconddelaybeforebeginning.
DecelerationRatio
Getsorsetsavaluespecifyingthepercentageofthedurationoftheanimationspentdeceleratingthepassageoftimefromitsmaximumratetozero
Duration
Getsorsetsthelengthoftimeforwhichtheanimationplays
FillBehavior
Getsorsetsavaluethatindicateshowtheanimationbehavesafterithascompleted
RepeatBehavior
Getsorsetsavaluethatindicateshowtheanimationrepeats
SpeedRatio
Getsorsetstherateatwhichtheanimationprogressesrelativetoitsparent
●∙LineAnimation的一些重要参数
属性
描述
From
Getsorsetsthestartingvalueoftheanimation.Ifomitted,theanimation
usesthecurrentpropertyvalue.
To
Getsorsetstheendingvalueoftheanimation
By
Getsorsetstheamountbywhichtoincreasethevalueofthetargetpropertyoverthecourseoftheanimation.IfboththeToandBypropertiesareset,thevalueoftheBypropertyisignored.
例子:
0:
10"From="1"To="200"/>
Animation控件一定要和StoryBoard一起用。
1.3.StoryboardObjects
Storyboardoverview是组织了Animation,Storyboard类内部有一个Children集合,能够控制timeline集合,来达到控制动画的目的。
0:
10"From="1"To="200"/>
1.3.1. USINGASTORYBOARDTOCONTROLANIMATIONS
●∙如何使用Storyboard来控制动画?
使用TatgetName和TargetProperty参数
0:
10"From="1"To="200"/>
●∙可以这么写,把TargetName和TargetProperty作为attachedproperty:
0:
10" From="1" To="200"
Storyboard.TargetName="Button1" Storyboard.TargetProperty="Height"/>
●∙Storyboard中可以有多个Animation,所以这种写法更常见,
1.3.2. SIMULTANEOUSANIMATIONS
在Storyboard中的子元素是同时开始的,所以可以设置成为同时的动态变化
0:
10"From="1"To="200"
Storyboard.TargetName="Button1"
Storyboard.TargetProperty="Height"/>
0:
10"From="1"To="100"
Storyboard.TargetName="Button1"
Storyboard.TargetProperty="Widtht"/>
1.4.UsingAnimationswithTriggers
●∙在什么时候使用Animation?
在程序中,需要制定什么时候Animation才开始,则可以触发Animation:
先用EventTrigger中的RoutedEvent定义click事件;然后使用EventTrigger.Actions类;Action类控制BeginStoryboard元素,里面才是Storyboardoverview:
0:
5"
Storyboard.TargetProperty="Height"To="200"/>
●∙在Action类中控制Animation的参数
属性
描述
BeginStoryboard
BeginsthechildStoryboardobject
PauseStoryboard
Pausestheplaybackofanindicatedstoryboardatthecurrentplaybackposition
ResumeStoryboard
Resumesplaybackofanindicatedstoryboard
SeekStoryboard
Fast-forwardstoaspecifiedpositioninatargetstoryboard
SetStoryboardSpeedRatio
Setsthespeedratioofthespecifiedstoryboard
SkipStoryboardToFill
Movesthespecifiedstoryboardtotheendofitstimeline
StopStoryboard
Stopsplaybackofthespecifiedstoryboardandreturnsthe
animationtothestartingposition
Examples:
●∙AStopStoryboardactionthatstopstheBeginStoryBoardobjectnamed“stb1”:
0:
5"
Storyboard.TargetProperty="Height"To="200"/>
●∙作用于同一个Storyboard的东西,都需要写在一个Trigger里面,写在不同trigger里,可能有些对于这个Storyboard没有用。
●∙这个例子讲了SetStoryboardSpeedRatio,速率2倍的变化
0:
5"
Storyboard.TargetProperty="Height"To="200"/>
●∙这个例子讲了SeekStoryboard例其中Origin属性一般填写“BeginTime”和“Duration”,
beginTime指的offset参数和storyboard的beginning相关;Duration指的是offset参数和storyboard的Duration相关;offset参数指的是开始的偏移量,一般指timeline
Offset="0:
0:
5"/>
●∙ActionbeenhostedinEventTrigger对象,可以在Trigger,MultiTrigger,DataTrigger,andMultiDataTrigger对象托管两个Action集合:
EnterActions&ExitActions集合;
To="20"Duration="0:
0:
.5"/>
1.5.ManagingthePlaybackTimeLine
在Animation类和Storyboard类中都有属性对播放时间线做精确的管理。
使用playback时注意其作用范围,如果是在整个storyboard设置,则作用在storyboard中,如果作用在某个属性中,则只能作用这个属性。
System.Windows.Media.Animation.Timeline
AccelerationRatioandDecelerationRatio
AutoReverse
FillBehavior
RepeatBehavior
SpeedRatio
●∙ACCELERATIONRATIOANDDECELERATIONRATIO
表示以标准速度多少百分比的速度,进行加速或者减速放映。
例子是指以20%的速度加速放映。
0:
5"AccelerationRatio="0.2"
Storyboard.TargetProperty="Height"To="200"/>
●∙AUTOREVERSE
表示到底了之后,是否会自动反转,默认值为false,设置为true则表示可以自动反转。
0:
5"AutoReverse="True"
Storyboard.TargetProperty="Height"To="200"/>
●∙FILLBEHAVIOR
表示到动画完成之后,如何操作,如果是HoldEnd,则表示动画完成之后,取最后一个值,如果是Stop,则表示动画完成之后,动画停止,并且回到最初的timeline.
0:
5"FillBehavior="Stop"
Storyboard.TargetProperty="Height"To="200"/>
●∙REPEATBEHAVIOR
表示是否或者如何重复。
参数Forever,nx(2x),Duration;Forever表示动画一直重复;nx表示重复的数字;Duration表示总共的字数;
0:
5"RepeatBehavior="Forever"
Storyboard.TargetProperty="Height"To="200"/>
0:
5"RepeatBehavior="3x"
Storyboard.TargetProperty="Height"To="200"/>
0:
5"RepeatBehavior="0:
1:
0"
Storyboard.TargetProperty="Height"To="200"/>
●∙SPEEDRATIO
表示加速和减慢速度;
0:
5"SpeedRatio="0.5"
Storyboard.TargetProperty="Height"To="200"/>
1.6.AnimatingNon-DoubleTypes
例如ColorAnimation标签;
Name="myBrush"/>
Storyboard.TargetProperty="Color"From="Red"To="LimeGreen"
Duration="0:
0:
5"/>
1.6.1. ANIMATIONWITHKEYFRAMES
Keyframesarewaypointsinananimation;
Keyframes把动画分成多个短segment,每个keyframe都可以有定义自己的Value参数和Key参数;
每个数据类型支持线性动画的一定支持key-frameanimation,有些没有线性动画的,却支持key-frameanimation.
命名 AnimationUsingKeyFrames,其中为动态变化的类型,有3种类型的keyframes动画
●∙LinearKeyFrame.Thesekeyframesprovidepointsinananimationthatareinterpolatedbetweeninalinearfashion.
0:
1"/>
0:
2"/>
0:
4"/>
高度,1秒的时候变成10,2秒时候变成100,4秒的时候比办成30;
1.6.2. DISCRETEKEYFRAMES
●∙DiscreteKeyFrame.Youcanusediscretekeyframeobjectstomakediscretechangesinthevalueofananimatedproperty
0:
0"/>
0:
1"/>
0:
2"/>