【问题标题】:WPF Animate width and Height with code behind (Zoom in)WPF 动画宽度和高度,后面有代码(放大)
【发布时间】:2012-09-23 22:54:36
【问题描述】:

我能够为边框的移动设置动画:

private void MoveTo(Border target, double newX, double newY)
{
    Vector offset = VisualTreeHelper.GetOffset(target);
    var top = offset.Y;
    var left = offset.X;
    TranslateTransform trans = new TranslateTransform();
    target.RenderTransform = trans;
    DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromMilliseconds(500));
    DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromMilliseconds(500));
    trans.BeginAnimation(TranslateTransform.YProperty, anim1);
    trans.BeginAnimation(TranslateTransform.XProperty, anim2);
}

但我希望能够对高度和宽度以及位置的增加进行动画处理,以给人一种放大图像的印象(在我的案例和上面的示例中,它包含在边框中)。

这是否可能与背后的代码?


好的,我尝试了缩放变换,但它似乎没有做任何事情 - 我需要故事板吗?

    private void Zoom(Border target)
    {   
        TranslateTransform trans = new TranslateTransform();
        target.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
        DoubleAnimation anim2 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
        trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim1);
        trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim2);
    }

【问题讨论】:

    标签: c# wpf animation code-behind


    【解决方案1】:

    最好使用缩放变换进行缩放,但如果您坚持设置 W/H 动画,则可以这样做,因为这些是普通 DP,您可以使用标准 DoubleAnimation/DoubleAnimationUsingKeyFrames 对其进行动画处理 像

    DoubleAnimation doubleAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromMilliseconds(500)));
            this.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);
    

    【讨论】:

      【解决方案2】:

      使用ScaleTransform,不需要HeightWidth动画,ScaleTransform会影响你的Border的VisualTree,所以内部图像也会被拉伸。

          private void Zoom(Border target)
          {
              ScaleTransform trans = new ScaleTransform();
              target.RenderTransform = trans;
              // if you use the same animation for X & Y you don't need anim1, anim2 
              DoubleAnimation anim = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
              trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim);
              trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
      
          }
      

      【讨论】:

      • 感谢您的回复 - 我试过这个,但它似乎没有做任何事情。我已经用我的尝试更新了我的答案。
      • 不要使用TranslateTransform trans = new TranslateTransform(); 而是使用ScaleTransform trans = new ScaleTransform(); 更新了我的代码,它完美地工作@DavidRelihan
      猜你喜欢
      • 2012-09-02
      • 1970-01-01
      • 2011-11-03
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多