-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressBar.cs
150 lines (121 loc) · 4.71 KB
/
ProgressBar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApp
{
public class ProgressBar : GraphicsView
{
private readonly ProgressBarDrawable _progressDrawable;
public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(double), typeof(ProgressBarDrawable), 0.0, BindingMode.TwoWay,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is ProgressBar progressControl)
{
progressControl.UpdateProgress();
}
});
public static readonly BindableProperty ProgressBrushProperty = BindableProperty.Create(nameof(ProgressBrush), typeof(Brush), typeof(ProgressBarDrawable), new SolidColorBrush(Colors.Blue),
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is ProgressBar progressBar)
{
progressBar.UpdateProgressBrush();
}
});
public static readonly BindableProperty StrokeBrushProperty = BindableProperty.Create(nameof(StrokeBrush), typeof(Brush), typeof(ProgressBarDrawable), new SolidColorBrush(Colors.Gray),
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is ProgressBar progressBar)
{
progressBar.UpdateStrokeBrush();
}
});
public double Progress
{
get => (double)GetValue(ProgressProperty);
set => SetValue(ProgressProperty, value);
}
public Brush ProgressBrush
{
get { return (Brush)GetValue(ProgressBrushProperty); }
set { SetValue(ProgressBrushProperty, value); }
}
public Brush StrokeBrush
{
get { return (Brush)GetValue(StrokeBrushProperty); }
set { SetValue(StrokeBrushProperty, value); }
}
public ProgressBar()
{
Drawable = _progressDrawable = new ProgressBarDrawable();
}
protected override void OnParentChanged()
{
base.OnParentChanged();
if (Parent != null)
{
UpdateStrokeBrush();
UpdateProgressBrush();
UpdateProgress();
}
}
protected void UpdateProgress()
{
if (_progressDrawable == null)
{
return;
}
_progressDrawable.Progress = Progress;
Invalidate();
}
void UpdateStrokeBrush()
{
if (_progressDrawable == null)
{
return;
}
_progressDrawable.StrokePaint = StrokeBrush;
Invalidate();
}
void UpdateProgressBrush()
{
if (_progressDrawable == null)
{
return;
}
_progressDrawable.ProgressPaint = ProgressBrush;
Invalidate();
}
public class ProgressBarDrawable : IDrawable
{
public Paint StrokePaint { get; set; }
public Paint ProgressPaint { get; set; }
public double Progress { get; set; }
public CornerRadius ProgressCornerRadius { get; set; } = 6f;
public CornerRadius CornerRadius { get; set; } = 6f;
public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.Antialias = true;
DrawTrack(canvas, dirtyRect);
DrawProgress(canvas, dirtyRect);
}
public void DrawTrack(ICanvas canvas, RectF dirtyRect)
{
canvas.SaveState();
canvas.SetFillPaint(StrokePaint, dirtyRect);
canvas.FillRoundedRectangle(dirtyRect, CornerRadius.TopLeft, CornerRadius.TopRight, CornerRadius.BottomLeft, CornerRadius.BottomRight);
canvas.RestoreState();
}
public void DrawProgress(ICanvas canvas, RectF dirtyRect)
{
canvas.SaveState();
RectF rect = new Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width * Progress, dirtyRect.Height);
canvas.SetFillPaint(ProgressPaint, dirtyRect);
canvas.FillRoundedRectangle(rect, ProgressCornerRadius.TopLeft, ProgressCornerRadius.TopRight, ProgressCornerRadius.BottomLeft, ProgressCornerRadius.BottomRight);
canvas.RestoreState();
}
}
}
}