Android SeekBar: How to change the color of the full seekbar on progress

余生长醉 提交于 2019-12-12 01:06:42

问题


i would like to a seek bar which is colored green when progress is at 0. The colour slowly changes to yellow when the progress reaches 50 and then again slowly changes to red when progress is 100.

I am able to do this for the progress portion of the seekbar.

But I would like to have the full colour of the seek bar change, not just the progress portion.

Could someone please help?

Thanks.


回答1:


try this, you need to change hsv array to meet your needs

final ProgressBar pb = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
LayerDrawable ld = (LayerDrawable) pb.getProgressDrawable();
final Drawable progressDrawable = ld.findDrawableByLayerId(android.R.id.progress);
OnTouchListener l = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int progress = (int) (event.getX() * 100 / pb.getWidth());
        float[] hsv = {
                event.getX() * 360 / pb.getWidth(), 1, 1
        };
        int color = Color.HSVToColor(hsv);
        progressDrawable.setColorFilter(color, Mode.SRC);
        pb.setProgress(progress);
        return true;
    }
};
pb.setOnTouchListener(l);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 32);
setContentView(pb, lp);


来源:https://stackoverflow.com/questions/20452083/android-seekbar-how-to-change-the-color-of-the-full-seekbar-on-progress

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