Android seekBar在价值变化时改变颜色

时间:2017-08-22 09:18:12

标签: android

我正在使用itertools.product。 有人知道如何在滚动时更改指示颜色吗?

当我使用时:

 class SeekBarOnProgressChangeListener implements DiscreteSeekBar.OnProgressChangeListener {
    int color;

    public Trap5SeekBarOnProgressChangeListener() {

    }

    @Override
    public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
        seekBar.setIndicatorPopupEnabled(false);

        if(value == 0) {
            color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarGreen);
        } else if(value < 20) {
            color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarGreen);
        } else if(value < 40) {
            color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarYellow);
        } else {
            color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarRed);
        }

        seekBar.setScrubberColor(color);
        seekBar.setThumbColor(color, color);
    }

}

自下次使用seekBar以来,颜色发生了变化,而不是我想要的 证明:discreteSeekBar

2 个答案:

答案 0 :(得分:0)

尝试这种方式

@Override
            public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
                if(value == 0) {
                    color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarGreen);
                } else if(value < 20) {
                    color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarGreen);
                } else if(value < 40) {
                    color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarYellow);
                } else {
                    color = ContextCompat.getColor(seekBar.getContext(), R.color.colorSneekBarRed);
                }
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                seekBar.setScrubberColor(color);
                seekBar.setThumbColor(color, color);
            }

答案 1 :(得分:0)

我成功实现了目标,这就是解决方案:

class SeekBarOnProgressChangeListener implements DiscreteSeekBar.OnProgressChangeListener {
    int color;
    int colorGreen;
    int colorYellow;
    int colorRed;

    public SeekBarOnProgressChangeListener() {
        colorGreen = ContextCompat.getColor(getContext(), R.color.colorSneekBarGreen);
        colorYellow = ContextCompat.getColor(getContext(), R.color.colorSneekBarYellow);
        colorRed= ContextCompat.getColor(getContext(), R.color.colorSneekBarRed);
    }

    @Override
    public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
        boolean colorChanged = false;

        if(value < 20) {
            if(color != colorGreen){
                color = colorGreen;
                colorChanged = true;
            }
        } else if(value < 40) {
            if(color != colorYellow){
                color = colorYellow;
                colorChanged = true;
            }
        } else {
            if(color != colorRed){
                color = colorRed;
                colorChanged = true;
            }

        }

        if(colorChanged) {
            seekBar.setScrubberColor(color);
            seekBar.setThumbColor(color, color);

            MotionEvent motionEvent = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0);
            seekBar.dispatchTouchEvent(motionEvent);
        }

    }

当设置新颜色时,拇指会以更改的颜色重新出现,这对我来说已经足够了,但是如果有人有一个改变拇指颜色的解决方案而不重新创建它,请在此处发布解决方案。