黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 行条类型的进度条控件
    /// </summary>
    public class ProgressRowBar : FrameLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 进度条能否往回走(默认不可以)
        /// </summary>
        public bool ProgressBarGoback = false;
        /// <summary>
        /// 会移动的进度条
        /// </summary>
        private FrameLayout btnProgressBar = null;
        /// <summary>
        /// 数值百分比文本的容器
        /// </summary>
        private FrameLayout frameProgressBack = null;
        /// <summary>
        /// 显示数值百分比的控件
        /// </summary>
        private NormalViewControl btnProgressTextView = null;
        /// <summary>
        /// 线程是否运行
        /// </summary>
        private bool isThreadAction = false;
        /// <summary>
        /// 模式区分
        /// </summary>
        private int m_ModeDiv = -1;
 
        /// <summary>
        /// 进度条是否可视
        /// </summary>
        public new bool Visible
        {
            get { return base.Visible; }
            set
            {
                if (this.frameProgressBack != null)
                {
                    this.frameProgressBack.Visible = value;
                }
                base.Visible = value;
            }
        }
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 行条类型的进度条控件
        /// </summary>
        /// <param name="width">
        /// <para>模式1:会动的那个进度条的宽度(非真实值)</para>
        /// <para>模式2:进度条在持续无限的来回移动的区域宽度(非真实值)</para>
        /// </param>
        /// <param name="height">
        /// <para>模式1:会动的那个进度条的高度(非真实值)</para>
        /// <para>模式2:进度条在持续无限的来回移动的高度(非真实值)</para>
        /// </param>
        public ProgressRowBar(int width, int height)
        {
            this.Height = Application.GetRealHeight(height);
            this.Width = Application.GetRealWidth(width);
            this.BackgroundColor = 0xffe6e6e6;
            this.Radius = (uint)Application.GetRealHeight(height) / 2;
        }
 
        #endregion
 
        #region ■ 模式1______________________________
 
        /// <summary>
        /// 模式1  该模式为:手动填写进度值
        /// </summary>
        /// <param name="showText">
        /// <para>是否在进度条上方显示数值百分比</para>
        /// <para>请确保控件的上方有足够的区域(注:请不要扩大此控件的高度)</para>
        /// </param>
        public void StartMode1(bool showText = false)
        {
            if (m_ModeDiv != -1) { return; }
            this.m_ModeDiv = 1;
 
            //会移动的进度条
            this.btnProgressBar = new FrameLayout();
            btnProgressBar.Width = 0;
            btnProgressBar.Height = this.Height;
            btnProgressBar.BackgroundColor = 0xfffb744a;
            btnProgressBar.Radius = (uint)this.Height / 2;
            this.AddChidren(btnProgressBar);
 
            if (showText == true)
            {
                //进度值文本
                this.frameProgressBack = new FrameLayout();
                frameProgressBack.Width = Application.GetRealWidth(120);
                frameProgressBack.Height = Application.GetRealHeight(60);
                frameProgressBack.Y = this.Y - Application.GetRealHeight(60);
                frameProgressBack.X = this.X - Application.GetRealWidth(84) / 2;
                this.Parent.AddChidren(frameProgressBack);
 
                var btnProgressPic = new PicViewControl(84, 60);
                btnProgressPic.UnSelectedImagePath = "Item/ProgressMsg.png";
                btnProgressPic.Gravity = Gravity.CenterHorizontal;
                frameProgressBack.AddChidren(btnProgressPic);
                this.btnProgressTextView = new NormalViewControl(120, 45, true);
                btnProgressTextView.TextSize = 10;
                btnProgressTextView.TextAlignment = TextAlignment.Center;
                btnProgressTextView.Text = "0%";
                btnProgressTextView.Gravity = Gravity.CenterHorizontal;
                frameProgressBack.AddChidren(btnProgressTextView);
            }
        }
 
        /// <summary>
        /// 重置进度条(只对模式1有效)
        /// </summary>
        public void ResetProgressBar()
        {
            if (this.m_ModeDiv == 1 && this.btnProgressBar != null)
            {
                this.btnProgressBar.Width = 0;
            }
        }
 
        /// <summary>
        /// 设置进度值
        /// </summary>
        /// <param name="value">此值为百分比值(也就是小于或者等于1的)</param>
        public void SetValue(decimal value)
        {
            this.SetValueEx(value);
        }
 
        /// <summary>
        /// 设置进度值
        /// </summary>
        /// <param name="value">进度值,内部会除以maxValue</param>
        /// <param name="maxValue">最大值</param>
        public void SetValue(decimal value, decimal maxValue)
        {
            decimal result = value / maxValue;
            this.SetValueEx(result);
        }
 
        /// <summary>
        /// 设置进度值
        /// </summary>
        /// <param name="value"></param>
        private void SetValueEx(decimal value)
        {
            if (btnProgressBar == null || this.m_ModeDiv != 1)
            {
                return;
            }
            if (value > 1) { value = 1; }
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                int width = (int)(value * this.Width);
                if (this.ProgressBarGoback == false && btnProgressBar.Width >= width)
                {
                    //不能让进度条往回走
                    return;
                }
                btnProgressBar.Width = width;
                if (this.btnProgressTextView != null)
                {
                    //文本显示
                    btnProgressTextView.Text = ((int)(value * 100)) + "%";
                    //文本显示的那个图片框移动
                    this.frameProgressBack.X = this.X + btnProgressBar.Right - frameProgressBack.Width / 2;
                }
            });
        }
 
        #endregion
 
        #region ■ 模式2______________________________
 
        /// <summary>
        /// 模式2  该模式为:不能手动指定进度值,由内部线程处理,进度条在持续无限的来回移动
        /// </summary>
        /// <param name="proWidth">持续无限的来回移动的进度条的宽度(非真实值)</param>
        public void StartMode2(int proWidth = 100)
        {
            if (m_ModeDiv != -1) { return; }
            this.m_ModeDiv = 2;
 
            //会移动的进度条
            this.btnProgressBar = new FrameLayout();
            btnProgressBar.Width = Application.GetRealWidth(proWidth);
            btnProgressBar.Height = this.Height;
            btnProgressBar.BackgroundColor = 0xfffb744a;
            btnProgressBar.Radius = (uint)this.Height / 2;
            this.AddChidren(btnProgressBar);
 
            //开启模式2的线程
            this.StartMode2Thread();
        }
 
        /// <summary>
        /// 重新开启模式2
        /// </summary>
        public void ReStartMode2()
        {
            //开启模式2的线程
            this.StartMode2Thread();
        }
 
        /// <summary>
        /// 暂停模式2
        /// </summary>
        public void StopMode2()
        {
            this.isThreadAction = false;
        }
 
        /// <summary>
        /// 开启模式2的线程
        /// </summary>
        private void StartMode2Thread()
        {
            if (this.isThreadAction == true)
            {
                return;
            }
            this.isThreadAction = true;
            int moveLength = Application.GetRealWidth(30);
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null && isThreadAction == true)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        if (this.btnProgressBar.X >= this.Width)
                        {
                            //超出右边之后,再次从左边循环
                            this.btnProgressBar.X = -this.btnProgressBar.Width;
                            return;
                        }
                        this.btnProgressBar.X += moveLength;
                    }, ShowErrorMode.NO);
                    System.Threading.Thread.Sleep(150);
                }
            });
        }
 
        #endregion
    }
}