1

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

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

2 回答 2

8

最好使用缩放变换进行缩放,但是如果您坚持为 W/H 设置动画,您可以这样做,因为这些是正常的 DP,您可以使用标准的 DoubleAnimation/DoubleAnimationUsingKeyFrames 为它们设置动画

DoubleAnimation doubleAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromMilliseconds(500)));
        this.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);
于 2012-09-23T11:23:03.787 回答
1

使用ScaleTransform,不需要Height&Width动画,ScaleTransform会影响你的边框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);

    }
于 2012-09-23T11:15:05.970 回答