首页 > 代码库 > RectAnimation用于在DrawingVisual画进度条

RectAnimation用于在DrawingVisual画进度条

使用Visual来画图,可以使用其派生类,UIElement、Viewport3DVisual用于呈现3D内容,其他可以用来画图的为DrawingVisual,使用DrawingVisual可以使用编程的方式来实现复杂的图形实现,当使用DrawingVisual,必须使用FrameworkElement作为宿主容器,用来提供DrawingVisual所缺乏的布局和事件支持。下面实现了使用DrawingVisual来实现进度条,主要使用RectAnimation来实现。DrawRoundedRectangle(Brush brush, Pen pen, Rect rectangle, AnimationClock rectangleAnimations, double radiusX, AnimationClock radiusXAnimations, double radiusY, AnimationClock radiusYAnimations)方法的使用RectAnimation对象的AnimationClock对 Rec进行动画处理。

        RectAnimation myRectAnimation;
        public override System.Windows.Media.Visual drawShape()
        {
                DrawingVisual drawingWordsVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingWordsVisual.RenderOpen();
                try
                {
                        Rect rect2 = new Rect(currentLayout.StartPoint.X,
                        currentLayout.StartPoint.Y, VisusalSize.Width, 3);
                        Rect rect3 = new Rect(currentLayout.StartPoint.X, 
                        currentLayout.StartPoint.Y, VisusalSize.Width, 3);
                        
                        Rect rect1 = new Rect(currentLayout.StartPoint.X, currentLayout.StartPoint.Y, 0, 3);
                        //Rect rect4 = new Rect(currentLayout.StartPoint.X, currentLayout.StartPoint.Y, VisusalSize.Width*0.5, 0);

                        myRectAnimation = new RectAnimation();

                        myRectAnimation.Duration = TimeSpan.FromSeconds(10);
                        myRectAnimation.FillBehavior = FillBehavior.HoldEnd;

                        myRectAnimation.From = rect1;
                        //myRectAnimation.By = rect4;
                        myRectAnimation.To = rect3;

                        _showFilePanelClock = myRectAnimation.CreateClock();

                        _showFilePanelClock.Controller.Pause();

                        GradientStop gs1 = new GradientStop(Color.FromRgb(49, 158, 
                        222), 1);
                        GradientStop gs2 = new GradientStop(Color.FromRgb(69, 182, 
                        233), 1);
                        GradientStopCollection gsc = new GradientStopCollection();
                        gsc.Add(gs1);
                        gsc.Add(gs2);
                        Brush colorbrush = new LinearGradientBrush(gsc,
                         new Point(0, 1), new Point(0, 0.8));
                        Brush solidBrush = new SolidColorBrush(Color.FromRgb(203, 
                        203, 203));
                        drawingContext.DrawRectangle(solidBrush, new Pen(), rect2);
                        drawingContext.DrawRoundedRectangle(colorbrush, new Pen(),
                        rect1, _showFilePanelClock, 2, null, 2, null);
                }
                catch (Exception ex)
                {
                        new SaveExceptionInfo().SaveLogAsTXTInfoex(ex.Message);
                }
                finally
                {
                        drawingContext.Close();
                }
                return drawingWordsVisual;
        }

使用下面的方法来控制进度条的进度,根据要显示的百分比,使用ClockController.Seek来控制进度条的进度。

public void FilesavedSize_saveSizeChanged(object sender, SendFileEventArgs args)
        {
             double percent = double.Parse(args.SavedSize.ToString())
             / double.Parse(args.FileSize.ToString());
             TimeSpan timeSpan = TimeSpan.FromMilliseconds(percent * 10000);
             _showFilePanelClock.Controller.Seek(timeSpan,
              TimeSeekOrigin.BeginTime);
             _showFilePanelClock.Controller.Pause();
        }