WPF变换混乱

时间:2009-12-27 19:07:34

标签: wpf animation transform

我有一个装满物体的画布,我可以使用

进行缩放和平移
       this.source = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
        this.zoomTransform = new ScaleTransform();
        this.transformGroup = new TransformGroup();
        this.transformGroup.Children.Add(this.zoomTransform);
        this.transformGroup.Children.Add(this.translateTransform);
        this.source.RenderTransform = this.transformGroup;

然后我有一个方法将画布移动到某个点(在原始坐标中)到屏幕中心:

  public void MoveTo(Point p)
    {
        var parent= VisualTreeHelper.GetParent(this) as FrameworkElement;
        Point centerPoint = new Point(parent.ActualWidth / 2, parent.ActualHeight / 2);

        double x = centerPoint.X  - p.X;
        double y = centerPoint.Y - p.Y;

        x *= this.zoomTransform.ScaleX;
        y *= this.zoomTransform.ScaleY;

        this.translateTransform.BeginAnimation(TranslateTransform.XProperty, CreatePanAnimation(x), HandoffBehavior.Compose);
        this.translateTransform.BeginAnimation(TranslateTransform.YProperty, CreatePanAnimation(y), HandoffBehavior.Compose);
    }

 private DoubleAnimation CreatePanAnimation(double toValue)
    {
        var da = new DoubleAnimation(toValue, new Duration(TimeSpan.FromMilliseconds(300)));
        da.AccelerationRatio = 0.1;
        da.DecelerationRatio = 0.9;
        da.FillBehavior = FillBehavior.HoldEnd;
        da.Freeze();
        return da;
    }

一切都很有效,直到我实际上有一个缩放动画激活,之后平移动画是不准确的。我已经尝试了不同的计算方法x,y和中心点,但似乎无法做到正确。任何帮助表示赞赏,应该很简单:)

我还想制作一种方法,既可以缩放动画也可以平移到某个点,对于完成该操作的顺序有点不确定

1 个答案:

答案 0 :(得分:0)

没关系,我很蠢

Point centerPoint = new Point(parent.ActualWidth / 2 / this.zoomTransform.ScaleX, parent.ActualHeight / 2 / this.zoomTransform.ScaleY);

我仍然对如何组合缩放和缩放动画感兴趣

this.translateTransform.BeginAnimation(TranslateTransform.XProperty, CreatePanAnimation(x), HandoffBehavior.Compose);
this.translateTransform.BeginAnimation(TranslateTransform.YProperty, CreatePanAnimation(y), HandoffBehavior.Compose);

 this.zoomTransform.BeginAnimation(ScaleTransform.ScaleXProperty,    CreateZoomAnimation(factor));
 this.zoomTransform.BeginAnimation(ScaleTransform.ScaleYProperty,  CreateZoomAnimation(factor));

不会工作,因为比例和平移值不会同步...