|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace BPASmartClient.MilkWithTea.Control
- {
- /// <summary>
- /// CircularProgressBar.xaml 的交互逻辑
- /// </summary>
- public partial class CircularProgressBar : UserControl
- {
- public CircularProgressBar()
- {
- InitializeComponent();
- UpdateValue(Percent * 3.6);
- }
- public double Percent
- {
- get { return (double)GetValue(PercentProperty); }
- set
- {
- SetValue(PercentProperty, value);
- UpdateValue(Percent * 3.6);
- }
-
- }
-
- //public static DependencyProperty PercentProperty => percentProperty;
-
- // Using a DependencyProperty as the backing store for Percent. This enables animation, styling, binding, etc...
- private static readonly DependencyProperty PercentProperty =
- DependencyProperty.Register("Percent", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(default(double), new PropertyChangedCallback(OnPropertyChanged)));
-
-
- private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- CircularProgressBar _cycleProgess = d as CircularProgressBar;
- _cycleProgess.UpdateValue(_cycleProgess.Percent * 3.6);
- }
- private void UpdateValue(double angle)
- {
-
- if (angle > 360) return;
- if (angle < 0) return;
-
- int offset = 5;
- if (angle > 359) angle = 359.8;
- double radius = this.Width / 2 - offset;
- if (radius < 0) return;
- double b = radius * Math.Sin(angle * Math.PI / 180);
- double x = radius + b;
- double y = radius - radius * Math.Cos(angle * Math.PI / 180);
- double startPositionX = radius + offset;
- double startPositionY = offset;
-
- Storyboard sb = new Storyboard();
- DoubleAnimation yd1 = new DoubleAnimation();//实例化浮点动画
- cyclePath.RenderTransform = new RotateTransform();//设置为旋转动画
- cyclePath.RenderTransformOrigin = new Point(0.5, 0.5);//设置旋转的中心
- yd1.From = 0;//动画的起始值
- yd1.To = 360;//动画的结束值
- yd1.Duration = TimeSpan.FromSeconds(3);//动画的播放时间
- yd1.RepeatBehavior = RepeatBehavior.Forever;//设置动画循环播放
- Storyboard.SetTarget(yd1, cyclePath);//给故事板绑定动画
- Storyboard.SetTargetProperty(yd1, new PropertyPath("RenderTransform.Angle"));//动画的依赖属性
- sb.Children.Add(yd1);//故事板添加动画
-
- if (angle == 0)
- {
- string Data = string.Format("M {0} {1} A {2} {2} 0 1 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
- this.cyclePath.Data = Geometry.Parse(Data);
-
- sb.Begin();//播放动画
- }
-
- else if (angle > 180)
- {
-
- //string Data = string.Format("M {0} 0 A {0} {0} 0 1 1 {1} {2}", radius, Math.Round(x, 2), Math.Round(y, 2));
- string Data = string.Format("M {0} {1} A {2} {2} 0 1 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
- this.cyclePath.Data = Geometry.Parse(Data);
- sb.Stop();
- }
- else
- {
-
- //string Data = string.Format("M {0} 0 A {0} {0} 0 0 1 {1} {2}", radius, Math.Round(x,2), Math.Round(y, 2));
-
- string Data = string.Format("M {0} {1} A {2} {2} 0 0 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
-
- this.cyclePath.Data = Geometry.Parse(Data);
- sb.Stop();
- }
-
-
- }
-
- }
- }
-
|