WPF中的动态动画 [英] Dynamic Animations in WPF

查看:161
本文介绍了WPF中的动态动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我敢肯定,我已经看过一种用WPF制作动画的方法,类似于Cocoa的动画制作方法,我使用静态方法告诉对象要移动的内容,在一行中全部完成.但是,我在MSDN上可以找到的所有示例都在创建情节提要板等.对吗?有没有一种方法可以在不创建大量对象的情况下制作简单的动态动画?

我发现了:

 DoubleAnimation da =  DoubleAnimation();
da.From = .
da.To =高度- 50 ;
da.Duration = 持续时间(TimeSpan.FromSeconds( 1 ));

 .BeginAnimation(Window.TopProperty,da); 




如果这是我动态地做事的方式,那么我如何一次对多个事物进行动画处理?我想这需要一个StoryBoard,而我又回到了开始的地方? .

至于移动元素,困难在于元素实际上没有设置Left/Top属性.在WPF中,所有内容都基于边距和对齐方式.

我从没有尝试过,但是您也许可以设置一个动画.虽然您很可能最终会得到一些拉伸效果.

我为移动设置动画的方法是使用TranslateTransform.

您需要为要移动的每个轴设置一个动画,因此在对角线上移动时,总共需要2个动画.

这是一个如何在对角线上移动元素150的快速示例:

对VB感到抱歉-虽然不难转换为C#

''This creates the new TranslateTransform that changes how the element is rendered
'' (x = 0, y = 0) means no translation is done
''[new] is the element to be animated
[new].RenderTransform = New TranslateTransform()

''The animation requires a NameScope within which to operate, as the animations must be using a registered name for the object
Dim ns As New NameScope()
NameScope.SetNameScope(_manager.Panel, ns) ''_manager.Panel in my case was the container of the element
ns.RegisterName("newTransform", [new].RenderTransform) ''Register the name

Dim board As New Storyboard()
Dim animLength As New TimeSpan(0, 0, 0, 0, 1500) ''1.5 seconds

''Handle YProperty (moving 150 units down)
''A FillBehavior of Stop resets the animation after it has completed
''A FillBehavior of Hold keeps the element at the intended target, but you won''t be able to manually set the property in question anymore
Dim dblNewY As New DoubleAnimation(150, New Duration(animLength), FillBehavior.Stop) 
Storyboard.SetTargetName(dblNewY, "newTransform") ''tell the animation what to animate against
Storyboard.SetTargetProperty(dblNewY, New PropertyPath(TranslateTransform.YProperty)) ''tell the animation which property
board.Children.Add(dblNewY)

''Handle XProperty (moving 150 units to the right)
Dim dblNewX As New DoubleAnimation(150, New Duration(animLength), FillBehavior.Stop)
Storyboard.SetTargetName(dblNewX, "newTransform")
Storyboard.SetTargetProperty(dblNewX, New PropertyPath(TranslateTransform.XProperty))
board.Children.Add(dblNewX)
board.Begin(_manager.Panel) ''the storyboard wanted a reference point for the namespace - this works with the NameSpace registered above and is again the container of the element to animate


我意识到这正在创建许多对象,但这是一个动态动画.我一直在使用其中的一些内容来创建一个完整的动画框架,以对给定容器中的元素进行动画处理.剪裁和显示动画更加容易,因为您可以使用一个动画对整个矩形进行动画处理.

希望对您有所帮助.


Hi all

I am sure I''ve seen a way to do an animation in WPF similar to how Cocoa does it, where I do it all in one line, using a static method telling the object what I want moved. However, all the examples I can find on MSDN are creating storyboards, etc. Am I right ? Is there a way to do simple, dynamic animations without creating a lot of objects ?

I found this:

DoubleAnimation da = new DoubleAnimation();
da.From = this.Top;
da.To = height - 50;
da.Duration = new Duration(TimeSpan.FromSeconds(1));

this.BeginAnimation(Window.TopProperty, da);




If this is how I do things dynamically, how do I animate more than one thing at once ? I guess that needs a StoryBoard and I''m back where I started ?

解决方案

To animate more than one property at once - yes you''ll need a Storyboard.

As for moving an element, the difficulty lies with the element not really having a set Left/Top property. In WPF everything is based on Margins and Alignments.

I''ve never tried it, but you might be able to setup an animation on a margin. Though you''ll most likely end up with some stretching effects.

The way I animate a move is to use a TranslateTransform.

You''ll need an animation for each axis you want to move on so for moving on a diagonal you''ll need a total of 2 animations.

Here is a quick example of how to move an element 150 on a diagonal:

Sorry about the VB - though it shouldn''t be hard to convert to C#

''This creates the new TranslateTransform that changes how the element is rendered
'' (x = 0, y = 0) means no translation is done
''[new] is the element to be animated
[new].RenderTransform = New TranslateTransform()

''The animation requires a NameScope within which to operate, as the animations must be using a registered name for the object
Dim ns As New NameScope()
NameScope.SetNameScope(_manager.Panel, ns) ''_manager.Panel in my case was the container of the element
ns.RegisterName("newTransform", [new].RenderTransform) ''Register the name

Dim board As New Storyboard()
Dim animLength As New TimeSpan(0, 0, 0, 0, 1500) ''1.5 seconds

''Handle YProperty (moving 150 units down)
''A FillBehavior of Stop resets the animation after it has completed
''A FillBehavior of Hold keeps the element at the intended target, but you won''t be able to manually set the property in question anymore
Dim dblNewY As New DoubleAnimation(150, New Duration(animLength), FillBehavior.Stop) 
Storyboard.SetTargetName(dblNewY, "newTransform") ''tell the animation what to animate against
Storyboard.SetTargetProperty(dblNewY, New PropertyPath(TranslateTransform.YProperty)) ''tell the animation which property
board.Children.Add(dblNewY)

''Handle XProperty (moving 150 units to the right)
Dim dblNewX As New DoubleAnimation(150, New Duration(animLength), FillBehavior.Stop)
Storyboard.SetTargetName(dblNewX, "newTransform")
Storyboard.SetTargetProperty(dblNewX, New PropertyPath(TranslateTransform.XProperty))
board.Children.Add(dblNewX)
board.Begin(_manager.Panel) ''the storyboard wanted a reference point for the namespace - this works with the NameSpace registered above and is again the container of the element to animate


I realize this is creating a number of objects, but it is a dynamic animation. I''ve been using some of this to create a whole animation framework for animating elements on/off of a given container. Clipping and reveal animations are easier as you can animate a whole rectangle with one animation.

I hope that helps you out.


这篇关于WPF中的动态动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆