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
using System;
using Android.Views;
using Android.Content;
using Android.Util;
using Android.Runtime;
 
namespace Shared
{
    //已经全面检查了代码
    /// <summary>
    /// 位置布局
    /// </summary>
    public class HorizontalSeekBar : View
    {
        Android.Widget.ImageView backImageView;
        Android.Widget.ImageView progressImageView;
        Android.Widget.Button thumbButton;
        AndroidHorizontalSeekBar androidHorizonetalSeekBar;
        /// <summary>
        /// 当前实例
        /// </summary>
        public HorizontalSeekBar()
        {
            AndroidView = new AndroidFrameLayout(Application.Activity, null) { };
            //AndroidView.SetBackgroundColor (Android.Graphics.Color.DarkCyan);
            {
                backImageView = new Android.Widget.ImageView(Application.Activity, null) { Enabled=false };
                backImageView.SetBackgroundColor(Android.Graphics.Color.WhiteSmoke);
                (AndroidView as AndroidFrameLayout).AddView(backImageView, new Android.Widget.FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Dip2Px(2), GravityFlags.CenterVertical));
            }
 
            progressImageView = new Android.Widget.ImageView(Application.Activity, null) {  Enabled=false};
            progressImageView.SetBackgroundColor(Android.Graphics.Color.Rgb(0xFE, 0x5E, 00));
            (AndroidView as AndroidFrameLayout).AddView(progressImageView, new Android.Widget.FrameLayout.LayoutParams(0, Dip2Px(2), GravityFlags.CenterVertical));
 
            thumbButton = new Android.Widget.Button(Application.Activity, null) { Background = new Android.Graphics.Drawables.GradientDrawable(),Enabled=false };
            (AndroidView as AndroidFrameLayout).AddView(thumbButton, new Android.Widget.FrameLayout.LayoutParams(Dip2Px(ThumbRadius), Dip2Px(ThumbRadius), GravityFlags.CenterVertical));
 
            androidHorizonetalSeekBar = new AndroidHorizontalSeekBar(Application.Activity, this);
            (AndroidView as AndroidFrameLayout).AddView(androidHorizonetalSeekBar, new Android.Widget.FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.MatchParent));
 
        }
 
        uint progressColor = 0xFFEB642C;
        /// <summary>
        /// 进度颜色
        /// </summary>
        /// <value>The color of the progress.</value>
        public uint ProgressColor
        {
            get
            {
                return progressColor;
            }
            set
            {
                progressColor = value;
                int r = (byte)(progressColor / 256 / 256 % 256);
                int g = (byte)(progressColor / 256 % 256);
                int b = (byte)(progressColor % 256);
                int a = (byte)(progressColor / 256 / 256 / 256 % 256);
                progressImageView.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        public int SleepTime = 0;
 
 
        uint backgroundColor = (uint)Android.Graphics.Color.WhiteSmoke.ToArgb();
        /// <summary>
        /// 底部颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public new uint BackgroundColor
        {
            get
            {
                return backgroundColor;
            }
            set
            {
                backgroundColor = value;
                int r = (byte)(backgroundColor / 256 / 256 % 256);
                int g = (byte)(backgroundColor / 256 % 256);
                int b = (byte)(backgroundColor % 256);
                int a = (byte)(backgroundColor / 256 / 256 / 256 % 256);
                backImageView.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        /// <summary>
        ///  进度变化事件
        /// </summary>
        public Action<View, int> ProgressChanged;
 
        /// <summary>
        /// 最大值
        /// </summary>
        /// <value>The max.</value>
        public int Max
        {
            get
            {
                return androidHorizonetalSeekBar.Max;
            }
            set
            {
                androidHorizonetalSeekBar.Max = value;
            }
        }
        /// <summary>
        /// 是否允许滑动滑块
        /// </summary>
        /// <value><c>true</c> if is can scrolled; otherwise, <c>false</c>.</value>
        public bool IsCanScrolled
        {
            get
            {
                return androidHorizonetalSeekBar.Enabled;
            }
            set
            {
                thumbButton.Visibility = value ? ViewStates.Visible : ViewStates.Gone;
                androidHorizonetalSeekBar.Enabled = value;
            }
        }
 
        //2019-08-29 改100毫秒
        public int DelayTime = 100;
        //防止跳动,延时显示状态
        public DateTime delayDateTime = DateTime.MinValue;
        /// <summary>
        /// 当前进度
        /// </summary>
        /// <value>The progress.</value>
        public int Progress
        {
            get
            {
                return androidHorizonetalSeekBar.Progress;
            }
            set
            {
                //发送命令后1000毫秒内不更新状态,防止跳动
                if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
                {
                    return;
                }
                androidHorizonetalSeekBar.Progress = value;
                if (!IsCanRefresh)
                {
                    return;
                }
 
                int width = (int)(androidHorizonetalSeekBar.Progress * 1.0 / androidHorizonetalSeekBar.Max * (Width - thumbButton.LayoutParameters.Width));
                progressImageView.LayoutParameters.Width = width + thumbButton.LayoutParameters.Width / 2;
                var layoutParameter = (thumbButton.LayoutParameters as Android.Widget.FrameLayout.LayoutParams);
                layoutParameter.LeftMargin = width;
                thumbButton.LayoutParameters = layoutParameter;
            }
        }
 
        uint thumbColor = 0xFFFFFFFF;
        /// <summary>
        /// 滑块颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public uint ThumbColor
        {
            get
            {
                return thumbColor;
            }
            set
            {
                thumbColor = value;
                int r = (byte)(thumbColor / 256 / 256 % 256);
                int g = (byte)(thumbColor / 256 % 256);
                int b = (byte)(thumbColor % 256);
                int a = (byte)(thumbColor / 256 / 256 / 256 % 256);
 
                ((Android.Graphics.Drawables.GradientDrawable)thumbButton.Background).SetColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        public DateTime dateTime = DateTime.MinValue;
        internal override bool TouchEvent(MotionEvent e)
        {
            if (!IsCanMove||!Enable)
            {
                return true;
            }
            delayDateTime = DateTime.Now;
            switch (e.Action)
            {
                case MotionEventActions.Down:
                    dateTime = DateTime.MinValue;
                    break;
                case MotionEventActions.Move:
                    if (dateTime == DateTime.MinValue)
                    {
                        dateTime = DateTime.Now;
                    }
                    setValue(e.GetX());
                    break;
                case MotionEventActions.Up:
                    dateTime = DateTime.MinValue;
                    setValue(e.GetX());
 
                    break;
            }
            return base.TouchEvent(e);
        }
 
        int beforValue;
        /// <summary>
        /// 是否可以滑动
        /// </summary>
        public bool IsCanMove = true;
        void setValue(float x)
        {
 
            if (x < 0)
            {
                x = 0;
            }
            if (Width - thumbButton.LayoutParameters.Width < x)
            {
                x = Width - thumbButton.LayoutParameters.Width;
            }
            var tempValue = androidHorizonetalSeekBar.Progress;
 
            androidHorizonetalSeekBar.Progress = (int)(x / (Width - thumbButton.LayoutParameters.Width) * Max);
 
 
            int width = (int)(androidHorizonetalSeekBar.Progress * 1.0 / androidHorizonetalSeekBar.Max * (Width - thumbButton.LayoutParameters.Width));
            progressImageView.LayoutParameters.Width = width + thumbButton.LayoutParameters.Width / 2;
            var layoutParameter = thumbButton.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
            layoutParameter.LeftMargin = width;
            thumbButton.LayoutParameters = layoutParameter;
 
            if (tempValue != androidHorizonetalSeekBar.Progress || tempValue == 0 || tempValue == Max|| dateTime== DateTime.MinValue)
            {
                //点击下或者弹起或者滑动时间500毫秒的时候,就触发事件
                if (ProgressChanged != null
                    && SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
                {
                    dateTime = DateTime.MinValue;
                    delayDateTime = DateTime.Now;
                    ProgressChanged(this, androidHorizonetalSeekBar.Progress);
                }
            }
        }
 
        int thumbRadius = 10;
        public int ThumbRadius
        {
            get
            {
                return thumbRadius;
            }
            set
            {
                thumbRadius = value;
                if (!IsCanRefresh)
                {
                    return;
                }
 
                thumbButton.LayoutParameters.Width = Dip2Px(ThumbRadius) * 2;
                thumbButton.LayoutParameters.Height = Dip2Px(ThumbRadius) * 2;
                var gradientDrawable = thumbButton.Background as Android.Graphics.Drawables.GradientDrawable;
                gradientDrawable.SetCornerRadius(Dip2Px(ThumbRadius));
                gradientDrawable.SetSize(Dip2Px(ThumbRadius) * 2, Dip2Px(ThumbRadius) * 2);
                thumbButton.Background = gradientDrawable;
                ThumbColor = thumbColor;
            }
        }
 
        public override void Refresh()
        {
            base.Refresh();
            ThumbRadius = thumbRadius;
            ProgressColor = progressColor;
            Progress = androidHorizonetalSeekBar.Progress;
        }
 
        /// <summary>
        /// 需要重写的视图
        /// </summary>
        class AndroidHorizontalSeekBar : Android.Widget.SeekBar
        {
            View view;
            public AndroidHorizontalSeekBar(Context context, View view)
                : base(context)
            {
                this.view = view;
                ProgressDrawable = null;
                SetThumb(null);
            }
            /// <summary>
            /// 如果已经是横向滑动,父控件不允许再请求中断
            /// </summary>
            bool isHorizontalActioned;
            /// <summary>
            /// 记录最后点击的X轴坐标
            /// </summary>
            float mLastMotionX;
            /// <summary>
            /// 点击Y轴的坐标
            /// </summary>
            float mLastMotionY;
            public override bool OnTouchEvent(MotionEvent e)
            {
                switch (e.Action)
                {
                    case MotionEventActions.Down:
                        isHorizontalActioned = false;
                        //进来的时候禁止父控制打断
                        Parent?.RequestDisallowInterceptTouchEvent(true);
                        //记录点击的X坐标
                        mLastMotionX = e.RawX;
                        mLastMotionY = e.RawY;
                        break;
                    case MotionEventActions.Move:
                        var deltaX = mLastMotionX - e.RawX;
                        var deltaY = mLastMotionY - e.RawY;
                        //如果主要滑动的是Y轴比较大,返回
                        if (Math.Abs(deltaX) < Math.Abs(deltaY))
                        {
                            if (ViewConfiguration.Get(Context).ScaledTouchSlop < Math.Abs(deltaY) && !isHorizontalActioned)
                            {
                                Parent?.RequestDisallowInterceptTouchEvent(false);
                            }
                            return true;
                        }
                        //如果不在滑动范围内,不执行滑动
                        if (Math.Abs(deltaX) < ViewConfiguration.Get(Context).ScaledTouchSlop)
                        {
                            return true;
                        }
                        isHorizontalActioned = true;
                        view?.TouchEvent(e);
                        break;
                    case MotionEventActions.Up:
                        view?.TouchEvent(e);
                        break;
                }
 
                return true;
            }
        }
    }
}