【问题标题】:WPF transform confusionWPF 转换混淆
【发布时间】:2009-12-27 19:07:34
【问题描述】:

我有一个充满对象的画布,我可以使用这些对象进行缩放和平移

       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 和中心点的方法,但似乎无法正确计算。任何帮助表示赞赏,应该很简单:)

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

【问题讨论】:

    标签: wpf animation transform


    【解决方案1】:

    没关系,我很笨

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      相关资源
      最近更新 更多