WPF动画宽度和高度,后面有代码(放大)

Rob*_*boy 1 c# wpf animation code-behind

我能够为边界的移动制作动画:

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);
}
Run Code Online (Sandbox Code Playgroud)

但我希望能够设置高度和宽度的增加以及位置的动画,以给出放大图像的印象(在我的情况和上面的例子中包含在边框中)).

这背后有代码吗?


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

    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);
    }
Run Code Online (Sandbox Code Playgroud)

Seb*_*mel 8

最好使用缩放变换进行缩放,但是如果你坚持使用W/H动画你就可以做到,因为这些是普通的DP,你可以使用标准的DoubleAnimation/DoubleAnimationUsingKeyFrames为它们设置动画.

DoubleAnimation doubleAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromMilliseconds(500)));
        this.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);
Run Code Online (Sandbox Code Playgroud)