Changing color according to seek bar value

强颜欢笑 提交于 2019-12-05 23:46:22

问题


I am trying to change color according to value of seek bar. I'd like colors to appear in wide range.

Here is what I tried, but it doesn't give me colors I want:

seekBarColor.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
        int progress = 0;
          @Override
          public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            progress = progresValue;
            int lower = Color.argb(0xFF,progress*0x10000, progress*0x100, progress)+0xff000000;
            color.setBackgroundColor(lower);
          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {

          }

          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {
              db.updateColor("");
          }
    });

Any ideas?


回答1:


If you need only bright, saturated colors, use Color.HSVToColor() instead of setting R, G and B components directly:

float[] hsvColor = {0, 1, 1};
// generate only hue component in range [0, 360),
// leaving saturation and brightness maximum possible
hsvColor[0] = 360f * progress / maxProgress;
view.setBackgroundColor(Color.HSVToColor(hsvColor));

This code will set color starting from red, then change smoothly to orange, yellow, green, blue and magenta back to red as progress is changing from 0 to maxProgress




回答2:


You have to pass 0-255 values to Color.argb(int,int,int,int) method.

This might work?

Color.argb(0xFF, progress, progress, progress) //will return some gray color based on progress. 

You might have to scale your progress value based on the min and max value. For example if you have progress 0-100 then you should calculate your color values with progress * 255 / 100



来源:https://stackoverflow.com/questions/23043321/changing-color-according-to-seek-bar-value

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