2

我有一个 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();
4

3 回答 3

5

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

于 2010-12-06T21:24:41.000 回答
2

我设法通过设置渲染源和使用 beginanimation 来让 matrixtransform 工作

像这样的东西:

        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);

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

于 2009-12-30T23:08:21.687 回答
2

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

于 2010-04-19T16:15:30.117 回答