/
ColorSeekBar.java
803 lines (688 loc) · 25.5 KB
/
ColorSeekBar.java
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package com.rtugeek.android.colorseekbar;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import androidx.annotation.ArrayRes;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class ColorSeekBar extends View {
private int[] mColorSeeds = new int[]{0xFF000000, 0xFF9900FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
0xFFFF0000, 0xFFFF00FF, 0xFFFF6600, 0xFFFFFF00, 0xFFFFFFFF, 0xFF000000};
private int mAlpha;
private OnColorChangeListener mOnColorChangeLister;
private Context mContext;
private boolean mIsShowAlphaBar = false;
private boolean mIsShowColorBar = true;
private boolean mIsVertical;
private boolean mMovingColorBar;
private boolean mMovingAlphaBar;
private Bitmap mTransparentBitmap;
private RectF mColorRect;
private int mThumbHeight = 20;
private float mThumbRadius;
private int mBarHeight = 2;
private Paint mColorRectPaint;
private int realLeft;
private int realRight;
private int mBarWidth;
private int mMaxPosition;
private RectF mAlphaRect = new RectF();
private int mColorBarPosition;
private int mAlphaBarPosition;
private int mDisabledColor;
private int mBarMargin = 5;
private int mAlphaMinPosition = 0;
private int mAlphaMaxPosition = 255;
private int mBarRadius;
private List<Integer> mCachedColors = new ArrayList<>();
private int mColorsToInvoke = -1;
private boolean mInit = false;
private boolean mFirstDraw = true;
private boolean mShowThumb = true;
private OnInitDoneListener mOnInitDoneListener;
private Paint colorPaint = new Paint();
private Paint alphaThumbGradientPaint = new Paint();
private Paint alphaBarPaint = new Paint();
private Paint mDisabledPaint = new Paint();
private Paint thumbGradientPaint = new Paint();
public ColorSeekBar(Context context) {
super(context);
init(context, null, 0, 0);
}
public ColorSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public ColorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ColorSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
applyStyle(context, attrs, defStyleAttr, defStyleRes);
}
public void applyStyle(int resId) {
applyStyle(getContext(), null, 0, resId);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Logger.i("onMeasure");
int mViewWidth = widthMeasureSpec;
int mViewHeight = heightMeasureSpec;
int widthSpeMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpeMode = MeasureSpec.getMode(heightMeasureSpec);
int barHeight = (mIsShowAlphaBar && mIsShowColorBar) ? mBarHeight * 2 : mBarHeight;
int thumbHeight = (mIsShowAlphaBar && mIsShowColorBar) ? mThumbHeight * 2 : mThumbHeight;
Logger.i("widthSpeMode:");
Logger.spec(widthSpeMode);
Logger.i("heightSpeMode:");
Logger.spec(heightSpeMode);
if (isVertical()) {
if (widthSpeMode == MeasureSpec.AT_MOST || widthSpeMode == MeasureSpec.UNSPECIFIED) {
mViewWidth = thumbHeight + barHeight + mBarMargin;
setMeasuredDimension(mViewWidth, mViewHeight);
}
} else {
if (heightSpeMode == MeasureSpec.AT_MOST || heightSpeMode == MeasureSpec.UNSPECIFIED) {
mViewHeight = thumbHeight + barHeight + mBarMargin;
setMeasuredDimension(mViewWidth, mViewHeight);
}
}
}
protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
mContext = context;
//get attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorSeekBar, defStyleAttr, defStyleRes);
int colorsId = a.getResourceId(R.styleable.ColorSeekBar_colorSeeds, 0);
mMaxPosition = a.getInteger(R.styleable.ColorSeekBar_maxPosition, 100);
mColorBarPosition = a.getInteger(R.styleable.ColorSeekBar_colorBarPosition, 0);
mAlphaBarPosition = a.getInteger(R.styleable.ColorSeekBar_alphaBarPosition, mAlphaMinPosition);
mDisabledColor = a.getInteger(R.styleable.ColorSeekBar_disabledColor, Color.GRAY);
mIsVertical = a.getBoolean(R.styleable.ColorSeekBar_isVertical, false);
mIsShowAlphaBar = a.getBoolean(R.styleable.ColorSeekBar_showAlphaBar, false);
mIsShowColorBar = a.getBoolean(R.styleable.ColorSeekBar_showColorBar, true);
mShowThumb = a.getBoolean(R.styleable.ColorSeekBar_showThumb, true);
int backgroundColor = a.getColor(R.styleable.ColorSeekBar_bgColor, Color.TRANSPARENT);
mBarHeight = (int) a.getDimension(R.styleable.ColorSeekBar_barHeight, (float) dp2px(2));
mBarRadius = (int) a.getDimension(R.styleable.ColorSeekBar_barRadius, 0);
mThumbHeight = (int) a.getDimension(R.styleable.ColorSeekBar_thumbHeight, (float) dp2px(30));
mBarMargin = (int) a.getDimension(R.styleable.ColorSeekBar_barMargin, (float) dp2px(5));
a.recycle();
mDisabledPaint.setAntiAlias(true);
mDisabledPaint.setColor(mDisabledColor);
if (colorsId != 0) {
mColorSeeds = getColorsById(colorsId);
}
setBackgroundColor(backgroundColor);
}
/**
* @param id color array resource
* @return
*/
private int[] getColorsById(@ArrayRes int id) {
if (isInEditMode()) {
String[] s = mContext.getResources().getStringArray(id);
int[] colors = new int[s.length];
for (int j = 0; j < s.length; j++) {
colors[j] = Color.parseColor(s[j]);
}
return colors;
} else {
TypedArray typedArray = mContext.getResources().obtainTypedArray(id);
int[] colors = new int[typedArray.length()];
for (int j = 0; j < typedArray.length(); j++) {
colors[j] = typedArray.getColor(j, Color.BLACK);
}
typedArray.recycle();
return colors;
}
}
private void init() {
Logger.i("init");
//init size
mThumbRadius = mThumbHeight / 2;
int mPaddingSize = (int) mThumbRadius;
int viewBottom = getHeight() - getPaddingBottom() - mPaddingSize;
int viewRight = getWidth() - getPaddingRight() - mPaddingSize;
//init left right top bottom
realLeft = getPaddingLeft() + mPaddingSize;
realRight = mIsVertical ? viewBottom : viewRight;
int realTop = getPaddingTop() + mPaddingSize;
mBarWidth = realRight - realLeft;
//init rect
mColorRect = new RectF(realLeft, realTop, realRight, realTop + mBarHeight);
//init paint
LinearGradient mColorGradient = new LinearGradient(0, 0, mColorRect.width(), 0, mColorSeeds, null, Shader.TileMode.CLAMP);
mColorRectPaint = new Paint();
mColorRectPaint.setShader(mColorGradient);
mColorRectPaint.setAntiAlias(true);
cacheColors();
setAlphaValue();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Logger.i("onSizeChanged");
if (mIsVertical) {
mTransparentBitmap = Bitmap.createBitmap(h, w, Bitmap.Config.ARGB_4444);
} else {
mTransparentBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
}
mTransparentBitmap.eraseColor(Color.TRANSPARENT);
init();
mInit = true;
if (mColorsToInvoke != -1) {
setColor(mColorsToInvoke);
}
}
private void cacheColors() {
//if the view's size hasn't been initialized. do not cache.
if (mBarWidth < 1) {
return;
}
mCachedColors.clear();
for (int i = 0; i <= mMaxPosition; i++) {
mCachedColors.add(pickColor(i));
}
}
@Override
protected void onDraw(Canvas canvas) {
Logger.i("onDraw");
if (mIsVertical) {
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
canvas.scale(-1, 1, getHeight() / 2, getWidth() / 2);
}
int color = isEnabled() ? getColor(false) : mDisabledColor;
int colorStartTransparent = Color.argb(mAlphaMaxPosition, Color.red(color), Color.green(color), Color.blue(color));
int colorEndTransparent = Color.argb(mAlphaMinPosition, Color.red(color), Color.green(color), Color.blue(color));
int[] toAlpha = new int[]{colorStartTransparent, colorEndTransparent};
if (mIsShowColorBar) {
float colorPosition = (float) mColorBarPosition / mMaxPosition * mBarWidth;
colorPaint.setAntiAlias(true);
colorPaint.setColor(color);
//clear
canvas.drawBitmap(mTransparentBitmap, 0, 0, null);
//draw color bar
canvas.drawRoundRect(mColorRect, mBarRadius, mBarRadius, isEnabled() ? mColorRectPaint : mDisabledPaint);
//draw color bar thumb
if (mShowThumb) {
float thumbX = colorPosition + realLeft;
float thumbY = mColorRect.top + mColorRect.height() / 2;
canvas.drawCircle(thumbX, thumbY, mBarHeight / 2 + 5, colorPaint);
//draw color bar thumb radial gradient shader
RadialGradient thumbShader = new RadialGradient(thumbX, thumbY, mThumbRadius, toAlpha, null, Shader.TileMode.MIRROR);
thumbGradientPaint.setAntiAlias(true);
thumbGradientPaint.setShader(thumbShader);
canvas.drawCircle(thumbX, thumbY, mThumbHeight / 2, thumbGradientPaint);
}
}
if (mIsShowAlphaBar) {
//init rect
if (mIsShowColorBar) {
int top = mIsShowColorBar ? (int) (mThumbHeight + mThumbRadius + mBarHeight + mBarMargin) :
(int) (mThumbHeight + mThumbRadius + mBarMargin);
mAlphaRect = new RectF(realLeft, top, realRight, top + mBarHeight);
} else {
mAlphaRect = new RectF(mColorRect);
}
//draw alpha bar
alphaBarPaint.setAntiAlias(true);
LinearGradient alphaBarShader = new LinearGradient(0, 0, mAlphaRect.width(), 0, toAlpha, null, Shader.TileMode.CLAMP);
alphaBarPaint.setShader(alphaBarShader);
canvas.drawRect(mAlphaRect, alphaBarPaint);
//draw alpha bar thumb
if (mShowThumb) {
float alphaPosition = (float) (mAlphaBarPosition - mAlphaMinPosition) / (mAlphaMaxPosition - mAlphaMinPosition) * mBarWidth;
float alphaThumbX = alphaPosition + realLeft;
float alphaThumbY = mAlphaRect.top + mAlphaRect.height() / 2;
canvas.drawCircle(alphaThumbX, alphaThumbY, mBarHeight / 2 + 5, colorPaint);
//draw alpha bar thumb radial gradient shader
RadialGradient alphaThumbShader = new RadialGradient(alphaThumbX, alphaThumbY, mThumbRadius, toAlpha, null, Shader.TileMode.MIRROR);
alphaThumbGradientPaint.setAntiAlias(true);
alphaThumbGradientPaint.setShader(alphaThumbShader);
canvas.drawCircle(alphaThumbX, alphaThumbY, mThumbHeight / 2, alphaThumbGradientPaint);
}
}
if (mFirstDraw) {
if (mOnColorChangeLister != null) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
mFirstDraw = false;
if (mOnInitDoneListener != null) {
mOnInitDoneListener.done();
}
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return true;
}
float x = mIsVertical ? event.getY() : event.getX();
float y = mIsVertical ? event.getX() : event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mIsShowColorBar && isOnBar(mColorRect, x, y)) {
mMovingColorBar = true;
float value = (x - realLeft) / mBarWidth * mMaxPosition;
setColorBarPosition((int) value);
} else if (mIsShowAlphaBar && isOnBar(mAlphaRect, x, y)) {
mMovingAlphaBar = true;
}
break;
case MotionEvent.ACTION_MOVE:
getParent().requestDisallowInterceptTouchEvent(true);
if (mMovingColorBar) {
float value = (x - realLeft) / mBarWidth * mMaxPosition;
setColorBarPosition((int) value);
} else if (mIsShowAlphaBar) {
if (mMovingAlphaBar) {
float value = (x - realLeft) / (float) mBarWidth * (mAlphaMaxPosition - mAlphaMinPosition) + mAlphaMinPosition;
mAlphaBarPosition = (int) value;
if (mAlphaBarPosition < mAlphaMinPosition) {
mAlphaBarPosition = mAlphaMinPosition;
} else if (mAlphaBarPosition > mAlphaMaxPosition) {
mAlphaBarPosition = mAlphaMaxPosition;
}
setAlphaValue();
}
}
if (mOnColorChangeLister != null && (mMovingAlphaBar || mMovingColorBar)) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
invalidate();
break;
case MotionEvent.ACTION_UP:
mMovingColorBar = false;
mMovingAlphaBar = false;
break;
default:
}
return true;
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
}
/***
*
* @param alphaMaxPosition <= 255 && > alphaMinPosition
*/
public void setAlphaMaxPosition(int alphaMaxPosition) {
mAlphaMaxPosition = alphaMaxPosition;
if (mAlphaMaxPosition > 255) {
mAlphaMaxPosition = 255;
} else if (mAlphaMaxPosition <= mAlphaMinPosition) {
mAlphaMaxPosition = mAlphaMinPosition + 1;
}
if (mAlphaBarPosition > mAlphaMinPosition) {
mAlphaBarPosition = mAlphaMaxPosition;
}
invalidate();
}
public int getAlphaMaxPosition() {
return mAlphaMaxPosition;
}
/***
*
* @param alphaMinPosition >=0 && < alphaMaxPosition
*/
public void setAlphaMinPosition(int alphaMinPosition) {
this.mAlphaMinPosition = alphaMinPosition;
if (mAlphaMinPosition >= mAlphaMaxPosition) {
mAlphaMinPosition = mAlphaMaxPosition - 1;
} else if (mAlphaMinPosition < 0) {
mAlphaMinPosition = 0;
}
if (mAlphaBarPosition < mAlphaMinPosition) {
mAlphaBarPosition = mAlphaMinPosition;
}
invalidate();
}
public int getAlphaMinPosition() {
return mAlphaMinPosition;
}
/**
* @param r
* @param x
* @param y
* @return whether MotionEvent is performing on bar or not
*/
private boolean isOnBar(RectF r, float x, float y) {
if (r.left - mThumbRadius < x && x < r.right + mThumbRadius && r.top - mThumbRadius < y && y < r.bottom + mThumbRadius) {
return true;
} else {
return false;
}
}
/**
* @return
* @deprecated use {@link #setOnInitDoneListener(OnInitDoneListener)} instead.
*/
public boolean isFirstDraw() {
return mFirstDraw;
}
/**
* @param value
* @return color
*/
private int pickColor(int value) {
return pickColor((float) value / mMaxPosition * mBarWidth);
}
/**
* @param position
* @return color
*/
private int pickColor(float position) {
float unit = position / mBarWidth;
if (unit <= 0.0) {
return mColorSeeds[0];
}
if (unit >= 1) {
return mColorSeeds[mColorSeeds.length - 1];
}
float colorPosition = unit * (mColorSeeds.length - 1);
int i = (int) colorPosition;
colorPosition -= i;
int c0 = mColorSeeds[i];
int c1 = mColorSeeds[i + 1];
// mAlpha = mix(Color.alpha(c0), Color.alpha(c1), colorPosition);
int mRed = mix(Color.red(c0), Color.red(c1), colorPosition);
int mGreen = mix(Color.green(c0), Color.green(c1), colorPosition);
int mBlue = mix(Color.blue(c0), Color.blue(c1), colorPosition);
return Color.rgb(mRed, mGreen, mBlue);
}
/**
* @param start
* @param end
* @param position
* @return
*/
private int mix(int start, int end, float position) {
return start + Math.round(position * (end - start));
}
public int getColor() {
return getColor(mIsShowAlphaBar);
}
/**
* @param withAlpha
* @return
*/
public int getColor(boolean withAlpha) {
//pick mode
if (mColorBarPosition >= mCachedColors.size()) {
int color = pickColor(mColorBarPosition);
if (withAlpha) {
return color;
} else {
return Color.argb(getAlphaValue(), Color.red(color), Color.green(color), Color.blue(color));
}
}
//cache mode
int color = mCachedColors.get(mColorBarPosition);
if (withAlpha) {
return Color.argb(getAlphaValue(), Color.red(color), Color.green(color), Color.blue(color));
}
return color;
}
public int getAlphaBarPosition() {
return mAlphaBarPosition;
}
public int getAlphaValue() {
return mAlpha;
}
public interface OnColorChangeListener {
/**
* @param colorBarPosition between 0-maxValue
* @param alphaBarPosition between 0-255
* @param color return the color contains alpha value whether showAlphaBar is true or without alpha value
*/
void onColorChangeListener(int colorBarPosition, int alphaBarPosition, int color);
}
/**
* @param onColorChangeListener
*/
public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
this.mOnColorChangeLister = onColorChangeListener;
}
public int dp2px(float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* Set colors by resource id. The resource's type must be ArrayRes
*
* @param resId
*/
public void setColorSeeds(@ArrayRes int resId) {
setColorSeeds(getColorsById(resId));
}
public void setColorSeeds(int[] colors) {
mColorSeeds = colors;
init();
invalidate();
if (mOnColorChangeLister != null) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
}
/**
* @param color
* @return the color's position in the bar, if not in the bar ,return -1;
*/
public int getColorIndexPosition(int color) {
return mCachedColors.indexOf(Color.argb(255, Color.red(color), Color.green(color), Color.blue(color)));
}
public List<Integer> getColors() {
return mCachedColors;
}
public boolean isShowAlphaBar() {
return mIsShowAlphaBar;
}
private void refreshLayoutParams() {
setLayoutParams(getLayoutParams());
}
// public void setVertical(boolean vertical) {
// mIsVertical = vertical;
// refreshLayoutParams();
// invalidate();
// }
public boolean isVertical() {
return mIsVertical;
}
public void setShowAlphaBar(boolean show) {
mIsShowAlphaBar = show;
refreshLayoutParams();
invalidate();
if (mOnColorChangeLister != null) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
}
/**
* @param dp
*/
public void setBarHeight(float dp) {
mBarHeight = dp2px(dp);
refreshLayoutParams();
invalidate();
}
/**
* @param px
*/
public void setBarHeightPx(int px) {
mBarHeight = px;
refreshLayoutParams();
invalidate();
}
private void setAlphaValue() {
mAlpha = 255 - mAlphaBarPosition;
}
private void setAlphaValue(int value) {
mAlpha = value;
mAlphaBarPosition = 255 - mAlpha;
// invalidate();
}
public void setAlphaBarPosition(int position) {
setPosition(mColorBarPosition,position);
}
public int getMaxValue() {
return mMaxPosition;
}
public void setMaxPosition(int value) {
this.mMaxPosition = value;
invalidate();
cacheColors();
}
/**
* set margin between bars
*
* @param mBarMargin
*/
public void setBarMargin(float mBarMargin) {
this.mBarMargin = dp2px(mBarMargin);
refreshLayoutParams();
invalidate();
}
/**
* set margin between bars
*
* @param mBarMargin
*/
public void setBarMarginPx(int mBarMargin) {
this.mBarMargin = mBarMargin;
refreshLayoutParams();
invalidate();
}
/**
* Set the value of color bar, if out of bounds , it will be 0 or maxValue;
*
* @param value
*/
public void setColorBarPosition(int value) {
setPosition(value,mAlphaBarPosition);
}
public void setPosition(int colorBarPosition,int alphaBarPosition) {
this.mColorBarPosition = colorBarPosition;
mColorBarPosition = mColorBarPosition > mMaxPosition ? mMaxPosition : mColorBarPosition;
mColorBarPosition = mColorBarPosition < 0 ? 0 : mColorBarPosition;
this.mAlphaBarPosition = alphaBarPosition;
setAlphaValue();
invalidate();
if (mOnColorChangeLister != null) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
}
public void setOnInitDoneListener(OnInitDoneListener listener) {
this.mOnInitDoneListener = listener;
}
/**
* Set color,the mCachedColors must contains the specified color, if not ,invoke setColorBarPosition(0);
*
* @param color
*/
public void setColor(int color) {
int withoutAlphaColor = Color.rgb(Color.red(color), Color.green(color), Color.blue(color));
if (mInit) {
int value = mCachedColors.indexOf(withoutAlphaColor);
if (mIsShowAlphaBar) {
setAlphaValue(Color.alpha(color));
}
setColorBarPosition(value);
} else {
mColorsToInvoke = color;
}
}
/**
* set thumb's height by dpi
*
* @param dp
*/
public void setThumbHeight(float dp) {
this.mThumbHeight = dp2px(dp);
mThumbRadius = mThumbHeight / 2;
refreshLayoutParams();
invalidate();
}
/**
* set thumb's height by pixels
*
* @param px
*/
public void setThumbHeightPx(int px) {
this.mThumbHeight = px;
mThumbRadius = mThumbHeight / 2;
refreshLayoutParams();
invalidate();
}
public int getBarHeight() {
return mBarHeight;
}
public int getThumbHeight() {
return mThumbHeight;
}
public int getBarMargin() {
return mBarMargin;
}
public float getColorBarValue() {
return mColorBarPosition;
}
public interface OnInitDoneListener {
void done();
}
public int getColorBarPosition() {
return mColorBarPosition;
}
public int getDisabledColor() {
return mDisabledColor;
}
public void setDisabledColor(int disabledColor) {
this.mDisabledColor = disabledColor;
mDisabledPaint.setColor(disabledColor);
}
public boolean isShowThumb() {
return mShowThumb;
}
public void setShowThumb(boolean showThumb) {
this.mShowThumb = showThumb;
invalidate();
}
public int getBarRadius() {
return mBarRadius;
}
/**
* Set bar radius with px unit
*
* @param barRadiusInPx
*/
public void setBarRadius(int barRadiusInPx) {
this.mBarRadius = barRadiusInPx;
invalidate();
}
public boolean isIsShowColorBar() {
return mIsShowColorBar;
}
public void setShowColorBar(boolean isShowColorBar) {
this.mIsShowColorBar = isShowColorBar;
refreshLayoutParams();
invalidate();
}
}