pskink的建议 https://stackoverflow.com/questions/33460225/how-to-get-current-color-from-linear-gradient#comment54706585_33460225是对的。您可以使用ArgbEvaluator
为了实现这个目标。
我们假设这是你的SeekBar
:
C1 C2 C3
+-------|-------|-------|-------|---+---|-------|-------|-------|-------+
1 2 3 4 5 6 7 8 9 10
你有 10 个Thumb
位置(数字来自1
- 10
), 3 种颜色 (+
符号表示颜色的位置,并且C1
, C2
and C3
代表颜色的名称)。
之间的距离C1
and C2
(以及之间C2
and C3
)可分为9块。这9件可以代表你Thumb
职位:
C1 C2 C3
+-------|-------|-------|-------|---+---|-------|-------|-------|-------+
| | | | | | | | | | |
0/9 2/9 4/9 6/9 8/9 9/9 | | | | |
| | | | | |
0/9 1/9 3/9 5/9 7/9 9/9
因此你的价值观SeekBar
可以这样计算:
int c1 = 0xFFFF0000; // ARGB representation of RED
int c2 = 0xFFFFFF00; // ARGB representation of YELLOW
int c3 = 0xFF00FF00; // ARGB representation of GREEN
ArgbEvaluator evaluator = new ArgbEvaluator();
int thumb1 = (int) evaluator.evaluate(0f, c1, c2); // 0f/9f = 0f
int thumb2 = (int) evaluator.evaluate(2f / 9f, c1, c2);
int thumb3 = (int) evaluator.evaluate(4f / 9f, c1, c2);
int thumb4 = (int) evaluator.evaluate(6f / 9f, c1, c2);
int thumb5 = (int) evaluator.evaluate(8f / 9f, c1, c2);
int thumb6 = (int) evaluator.evaluate(1f / 9f, c2, c3);
int thumb7 = (int) evaluator.evaluate(3f / 9f, c2, c3);
int thumb8 = (int) evaluator.evaluate(5f / 9f, c2, c3);
int thumb9 = (int) evaluator.evaluate(7f / 9f, c2, c3);
int thumb10 = (int) evaluator.evaluate(1f, c2, c3); // 9f/9f = 1f