我有一个 Canvas,我需要为它的 RenderTransform 属性设置动画。开始和结束矩阵是任意的,所以我不能用 XAML 预先编写 Storyboard,所以我试图用代码来做,我找不到任何如何做到这一点的例子,下面是我最好的尝试这不起作用(它编译并运行,但 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();
最佳答案
我已经实现了支持平滑平移、缩放和旋转动画的 MatrixAnimation 类。它还支持缓动功能!查找 http://pwlodek.blogspot.com/2010/12/matrixanimation-for-wpf.html
关于c# - 从代码中为 WPF 中的 MatrixTransform 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/915212/