在WPF中设置RenderTransform动画

时间:2014-10-15 18:31:59

标签: c# wpf animation rendertransform

我在C#代码中有关于WPF动画的问题!
我有一个MouseWheel事件的处理函数。它只是检查你是否'放大'或'缩小'。看看代码,这里的重要一行是第4行,其中设置了RenderTransform。

    private void ZoomPanCanvas_MouseWheel(object sender, MouseWheelEventArgs e) {
        var factor = (e.Delta > 0) ? (1.1) : (1 / 1.1);
        currrentScale = factor * currrentScale;
        mNetworkUI.RenderTransform = new ScaleTransform(currrentScale, currrentScale);
        var pos = e.GetPosition(mNetworkUI);
        mNetworkUI.Width = ZoomPanCanvas.ActualWidth / currrentScale;
        mNetworkUI.Height = ZoomPanCanvas.ActualHeight /currrentScale;
        var dummyTransform = new ScaleTransform(factor, factor, pos.X, pos.Y);
        var offSet = new Point(dummyTransform.Value.OffsetX, dummyTransform.Value.OffsetY);
        mNetworkUI.ViewModel.Network.SetTransformOffset(offSet);
    }

我将代码的其余部分保留在代码中以保证完整性 我想做的是为RenderTransform的这个变化设置动画

我已经尝试使用Storyboard(设置了UIElement.RenderTransformProperty)。最好的结果是RenderTransform的一个没有动画的变化(但它与代码实现的第四行的结果不同)。

也许你可以帮助我,我已经从其他问题中尝试了一些建议的方法。

修改

这是非工作尝试,首先是来自上面的变换代码:

    private void ZoomPanCanvas_MouseWheel(object sender, MouseWheelEventArgs e) {
        var factor = (e.Delta > 0) ? (1.1) : (1 / 1.1);
        currrentScale = factor * currrentScale;
        ///mNetworkUI.RenderTransform = new ScaleTransform(currrentScale, currrentScale);
        Helper.Animations.RenderTransformAnimation(mNetworkUI, new ScaleTransform(currrentScale, currrentScale));
        var pos = e.GetPosition(mNetworkUI);
        mNetworkUI.Width = ZoomPanCanvas.ActualWidth / currrentScale;
        mNetworkUI.Height = ZoomPanCanvas.ActualHeight /currrentScale;
        var dummyTransform = new ScaleTransform(factor, factor, pos.X, pos.Y);
        var offSet = new Point(dummyTransform.Value.OffsetX, dummyTransform.Value.OffsetY);
        mNetworkUI.ViewModel.Network.SetTransformOffset(offSet);
    }

它遵循静态辅助函数:

    public static void RenderTransformAnimation(FrameworkElement element, Transform newTransform) {
        MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
        var key1 = new DiscreteMatrixKeyFrame(element.RenderTransform.Value, KeyTime.FromPercent(0));
        var key2 = new DiscreteMatrixKeyFrame(newTransform.Value, KeyTime.FromPercent(1));

        Storyboard.SetTarget(anim, element.RenderTransform);
        Storyboard.SetTargetProperty(anim, new PropertyPath(UIElement.RenderTransformProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(anim);
        sb.Duration = AnimationDuration;
        sb.Begin();
    }

它总是在sb.Begin()调用上抛出一个异常,告诉我,我的'PropertyPath'有些不对劲。我不知道怎么做:(。 我的意思是,没有办法直接创建“TransformAnimation”,对吗?只有MatrixAnimations可用...

1 个答案:

答案 0 :(得分:2)

我在下面提供了一个ScaleTransform的简单动画。为了提供一个“最小”的例子,我只调整了规模;我没有根据鼠标位置进行任何偏移计算。你应该能够从这里找出去处:

private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
    var factor = e.Delta > 0d ? 1.1d : 0.9d;

    var t = mNetworkUI.RenderTransform as ScaleTransform;
    if (t == null)
    {
        mNetworkUI.RenderTransform = t = new ScaleTransform(1d, 1d)
                                         {
                                             CenterX = 0.5d,
                                             CenterY = 0.5d
                                         };
    }

    var oldScale = (double)t.GetAnimationBaseValue(ScaleTransform.ScaleXProperty);
    var newScale = oldScale * factor;

    //
    // Make sure `GetAnimationBaseValue()` reflects the `To` value next time
    // (needed to calculate `oldScale`, and for the animation to infer `From`).
    //
    t.ScaleX = newScale;
    t.ScaleY = newScale;

    var animation = new DoubleAnimation
                    {
                        To = newScale,
                        Duration = TimeSpan.FromSeconds(0.5d),
                        DecelerationRatio = 0.5d,
                        FillBehavior = FillBehavior.Stop
                    };

    //
    // Use `HandoffBehavior.Compose` to transition more smoothly if an animation
    // is already in progress.
    //
    t.BeginAnimation(
        ScaleTransform.ScaleXProperty, 
        animation, 
        HandoffBehavior.Compose);

    t.BeginAnimation(
        ScaleTransform.ScaleYProperty,
        animation,
        HandoffBehavior.Compose);
}