【问题标题】:Animating a MatrixTransform in WPF from code从代码中为 WPF 中的 MatrixTransform 设置动画
【发布时间】:2010-10-29 05:59:49
【问题描述】:

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

【问题讨论】:

    标签: c# wpf animation matrix transform


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这对我的问题来说很好,至少是缩放和翻译。
      【解决方案3】:

      我设法通过设置渲染源和使用 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);
      

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

      【讨论】:

        猜你喜欢
        • 2011-04-15
        • 2013-08-27
        • 2017-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多