如何从线性渐变获取当前颜色? [英] How to get current color from Linear Gradient?
问题描述
我有一个Seek Bar,值范围从1到10.ThUMB停在1,2,3,4,5 ... 10。
I have an Seek Bar whose values ranges from 1 to 10. The THUMB stops at 1,2,3,4,5 ... 10.
背景颜色如果SeekBar是线性渐变[颜色从红色开始,然后黄色,最后绿色]。如何获取缩略图所在位置的当前颜色?
The background color if SeekBar is Linear Gradient [Colors Start from RED, then YELLOW and lastly GREEN]. How to get the current color where thumb is positioned?
推荐答案
pskink的建议是对的。您可以使用 ArgbEvaluator
达到此目标。
pskink's suggestion is right. You can use an ArgbEvaluator
to achieve this goal.
让我们假设这是您的 SeekBar
:
C1 C2 C3
+-------|-------|-------|-------|---+---|-------|-------|-------|-------+
1 2 3 4 5 6 7 8 9 10
您有10个 Thumb
位置(来自 1
- 10的数字
),3种颜色( +
符号表示颜色的位置, C1
C2
和 C3
代表颜色的名称)。
You've got 10 Thumb
positions (numbers from 1
- 10
), 3 colors (+
sign indicates the position of the color, and C1
, C2
and C3
represent the name of the color).
C1
和 C2
之间的距离(以及 C2
和 C3
)可以分为9个部分。这9个可代表您的 Thumb
位置:
The distance between C1
and C2
(as well as between C2
and C3
) can be devided into 9 pieces. Those 9 pieces can represent your Thumb
positions:
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
的值可以通过以下方式计算:
Therefore the values of your SeekBar
can be calculated this way:
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
这篇关于如何从线性渐变获取当前颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!