代码之家  ›  专栏  ›  技术社区  ›  Homde

WPF变换混淆

  •  0
  • Homde  · 技术社区  · 16 年前

    我有一张画布,上面有我可以缩放和平移的对象

           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 回复  |  直到 16 年前
        1
  •  0
  •   Patrick D'Souza ob1    13 年前

    没关系,我很蠢

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

    无法工作,因为缩放和平移值无法同步。。。