lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
 * Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
package com.zhaoyang.driver.utils.citypickerview.picker;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
import androidx.annotation.NonNull;
 
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.ThemeUtils;
 
import java.util.HashSet;
import java.util.Set;
 
/**
 * 双向滑块选择器
 *
 * @author xuexiang
 * @since 2020-01-04 18:34
 */
public class XRangeSlider extends View {
 
    public interface OnRangeSliderListener {
        /**
         * max value changed callback
         *
         * @param maxValue 最大值
         */
        void onMaxChanged(XRangeSlider slider, int maxValue);
 
        /**
         * min value changed callback
         *
         * @param minValue 最小值
         */
        void onMinChanged(XRangeSlider slider, int minValue);
    }
 
    private static int DEFAULT_TOUCH_TARGET_SIZE;
    private static int DEFAULT_TEXT_MIN_SPACE;
    private static final int DEFAULT_MAX = 100;
    /**
     * 刻度的宽度参数
     */
    private static final float DEFAULT_BIG_SCALE_WITH = 1.7f;
    private static final float DEFAULT_MIDDLE_SCALE_WITH = 1.2f;
    private static final float DEFAULT_SMALL_SCALE_WITH = 1.0f;
 
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int mLineStartX;
    private int mLineEndX;
    private int mLineLength;
    private int mMinPosition = 0;
    private int mMaxPosition = 0;
    private int mRange;
    private float mConvertFactor;
    private int mMiddleY = 0;
    private boolean mLastTouchedMin;
    private boolean mIsTouchingMin;
    private int mStartingMin = -1;
    private int mStartingMax = -1;
    private boolean mIsFirstInit = true;
    private boolean mIsTouching = false;
    private Rect mMinTextRect = new Rect();
    private Rect mMaxTextRect = new Rect();
    private Rect mRulerTextRect = new Rect();
    /**
     * List of event IDs touching targets
     */
    private Set<Integer> mTouchingMinTarget = new HashSet<>();
    private Set<Integer> mTouchingMaxTarget = new HashSet<>();
    private OnRangeSliderListener mOnRangeSliderListener;
    //========属性==========//
    private int mVerticalPadding;
    private int mInsideRangeColor;
    private int mOutsideRangeColor;
    private float mInsideRangeLineStrokeWidth;
    private float mOutsideRangeLineStrokeWidth;
    private int mMin = 0;
    private int mMax = DEFAULT_MAX;
    private Bitmap mSliderIcon;
    private Bitmap mSliderIconFocus;
    private boolean mIsLineRound;
 
    private boolean mIsShowBubble;
    private Bitmap mBubbleBitmap;
    private int mNumberTextColor;
    private float mNumberTextSize;
    private float mNumberMarginBottom;
 
    private boolean mIsShowRuler;
    private int mRulerColor;
    private int mRulerTextColor;
    private float mRulerTextSize;
    private float mRulerMarginTop;
    private float mRulerDividerHeight;
    private float mRuleTextMarginTop;
    private int mRulerInterval;
 
    public XRangeSlider(Context context) {
        this(context, null);
    }
 
    public XRangeSlider(Context context, AttributeSet attrs) {
        this(context, attrs, com.xuexiang.xui.R.attr.XRangeSliderStyle);
    }
 
    public XRangeSlider(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }
 
    public void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        DEFAULT_TOUCH_TARGET_SIZE = DensityUtils.dp2px(20);
        DEFAULT_TEXT_MIN_SPACE = DensityUtils.dp2px(2);
        int colorAccent = ThemeUtils.resolveColor(context, com.xuexiang.xui.R.attr.colorAccent);
        int colorControlNormal = ThemeUtils.resolveColor(context, com.xuexiang.xui.R.attr.colorControlNormal);
 
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, com.xuexiang.xui.R.styleable.XRangeSlider, defStyleAttr, 0);
            mVerticalPadding = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_verticalPadding, DensityUtils.dp2px(10));
            //滑条
            mInsideRangeColor = array.getColor(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_insideRangeLineColor, colorAccent);
            mOutsideRangeColor = array.getColor(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_outsideRangeLineColor, ResUtils.getColor(com.xuexiang.xui.R.color.default_xrs_outside_line_color));
            mInsideRangeLineStrokeWidth = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_insideRangeLineStrokeWidth, DensityUtils.dp2px(5));
            mOutsideRangeLineStrokeWidth = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_outsideRangeLineStrokeWidth, DensityUtils.dp2px(5));
            mMin = array.getInt(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_min, mMin);
            mMax = array.getInt(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_max, mMax);
            mSliderIcon = BitmapFactory.decodeResource(getResources(), array.getResourceId(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_sliderIcon, com.xuexiang.xui.R.drawable.xui_ic_slider_icon));
            mSliderIconFocus = BitmapFactory.decodeResource(getResources(), array.getResourceId(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_sliderIconFocus, com.xuexiang.xui.R.drawable.xui_ic_slider_icon));
            mIsLineRound = array.getBoolean(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_isLineRound, true);
 
            //气泡
            mIsShowBubble = array.getBoolean(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_isShowBubble, false);
            boolean isFitColor = array.getBoolean(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_isFitColor, true);
            mNumberTextColor = array.getColor(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_numberTextColor, colorAccent);
            mNumberTextSize = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_numberTextSize, DensityUtils.sp2px(12));
            mNumberMarginBottom = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_numberMarginBottom, DensityUtils.dp2px(2));
            if (isFitColor) {
                if (mIsShowBubble) {
                    mNumberTextColor = Color.WHITE;
                }
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), array.getResourceId(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_bubbleResource, com.xuexiang.xui.R.drawable.xui_bg_bubble_blue));
                if (bitmap != null) {
                    mBubbleBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                    Canvas canvas = new Canvas(mBubbleBitmap);
                    canvas.drawColor(mInsideRangeColor, PorterDuff.Mode.SRC_IN);
                }
            } else {
                mBubbleBitmap = BitmapFactory.decodeResource(getResources(), array.getResourceId(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_bubbleResource, com.xuexiang.xui.R.drawable.xui_bg_bubble_blue));
            }
 
            //刻度尺
            mIsShowRuler = array.getBoolean(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_isShowRuler, false);
            mRulerColor = array.getColor(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerColor, colorControlNormal);
            mRulerTextColor = array.getColor(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerTextColor, colorControlNormal);
            mRulerTextSize = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerTextSize, DensityUtils.sp2px(12));
            mRulerMarginTop = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerMarginTop, DensityUtils.dp2px(4));
            mRulerDividerHeight = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerDividerHeight, DensityUtils.dp2px(4));
            mRuleTextMarginTop = array.getDimensionPixelSize(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerTextMarginTop, DensityUtils.dp2px(4));
            mRulerInterval = array.getInt(com.xuexiang.xui.R.styleable.XRangeSlider_xrs_rulerInterval, 20);
            array.recycle();
        }
        mRange = mMax - mMin;
    }
 
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int desiredWidth = widthSize;
        int desiredHeight;
 
        getTextBounds(String.valueOf(mMin), mMinTextRect);
        getTextBounds(String.valueOf(mMax), mMaxTextRect);
 
        if (mIsShowBubble) {
            desiredHeight = (int) (mSliderIcon.getHeight() + mNumberMarginBottom) + mBubbleBitmap.getHeight();
        } else {
            desiredHeight = (int) (mSliderIcon.getHeight() + mNumberMarginBottom + mMinTextRect.height());
        }
 
        int rulerHeight = (int) (mRulerMarginTop + mRulerDividerHeight * 3 + mRuleTextMarginTop + mRulerTextRect.height());
        if (mIsShowRuler) {
            getRulerTextBounds(String.valueOf(mMin), mRulerTextRect);
            desiredHeight += rulerHeight;
        }
 
        int width;
        int height = desiredHeight;
 
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(desiredWidth, widthSize);
        } else {
            width = desiredWidth;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = desiredHeight;
        }
 
        height += mVerticalPadding;
 
        int marginStartEnd = mIsShowBubble ? mBubbleBitmap.getWidth() : Math.max(mSliderIcon.getWidth(), mMaxTextRect.width());
 
        mLineLength = (width - marginStartEnd);
        mMiddleY = mIsShowRuler ? height - rulerHeight - mSliderIcon.getHeight() / 2 : height - mSliderIcon.getHeight() / 2;
        mLineStartX = marginStartEnd / 2;
        mLineEndX = mLineLength + marginStartEnd / 2;
 
        calculateConvertFactor();
 
        if (mIsFirstInit) {
            setSelectedMin(mStartingMin != -1 ? mStartingMin : mMin);
            setSelectedMax(mStartingMax != -1 ? mStartingMax : mMax);
        }
 
        height += mVerticalPadding;
 
        setMeasuredDimension(width, height);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        drawEntireRangeLine(canvas);
        drawSelectedRangeLine(canvas);
        drawSelectedNumber(canvas);
        drawRuler(canvas);
        drawSelectedTargets(canvas);
    }
 
    private void drawEntireRangeLine(Canvas canvas) {
        mPaint.setColor(mOutsideRangeColor);
        mPaint.setStrokeWidth(mOutsideRangeLineStrokeWidth);
        canvas.drawLine(mLineStartX, mMiddleY, mLineEndX, mMiddleY, mPaint);
 
        if (mIsLineRound) {
            canvas.drawCircle(mLineStartX, mMiddleY, mOutsideRangeLineStrokeWidth / 2, mPaint);
            canvas.drawCircle(mLineEndX, mMiddleY, mOutsideRangeLineStrokeWidth / 2, mPaint);
        }
    }
 
    private void drawSelectedRangeLine(Canvas canvas) {
        mPaint.setStrokeWidth(mInsideRangeLineStrokeWidth);
        mPaint.setColor(mInsideRangeColor);
        canvas.drawLine(mMinPosition, mMiddleY, mMaxPosition, mMiddleY, mPaint);
    }
 
    private void drawSelectedNumber(Canvas canvas) {
 
        String min = String.valueOf(getSelectedMin());
        String max = String.valueOf(getSelectedMax());
 
        getTextBounds(min, mMinTextRect);
        getTextBounds(max, mMaxTextRect);
 
 
        float minXBubble = mMinPosition - mBubbleBitmap.getWidth() / 2;
        float maxXBubble = mMaxPosition - mBubbleBitmap.getWidth() / 2;
        int maxTextLengthBubble = mBubbleBitmap.getWidth() + 5;
        if (mIsTouching && mTouchingMaxTarget.size() > 0) {
            if (Math.abs(maxXBubble - minXBubble) <= maxTextLengthBubble) {
                maxXBubble = minXBubble + maxTextLengthBubble;
 
                if (maxXBubble > mLineEndX - maxTextLengthBubble / 2) {
                    maxXBubble = mLineEndX - maxTextLengthBubble / 2;
                }
            }
        }
 
        if (mIsTouching && mTouchingMinTarget.size() > 0) {
            if (Math.abs(maxXBubble - minXBubble) <= maxTextLengthBubble) {
                minXBubble = maxXBubble - maxTextLengthBubble;
 
                if (minXBubble < mLineStartX) {
                    minXBubble = mLineStartX;
                }
            }
        }
 
        if (Math.abs(maxXBubble - minXBubble) <= maxTextLengthBubble) {
 
            if (!mIsTouchingMin) {
                maxXBubble = minXBubble + maxTextLengthBubble;
                if (maxXBubble > mLineEndX - maxTextLengthBubble / 2) {
                    maxXBubble = mLineEndX - maxTextLengthBubble / 2;
                    minXBubble = maxXBubble - maxTextLengthBubble;
                }
            } else {
                minXBubble = maxXBubble - maxTextLengthBubble;
                if (minXBubble < mLineStartX + maxTextLengthBubble / 2) {
                    minXBubble = mLineStartX + maxTextLengthBubble / 2;
                    maxXBubble = minXBubble + maxTextLengthBubble;
                }
            }
        }
 
 
        float yText;
        //bubble
        if (mIsShowBubble) {
 
            float top = mMiddleY - mSliderIcon.getHeight() / 2 - mBubbleBitmap.getHeight() - mNumberMarginBottom;
            yText = top + mBubbleBitmap.getHeight() / 2 + mMinTextRect.height() / 2 - 6;
 
            canvas.drawBitmap(mBubbleBitmap, maxXBubble, top, mPaint);
            canvas.drawBitmap(mBubbleBitmap, minXBubble, top, mPaint);
 
        } else {
            yText = mMiddleY - mSliderIcon.getHeight() / 2 - mNumberMarginBottom;
        }
 
        //text
 
        float minX = mMinPosition - mMinTextRect.width() / 2;
        float maxX = mMaxPosition - mMaxTextRect.width() / 2 - 6;
        int maxTextLength = mIsShowBubble ? mBubbleBitmap.getWidth() : getMaxTextLength() + 5;
 
        if (mIsTouching && mTouchingMaxTarget.size() > 0) {
            if (Math.abs(maxX - minX) <= maxTextLength) {
                maxX = minX + maxTextLength;
 
                if (maxX > mLineEndX - maxTextLength / 2) {
                    maxX = mLineEndX - maxTextLength / 2;
                }
            }
        }
 
        if (mIsTouching && mTouchingMinTarget.size() > 0) {
            if (Math.abs(maxX - minX) <= maxTextLength) {
                minX = maxX - maxTextLength;
 
                if (minX < mLineStartX) {
                    minX = mLineStartX;
                }
            }
        }
 
        if (Math.abs(maxX - minX) <= maxTextLength) {
            if (!mIsTouchingMin) {
                maxX = minX + maxTextLength;
                if (maxX > mLineEndX - maxTextLength / 2) {
                    maxX = mLineEndX - maxTextLength / 2;
                    minX = maxX - maxTextLength;
                }
            } else {
                minX = maxX - maxTextLength;
                if (minX < mLineStartX + maxTextLength / 2) {
                    minX = mLineStartX + maxTextLength / 2;
                    maxX = minX + maxTextLength;
                }
            }
        }
 
 
        mPaint.setTextSize(mNumberTextSize);
 
        mPaint.setColor(mNumberTextColor);
        canvas.drawText(min, minX, yText, mPaint);
 
        mPaint.setColor(mNumberTextColor);
        canvas.drawText(max, maxX, yText, mPaint);
 
 
    }
 
    private void drawRuler(Canvas canvas) {
        if (mIsShowRuler) {
            float startX = mLineStartX;
            float stopY;
            float startY;
            int divider = mRulerInterval / 10;
            float scaleLength = (float) mLineLength / (float) ((mMax - mMin) / (mRulerInterval / 10)) / divider;
 
            boolean isMinHasText = false;
            boolean isMaxHasText = false;
 
            for (int i = mMin; i <= mMax; i++) {
                if (i % mRulerInterval == 0) {
                    //draw big scale
                    startY = mMiddleY + mSliderIcon.getHeight() / 2 + mRulerMarginTop;
                    stopY = startY + mRulerDividerHeight * 3;
 
 
                    mPaint.setColor(mRulerTextColor);
                    mPaint.setTextSize(mRulerTextSize);
                    getRulerTextBounds(String.valueOf(i), mRulerTextRect);
                    canvas.drawText(String.valueOf(i), startX - mRulerTextRect.width() / 2, stopY + mRulerTextRect.height() + mRuleTextMarginTop, mPaint);
 
                    if (i == mMin) {
                        isMinHasText = true;
                    }
                    if (i == mMax) {
                        isMaxHasText = true;
                    }
                    mPaint.setStrokeWidth(DEFAULT_BIG_SCALE_WITH);
 
 
                    mPaint.setColor(mRulerColor);
                    canvas.drawLine(startX, startY, startX, stopY, mPaint);
 
                } else if (i % (mRulerInterval / 2) == 0) {
                    //draw middle scale
                    startY = mMiddleY + mSliderIcon.getHeight() / 2 + mRulerMarginTop;
                    stopY = startY + mRulerDividerHeight * 2;
                    mPaint.setStrokeWidth(DEFAULT_MIDDLE_SCALE_WITH);
 
                    mPaint.setColor(mRulerColor);
                    canvas.drawLine(startX, startY, startX, stopY, mPaint);
 
 
                } else {
                    //draw small scale
                    startY = mMiddleY + mSliderIcon.getHeight() / 2 + mRulerMarginTop;
                    stopY = startY + mRulerDividerHeight;
                    mPaint.setStrokeWidth(DEFAULT_SMALL_SCALE_WITH);
 
                    if (i % (mRulerInterval / 10) == 0) {
                        mPaint.setColor(mRulerColor);
                        canvas.drawLine(startX, startY, startX, stopY, mPaint);
                    }
 
                }
 
                if ((i == mMax && !isMaxHasText) || (i == mMin && !isMinHasText)) {
                    mPaint.setColor(mRulerTextColor);
                    mPaint.setTextSize(mRulerTextSize);
                    getRulerTextBounds(String.valueOf(i), mRulerTextRect);
 
                    float x = startX - mRulerTextRect.width() / 2;
                    //修正最大值与最小值文本与满刻度文本太靠近时显示重叠问题
                    if (i == mMax && i % mRulerInterval == 1) {
                        x = startX + DEFAULT_TEXT_MIN_SPACE;
                    }
 
                    if (i == mMin && i % mRulerInterval == mRulerInterval - 1) {
                        x = startX - mRulerTextRect.width() / 2 - DEFAULT_TEXT_MIN_SPACE;
                    }
 
                    canvas.drawText(String.valueOf(i), x, startY + mRulerDividerHeight * 3 + mRulerTextRect.height() + mRuleTextMarginTop, mPaint);
 
                }
 
                startX += scaleLength;
 
 
            }
        }
 
    }
 
    private void drawSelectedTargets(Canvas canvas) {
        mPaint.setColor(mInsideRangeColor);
        canvas.drawCircle(mMinPosition, mMiddleY, DensityUtils.dp2px(3), mPaint);
        canvas.drawCircle(mMaxPosition, mMiddleY, DensityUtils.dp2px(3), mPaint);
 
        if (mIsTouching) {
            if (mLastTouchedMin) {
                canvas.drawBitmap(mSliderIconFocus, mMinPosition - mSliderIconFocus.getWidth() / 2, mMiddleY - mSliderIconFocus.getWidth() / 2, mPaint);
                canvas.drawBitmap(mSliderIcon, mMaxPosition - mSliderIcon.getWidth() / 2, mMiddleY - mSliderIcon.getWidth() / 2, mPaint);
            } else {
                canvas.drawBitmap(mSliderIcon, mMinPosition - mSliderIcon.getWidth() / 2, mMiddleY - mSliderIcon.getWidth() / 2, mPaint);
                canvas.drawBitmap(mSliderIconFocus, mMaxPosition - mSliderIconFocus.getWidth() / 2, mMiddleY - mSliderIconFocus.getWidth() / 2, mPaint);
            }
        } else {
            canvas.drawBitmap(mSliderIcon, mMaxPosition - mSliderIcon.getWidth() / 2, mMiddleY - mSliderIcon.getWidth() / 2, mPaint);
            canvas.drawBitmap(mSliderIcon, mMinPosition - mSliderIcon.getWidth() / 2, mMiddleY - mSliderIcon.getWidth() / 2, mPaint);
        }
    }
 
    private void getTextBounds(String text, Rect rect) {
        mPaint.setTextSize(mNumberTextSize);
        mPaint.getTextBounds(text, 0, text.length(), rect);
    }
 
    private void getRulerTextBounds(String text, Rect rect) {
        mPaint.setTextSize(mRulerTextSize);
        mPaint.getTextBounds(text, 0, text.length(), rect);
    }
 
    private void jumpToPosition(int index, MotionEvent event) {
        //user has touched outside the target, lets jump to that position
        if (event.getX(index) > mMaxPosition && event.getX(index) <= mLineEndX) {
            mMaxPosition = (int) event.getX(index);
            invalidate();
            callMaxChangedCallbacks();
        } else if (event.getX(index) < mMinPosition && event.getX(index) >= mLineStartX) {
            mMinPosition = (int) event.getX(index);
            invalidate();
            callMinChangedCallbacks();
        }
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
 
        mIsFirstInit = false;
 
        final int actionIndex = event.getActionIndex();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
 
                mIsTouching = true;
 
                if (mLastTouchedMin) {
                    if (!checkTouchingMinTarget(actionIndex, event)
                            && !checkTouchingMaxTarget(actionIndex, event)) {
                        jumpToPosition(actionIndex, event);
                    }
                } else if (!checkTouchingMaxTarget(actionIndex, event)
                        && !checkTouchingMinTarget(actionIndex, event)) {
                    jumpToPosition(actionIndex, event);
                }
                break;
 
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
 
                mIsTouching = false;
 
                mTouchingMinTarget.remove(event.getPointerId(actionIndex));
                mTouchingMaxTarget.remove(event.getPointerId(actionIndex));
 
                invalidate();
                break;
 
            case MotionEvent.ACTION_MOVE:
 
                mIsTouching = true;
 
                for (int i = 0; i < event.getPointerCount(); i++) {
                    if (mTouchingMinTarget.contains(event.getPointerId(i))) {
                        int touchX = (int) event.getX(i);
                        touchX = clamp(touchX, mLineStartX, mLineEndX);
                        if (touchX >= mMaxPosition) {
                            mMaxPosition = touchX;
                            callMaxChangedCallbacks();
                        }
                        mMinPosition = touchX;
                        callMinChangedCallbacks();
                        mIsTouchingMin = true;
                    }
                    if (mTouchingMaxTarget.contains(event.getPointerId(i))) {
                        int touchX = (int) event.getX(i);
                        touchX = clamp(touchX, mLineStartX, mLineEndX);
                        if (touchX <= mMinPosition) {
                            mMinPosition = touchX;
                            callMinChangedCallbacks();
                        }
                        mMaxPosition = touchX;
                        callMaxChangedCallbacks();
                        mIsTouchingMin = false;
                    }
                }
                invalidate();
                break;
 
            case MotionEvent.ACTION_POINTER_DOWN:
 
                mIsTouching = true;
 
                for (int i = 0; i < event.getPointerCount(); i++) {
                    if (mLastTouchedMin) {
                        if (!checkTouchingMinTarget(i, event)
                                && !checkTouchingMaxTarget(i, event)) {
                            jumpToPosition(i, event);
                        }
                    } else if (!checkTouchingMaxTarget(i, event)
                            && !checkTouchingMinTarget(i, event)) {
                        jumpToPosition(i, event);
                    }
                }
 
                break;
 
            case MotionEvent.ACTION_CANCEL:
 
                mIsTouching = false;
 
                mTouchingMinTarget.clear();
                mTouchingMaxTarget.clear();
 
                invalidate();
                break;
 
            default:
                break;
        }
 
        return true;
    }
 
    private int getMinTextLength() {
        getTextBounds(String.valueOf(mMin), mMinTextRect);
        return mMinTextRect.width();
    }
 
    private int getMaxTextLength() {
        getTextBounds(String.valueOf(mMax), mMaxTextRect);
        return mMaxTextRect.width();
    }
 
    /**
     * Checks if given index is touching the min target.  If touching start animation.
     */
    private boolean checkTouchingMinTarget(int index, MotionEvent event) {
        if (isTouchingMinTarget(index, event)) {
            mLastTouchedMin = true;
            mTouchingMinTarget.add(event.getPointerId(index));
            return true;
        }
        return false;
    }
 
    /**
     * Checks if given index is touching the max target.  If touching starts animation.
     */
    private boolean checkTouchingMaxTarget(int index, MotionEvent event) {
        if (isTouchingMaxTarget(index, event)) {
            mLastTouchedMin = false;
            mTouchingMaxTarget.add(event.getPointerId(index));
            return true;
        }
        return false;
    }
 
    private void callMinChangedCallbacks() {
        if (mOnRangeSliderListener != null) {
            mOnRangeSliderListener.onMinChanged(this, getSelectedMin());
        }
    }
 
    private void callMaxChangedCallbacks() {
        if (mOnRangeSliderListener != null) {
            mOnRangeSliderListener.onMaxChanged(this, getSelectedMax());
        }
    }
 
    private boolean isTouchingMinTarget(int pointerIndex, MotionEvent event) {
        return event.getX(pointerIndex) > mMinPosition - DEFAULT_TOUCH_TARGET_SIZE
                && event.getX(pointerIndex) < mMinPosition + DEFAULT_TOUCH_TARGET_SIZE
                && event.getY(pointerIndex) > mMiddleY - DEFAULT_TOUCH_TARGET_SIZE
                && event.getY(pointerIndex) < mMiddleY + DEFAULT_TOUCH_TARGET_SIZE;
    }
 
    private boolean isTouchingMaxTarget(int pointerIndex, MotionEvent event) {
        return event.getX(pointerIndex) > mMaxPosition - DEFAULT_TOUCH_TARGET_SIZE
                && event.getX(pointerIndex) < mMaxPosition + DEFAULT_TOUCH_TARGET_SIZE
                && event.getY(pointerIndex) > mMiddleY - DEFAULT_TOUCH_TARGET_SIZE
                && event.getY(pointerIndex) < mMiddleY + DEFAULT_TOUCH_TARGET_SIZE;
    }
 
    private void calculateConvertFactor() {
        mConvertFactor = ((float) mRange) / mLineLength;
    }
 
    public int getSelectedMin() {
        return Math.round((mMinPosition - mLineStartX) * mConvertFactor + mMin);
    }
 
    public int getSelectedMax() {
        return Math.round((mMaxPosition - mLineStartX) * mConvertFactor + mMin);
    }
 
    public void setStartingMinMax(int startingMin, int startingMax) {
        mStartingMin = startingMin;
        mStartingMax = startingMax;
        setSelectedMin(startingMin);
        setSelectedMax(startingMax);
        invalidate();
    }
 
    private void setSelectedMin(int selectedMin) {
        mMinPosition = Math.round(((selectedMin - mMin) / mConvertFactor) + mLineStartX);
        callMinChangedCallbacks();
    }
 
    private void setSelectedMax(int selectedMax) {
        mMaxPosition = Math.round(((selectedMax - mMin) / mConvertFactor) + mLineStartX);
        callMaxChangedCallbacks();
    }
 
    public void setOnRangeSliderListener(OnRangeSliderListener listener) {
        mOnRangeSliderListener = listener;
    }
 
    public int getMin() {
        return mMin;
    }
 
    public void setMin(int min) {
        mMin = min;
        mRange = mMax - min;
    }
 
    public int getMax() {
        return mMax;
    }
 
    public void setMax(int max) {
        mMax = max;
        mRange = max - mMin;
    }
 
    /**
     * Resets selected values to MIN and MAX.
     */
    public void reset() {
        mMinPosition = mLineStartX;
        mMaxPosition = mLineEndX;
        if (mOnRangeSliderListener != null) {
            mOnRangeSliderListener.onMinChanged(this, getSelectedMin());
            mOnRangeSliderListener.onMaxChanged(this, getSelectedMax());
        }
        invalidate();
    }
 
 
    /**
     * Keeps Number value inside min/max bounds by returning min or max if outside of
     * bounds.  Otherwise will return the value without altering.
     */
    private <T extends Number> T clamp(@NonNull T value, @NonNull T min, @NonNull T max) {
        if (value.doubleValue() > max.doubleValue()) {
            return max;
        } else if (value.doubleValue() < min.doubleValue()) {
            return min;
        }
        return value;
    }
}