wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using Android.Widget;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.Util;
using Android.Runtime;
 
namespace Shared
{
    //已经全面检查了代码
    /// <summary>
    /// 位置布局
    /// </summary>
    public class VerticalSeekBar : View
    {
        /// <summary>
        /// 当前视图
        /// </summary>
        /// <value>The android horizonetal seek bar.</value>
        VerticalSeekBarAndroidFrameLayout verticalSeekBarAndroidFrameLayout
        {
            get
            {
                return AndroidView as VerticalSeekBarAndroidFrameLayout;
            }
            set
            {
                AndroidView = value;
            }
        }
        Android.Widget.ImageView btnBackgroundColor;
        /// <summary>
        /// 显示进度控件
        /// </summary>
        Android.Widget.ImageView btnProgressColor;
        /// <summary>
        /// 滑块
        /// </summary>
        Android.Widget.ImageView btnThum;
 
 
        /// <summary>
        /// 当前实例
        /// </summary>
        public VerticalSeekBar()
        {
            verticalSeekBarAndroidFrameLayout = new VerticalSeekBarAndroidFrameLayout(Application.Activity, this);
            btnBackgroundColor = new Android.Widget.ImageView(Application.Activity);
            verticalSeekBarAndroidFrameLayout.AddView(btnBackgroundColor, new Android.Widget.FrameLayout.LayoutParams(DensityUtil.Dip2Px(1), 0, GravityFlags.CenterHorizontal));
            btnProgressColor = new Android.Widget.ImageView(Application.Activity);
            verticalSeekBarAndroidFrameLayout.AddView(btnProgressColor, new Android.Widget.FrameLayout.LayoutParams(DensityUtil.Dip2Px(1), 0, GravityFlags.CenterHorizontal));
            btnThum = new Android.Widget.ImageView(Application.Activity);
            verticalSeekBarAndroidFrameLayout.AddView(btnThum, new Android.Widget.FrameLayout.LayoutParams(DensityUtil.Dip2Px(24), DensityUtil.Dip2Px(24), GravityFlags.CenterHorizontal));
 
        }
 
        internal override bool TouchEvent(MotionEvent e)
        {
            switch (e.Action)
            {
                case MotionEventActions.Down:
                    dateTime = DateTime.Now;
                    setButtonLoaction((int)e.GetY());
                    break;
                case MotionEventActions.Move:
                    setButtonLoaction((int)e.GetY());
                    break;
                case MotionEventActions.Cancel:
                case MotionEventActions.Up:
                    dateTime = DateTime.MinValue;
                    setButtonLoaction((int)e.GetY());
                    break;
            }
            return base.TouchEvent(e);
        }
 
        /// <summary>
        /// 当前视图的高度
        /// </summary>
        /// <value>The height.</value>
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
 
                var frameBackgroundColor = btnBackgroundColor.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
                frameBackgroundColor.Height = base.Height;
                btnBackgroundColor.LayoutParameters = frameBackgroundColor;
            }
        }
        DateTime dateTime;
        /// <summary>
        ///  进度变化事件
        /// </summary>
        public Action<View, int> ProgressChanged;
 
        int max = 100;
        /// <summary>
        /// 最大值
        /// </summary>
        /// <value>The max.</value>
        public int Max
        {
            get
            {
                return max;
            }
            set
            {
                max = value;
            }
        }
        bool isCanScrolled = true;
        /// <summary>
        /// 是否允许滑动滑块
        /// </summary>
        /// <value><c>true</c> if is can scrolled; otherwise, <c>false</c>.</value>
        public bool IsCanScrolled
        {
            get
            {
                return isCanScrolled;
            }
            set
            {
                isCanScrolled = value;
            }
        }
 
        int validHeight
        {
            get
            {
                return AndroidView.Height - btnThum.Height;
            }
        }
 
        //2019-08-29 改100毫秒
        public int DelayTime = 100;
        //防止跳动,延时显示状态
        DateTime delayDateTime = DateTime.MinValue;
        int progress;
        /// <summary>
        /// 当前进度
        /// </summary>
        /// <value>The progress.</value>
        public int Progress
        {
            get
            {
                return (int)(Max * (validHeight - btnThum.Top) / validHeight);
            }
            set
            {
                progress = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                //发送命令后3000毫秒内不更新状态,防止跳动
                if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
                {
                    return;
                }
 
                if (Max < progress)
                {
                    progress = Max;
                }
                var frameThum = btnThum.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
                frameThum.TopMargin = (int)(1.0f * (Max - progress) / Max * validHeight);
                btnThum.LayoutParameters = frameThum;
 
                var frameProgress = btnProgressColor.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
                frameProgress.TopMargin = frameThum.TopMargin + frameThum.Height / 2;
                frameProgress.Height = AndroidView.LayoutParameters.Height - frameProgress.TopMargin;
                btnProgressColor.LayoutParameters = frameProgress;
            }
        }
 
 
 
 
        /// <summary>
        /// 刷新当前对象
        /// </summary>
        public override void Refresh()
        {
            base.Refresh();
            ProgressColor = progressColor;
            BackgroundColor = BackgroundColor;
            ThumbColor = thumbColor;
            Progress = progress;
        }
 
        /// <summary>
        /// 滑动休眠时间
        /// </summary>
        public int SleepTime = 0;
 
 
        uint progressColor = 0x99000099;
        /// <summary>
        /// 进度视图颜色
        /// </summary>
        /// <value>The color of the progress.</value>
        public uint ProgressColor
        {
            get
            {
                return progressColor;
            }
            set
            {
                progressColor = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                byte r, g, b, a;
                r = (byte)(progressColor / 256 / 256 % 256);
                g = (byte)(progressColor / 256 % 256);
                b = (byte)(progressColor % 256);
                a = (byte)(progressColor / 256 / 256 / 256 % 256);
 
                btnProgressColor.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        uint backgroundColor=0xFFFFFFFF;
        /// <summary>
        /// 进度视图颜色
        /// </summary>
        /// <value>The color of the progress.</value>
        public new uint BackgroundColor
        {
            get
            {
                return backgroundColor;
            }
            set
            {
                backgroundColor = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                byte r, g, b, a;
                r = (byte)(backgroundColor / 256 / 256 % 256);
                g = (byte)(backgroundColor / 256 % 256);
                b = (byte)(backgroundColor % 256);
                a = (byte)(backgroundColor / 256 / 256 / 256 % 256);
 
                btnBackgroundColor.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
 
        uint thumbColor = 0xFFFFFFFF;
        /// <summary>
        /// 滑块颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public uint ThumbColor
        {
            get
            {
                return thumbColor;
            }
            set
            {
                thumbColor = value;
                var gradientDrawable = new Android.Graphics.Drawables.GradientDrawable();
                gradientDrawable.SetCornerRadius(DensityUtil.Dip2Px(12));
                byte r, g, b, a;
                r = (byte)(thumbColor / 256 / 256 % 256);
                g = (byte)(thumbColor / 256 % 256);
                b = (byte)(thumbColor % 256);
                a = (byte)(thumbColor / 256 / 256 / 256 % 256);
                gradientDrawable.SetColor(Android.Graphics.Color.Argb(a, r, g, b).ToArgb());
                btnThum.Background = gradientDrawable;
            }
        }
        int beforValue = 0;
        //将图片移动到点击的位置
        void setButtonLoaction(int y)
        {
            if (!IsCanScrolled)
            {
                return;
            }
            delayDateTime = DateTime.Now;
            if (y < 0)
            {
                y = 0;
            }
 
            if (validHeight < y)
            {
                y = validHeight;
            }
 
            var frameThum = btnThum.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
            frameThum.TopMargin = y;
            btnThum.LayoutParameters = frameThum;
 
            var frameProgress = btnProgressColor.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
            frameProgress.TopMargin = frameThum.TopMargin + frameThum.Height / 2;
            frameProgress.Height = AndroidView.LayoutParameters.Height - frameProgress.TopMargin;
            btnProgressColor.LayoutParameters = frameProgress;
 
            if (dateTime == DateTime.MinValue)
            {
                if (ProgressChanged != null && beforValue != Progress)
                {
                    dateTime = DateTime.Now;
                    delayDateTime = DateTime.Now;
                    ProgressChanged(this, beforValue = Progress);
                }
            }
            else
            {
                //变化大于500毫秒的时候才通知
                if (SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
                {
                    dateTime = DateTime.Now;
                    delayDateTime = DateTime.Now;
                    if (ProgressChanged != null && beforValue != Progress)
                    {
                        ProgressChanged(this, beforValue = Progress);
                    }
                }
            }
        }
 
        /// <summary>
        /// 需要重写的视图
        /// </summary>
        class VerticalSeekBarAndroidFrameLayout : Android.Widget.FrameLayout
        {
            View _view;
            public VerticalSeekBarAndroidFrameLayout(Context context, View view)
                : base(context)
            {
                _view = view;
                Touch += (sender, e) =>
                {
                    if (_view != null)
                    {
                        _view.TouchEvent(e.Event);
                    }
                };
            }
 
            /// <summary>
            /// 所有事件本视图全部消化,不通知给子视图
            /// </summary>
            /// <returns><c>true</c>事件已经处理<c>false</c>需要父视图处理</returns>
            /// <param name="ev">Ev.</param>
            public override bool OnInterceptTouchEvent(MotionEvent ev)
            {
                //不需要子视图处理事件,所有的事件自己处理
                return true;
            }
 
            float startPointX;
            /// <summary>
            /// 执行OnInterceptTouchEvent和OnTouchEvent
            /// </summary>
            /// <returns><c>true</c>事件已经处理<c>false</c>事件没有处理</returns>
            /// <param name="e">E.</param>
            public override bool DispatchTouchEvent(MotionEvent e)
            {
                switch (e.Action)
                {
                    case MotionEventActions.Down:
                        startPointX = e.RawX;
                        requestParentDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEventActions.Move:
                        if (20 < Math.Abs(e.RawX - startPointX))
                        {
                            requestParentDisallowInterceptTouchEvent(false);
                        }
                        break;
                    case MotionEventActions.Up:
                    case MotionEventActions.Cancel:
                        requestParentDisallowInterceptTouchEvent(false);
                        break;
                }
                return base.DispatchTouchEvent(e);
            }
            /// <summary>
            /// 设置所有父视图禁止拦截事件
            /// </summary>
            /// <param name="b">如果设置True表示不要拦截,如果设置为False表示可以拦截</param>
            void requestParentDisallowInterceptTouchEvent(bool b)
            {
                var tempParent = Parent;
                while (tempParent != null)
                {   //告诉父类不要拦截这个视图的事件
                    tempParent.RequestDisallowInterceptTouchEvent(b);
                    tempParent = tempParent.Parent;
                }
            }
        }
    }
}