WPF 位置转化和动画

位置转化

        private void DrawScale()
        {
            double majorTickUnitValue = this.ScaleSweepLenth / this.MajorDivisionsCount;
            double minorTickUnitValue = this.ScaleSweepLenth / this.MinorDivisionsCount;
            double correctionOffset = this.rootGrid.Width / 2 ;

            Double minvalue = MinValue; ;

            //画主刻度
            for (int i = 0; i < this.MajorDivisionsCount; i++)
            {
                Rectangle majorTickRect = new Rectangle();
                majorTickRect.Height = this.MajorTickSize.Height;
                majorTickRect.Width = this.MajorTickSize.Width;
                majorTickRect.Fill = new SolidColorBrush(this.MajorTickColor);

                TransformGroup majorTickTransformGroup = new TransformGroup();
                TranslateTransform majorTickTranslateTransform = new TranslateTransform();

                majorTickTranslateTransform.X = i * majorTickUnitValue - correctionOffset;
                majorTickTranslateTransform.Y = this.MajorMinorDivisionOffset;

                majorTickTransformGroup.Children.Add(majorTickTranslateTransform);
                majorTickRect.RenderTransform = majorTickTransformGroup;

                this.rootGrid.Children.Add(majorTickRect);
            }

        }

动画

        private void MovePointerUsingAnimate(double oldValue, double newValue)
        {
            if (null != this.pointer)
            {
                double distanceOldAndNew = Math.Abs(newValue - oldValue);
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                double animDuration = 0.0f;
                Storyboard movingPointerStoryboard = new Storyboard();
                TransformGroup transformGroup = new TransformGroup();
                TranslateTransform transform = new TranslateTransform();
           
                doubleAnimation.From = oldValue;
                doubleAnimation.To = newValue;
                animDuration = distanceOldAndNew * animatingSpeedFactor;
                doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(animDuration));

                movingPointerStoryboard.Completed +=new EventHandler(MovingPointerStoryboardStoryboard_Completed);
                movingPointerStoryboard.Children.Add(doubleAnimation);
                Storyboard.SetTarget(doubleAnimation, this.pointer);

                transformGroup.Children.Add(transform);
                this.pointer.RenderTransform = transformGroup;

                Storyboard.SetTargetProperty(doubleAnimation,
                    new PropertyPath("(Path.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));

                if (Math.Abs(oldValue - newValue) > 0)
                {
                    movingPointerStoryboard.Begin();
                }

            }
        }
原文地址:https://www.cnblogs.com/easy5weikai/p/3335519.html