How to get current color from Linear Gradient?

后端 未结 1 928
轻奢々
轻奢々 2021-01-22 07:24

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

相关标签:
1条回答
  • 2021-01-22 07:56

    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
    
    0 讨论(0)
提交回复
热议问题