安卓ProgressBar.setProgressDrawable只能一次?ProgressBar、setProgressDrawable

2023-09-12 09:10:49 作者:作业是个打不死的boss

在一个颜色选择器,我有3个SeekBars色调,饱和度和明度。从的onCreate在初始化 - 调用setProgressDrawable这些SeekBars只能一次。当用户更新色相搜索栏,我想打电话给setProgressDrawable的饱和度和数值SeekBars,向用户展示他们的色彩选择为新的色调。

In a color picker, I have 3 SeekBars for Hue, Saturation, and Value. Calling setProgressDrawable on these SeekBars only works once -- at initialization from onCreate. When the user updates the Hue SeekBar, I want to call setProgressDrawable for the Saturation and Value SeekBars, to show the user their color choices for the new Hue.

不过,所有调用setProgressDrawable(初始那些从的onCreate 之后)会导致搜索栏被封闭。

But all calls to setProgressDrawable (after the initial ones from onCreate) cause the SeekBar to be blanked.

我如何更新我的SeekBars基于用户输入的背景渐变?

How can I update the background gradient of my SeekBars based upon user input?

推荐答案

我发现的是,绘制不知道它的大小,当setprogressdrawable被调用。当最初设置,它不知道它的大小。这意味着有一个新的绘制设置为搜索栏,但绘制的大小为0,你不会看到任何东西。

What I found out is that the drawable doesn't know it's size when setprogressdrawable is called. When it is initially set up, it does know it's size. This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.

解决的办法是先获取当前绘制的边界,然后设置新的绘制,最后再设定范围:

The solution is to first get the bounds of the current drawable, then set the new drawable and finally set the bounds again:

Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);