通过代码在 WPF 中对 MatrixTransform 进行动画处理

发布于 2024-07-22 06:45:53 字数 841 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-07-29 06:45:53

我已经实现了 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

感情废物 2024-07-29 06:45:53

今天早上我遇到了这个问题,尽管我使用的解决方案无法应对旋转或剪切。 链接

I bumped into this problem this morning, although the solution I used won't cope with rotations or shearing. link

简单 2024-07-29 06:45:53

我设法通过设置渲染源并使用 beginanimation 来使矩阵转换工作,

如下所示:

        this.matrixTransform = new MatrixTransform();
        this.canvas.RenderTransform = this.matrixTransform;


        MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
        anim.KeyFrames = new MatrixKeyFrameCollection();
        anim.Duration = TimeSpan.FromSeconds(4);

        Matrix fromMatrix = new Matrix(2, 0, 0, 2, 0, 0);
        Matrix toMatrix =  new Matrix(3, 0, 0, 3, 0, 0);

        anim.FillBehavior = FillBehavior.HoldEnd;
        DiscreteMatrixKeyFrame start = new DiscreteMatrixKeyFrame(fromMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)));
        DiscreteMatrixKeyFrame end = new DiscreteMatrixKeyFrame(toMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4)));

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

        this.matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, anim);

但不确定我将如何自己对所有关键帧进行插值:)

I managed to get matrixtransform working by setting rendersource and using beginanimation

something like this:

        this.matrixTransform = new MatrixTransform();
        this.canvas.RenderTransform = this.matrixTransform;


        MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
        anim.KeyFrames = new MatrixKeyFrameCollection();
        anim.Duration = TimeSpan.FromSeconds(4);

        Matrix fromMatrix = new Matrix(2, 0, 0, 2, 0, 0);
        Matrix toMatrix =  new Matrix(3, 0, 0, 3, 0, 0);

        anim.FillBehavior = FillBehavior.HoldEnd;
        DiscreteMatrixKeyFrame start = new DiscreteMatrixKeyFrame(fromMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)));
        DiscreteMatrixKeyFrame end = new DiscreteMatrixKeyFrame(toMatrix, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4)));

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

        this.matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, anim);

Not sure exactly how I'm going to do the interpolation for all the keyframes myself though :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文