RectAnimation用于在DrawingVisual画进度条

时间:2023-03-09 18:47:41
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, );
Rect rect3 = new Rect(currentLayout.StartPoint.X,
                        currentLayout.StartPoint.Y, VisusalSize.Width, );

                        Rect rect1 = new Rect(currentLayout.StartPoint.X, currentLayout.StartPoint.Y, , );
//Rect rect4 = new Rect(currentLayout.StartPoint.X, currentLayout.StartPoint.Y, VisusalSize.Width*0.5, 0); myRectAnimation = new RectAnimation(); myRectAnimation.Duration = TimeSpan.FromSeconds();
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(, ,
                        ), );
GradientStop gs2 = new GradientStop(Color.FromRgb(, ,
                        ), );
GradientStopCollection gsc = new GradientStopCollection();
gsc.Add(gs1);
gsc.Add(gs2);
Brush colorbrush = new LinearGradientBrush(gsc,
                         new Point(, ), new Point(, 0.8));
Brush solidBrush = new SolidColorBrush(Color.FromRgb(,
                        , ));
drawingContext.DrawRectangle(solidBrush, new Pen(), rect2);
drawingContext.DrawRoundedRectangle(colorbrush, new Pen(),
                        rect1, _showFilePanelClock, , null, , 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 * );
_showFilePanelClock.Controller.Seek(timeSpan,
              TimeSeekOrigin.BeginTime);
_showFilePanelClock.Controller.Pause();
}