从代码中为WPF中的MatrixTransform制作动画

Twe*_*e47 2 c# wpf animation transform matrix

我有一个Canvas,我需要为它的RenderTransform属性设置动画.开始和结束矩阵都是abitrary,所以我不能预先在XAML中编写故事板,所以我试图在代码中执行它,我找不到任何如何执行此操作的示例,下面是我最好的尝试哪个不起作用(它编译并运行,但rendertransform不会改变).

有关如何做到这一点的任何建议?

MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
MatrixKeyFrameCollection keyframes = new MatrixKeyFrameCollection();
DiscreteMatrixKeyFrame start = new DiscreteMatrixKeyFrame(fromMatrix, KeyTime.FromPercent(0));
DiscreteMatrixKeyFrame end = new DiscreteMatrixKeyFrame(toMatrix, KeyTime.FromPercent(1));

keyframes.Add(start);
keyframes.Add(end);
anim.KeyFrames = keyframes;

Storyboard.SetTarget(anim, World.RenderTransform);
Storyboard.SetTargetProperty(anim, new PropertyPath("Matrix"));

Storyboard sb = new Storyboard();
sb.Children.Add(anim);
sb.Duration = TimeSpan.FromSeconds(4);
sb.Begin();
Run Code Online (Sandbox Code Playgroud)

小智 5

我已经实现了MatrixAnimation类,它支持平滑的平移,缩放和旋转动画.它还支持缓动功能!在http://pwlodek.blogspot.com/2010/12/matrixanimation-for-wpf.html上找到