Android ProgressBar.setProgressDrawable only works once?

后端 未结 2 1793
灰色年华
灰色年华 2020-12-09 05:15

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

相关标签:
2条回答
  • 2020-12-09 05:50

    Hey I got the solutions to this, you first have to set the drawable to the ProgressBar by calling setProgresssDrawable(drawable) and only then set the value and not vice versa. That'll work.

    To set a progress drawable:

    Use a drawable xml file like this one:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient android:startColor="#00CCCC"
                android:centerColor="#00CCCC" android:centerY="0.75"
                android:endColor="#00CCCC" android:angle="270" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient android:startColor="#00CCCC"
                    android:centerColor="#00CCCC" android:centerY="0.75"
                    android:endColor="#00CCCC" android:angle="270" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient android:startColor="#00CCCC" android:centerColor="#00CCCC"
                    android:centerY="0.75" android:endColor="#00CCCC" android:angle="270" />
            </shape>
        </clip>
    </item>
    

    save this into your /drawable folder and use it in the setProgressDrawable(drawable) function

    0 讨论(0)
  • 2020-12-09 06:10

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