通过代码在 WPF 中对 MatrixTransform 进行动画处理
我有一个 Canvas,我需要对其 RenderTransform 属性进行动画处理。 开始和结束矩阵将是任意的,所以我无法在 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();
I have a Canvas which I would need to animate the RenderTransform property of. The start and end matrices will be abitrary, so I can't pre write the storyboard in XAML, so I'm trying to do it in code, I can't find any example of how to do this, below is my best try which does not work (it compiles and runs, but the rendertransform does not change).
Any suggestions on how this should be done?
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经实现了 MatrixAnimation 类,它支持平滑的平移、缩放和旋转动画。 它还支持缓动功能! 可以在 http://pwodek.blogspot.com/2010/12/matrixanimation-for-wpf 查找。 html
I have implemented MatrixAnimation class which supports smooth translation, scaling and rotation animations. It also supports easing functions! Find at http://pwlodek.blogspot.com/2010/12/matrixanimation-for-wpf.html
今天早上我遇到了这个问题,尽管我使用的解决方案无法应对旋转或剪切。 链接
I bumped into this problem this morning, although the solution I used won't cope with rotations or shearing. link
我设法通过设置渲染源并使用 beginanimation 来使矩阵转换工作,
如下所示:
但不确定我将如何自己对所有关键帧进行插值:)
I managed to get matrixtransform working by setting rendersource and using beginanimation
something like this:
Not sure exactly how I'm going to do the interpolation for all the keyframes myself though :)