How to get current color from Linear Gradient?

流过昼夜 提交于 2019-12-20 03:32:28

问题


I have an Seek Bar whose values ranges from 1 to 10. The THUMB stops at 1,2,3,4,5 ... 10.

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?


回答1:


pskink's suggestion is right. You can use an ArgbEvaluator to achieve this goal.

Let's assume this is your SeekBar:

 C1                                  C2                                  C3
 +-------|-------|-------|-------|---+---|-------|-------|-------|-------+                          
 1       2       3       4       5       6       7       8       9       10

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).

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

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


来源:https://stackoverflow.com/questions/33460225/how-to-get-current-color-from-linear-gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!