简体   繁体   中英

Android - ProgressBar setProgress() method doesn't update the progress bar drawable

I set a circular progress bar (from 0 to 100) for showing a time running progress (from 1:00 to 0:00). When time is 1:00, the progress must be 0, when time is 0:00, the progress must be 100.

<ImageView
    android:layout_width="@dimen/_56sdp"
    android:layout_height="@dimen/_56sdp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/_2sdp"
    android:src="@drawable/a_circular_black" />

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="@dimen/_56sdp"
    android:layout_height="@dimen/_56sdp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/_2sdp"
    android:max="100"
    android:progress="26" //I set the progress 26 to show what it will be look like. Normally for 1:00 the progress must be 0.
    android:progressDrawable="@drawable/a_circular" />

<TextView
    android:id="@+id/textViewTimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/_17sdp"
    android:text="1:00"
    android:textColor="#000000"
    android:textSize="@dimen/_18sdp"
    android:textStyle="bold" />

Here's the progress bar background:

    <?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%" >
            <shape
                android:innerRadiusRatio="2.5"
                android:shape="ring"
                android:thickness="@dimen/_3sdp" >
                <size android:width="@dimen/_56sdp" android:height="@dimen/_56sdp" />
                <gradient
                    android:centerColor="#FFA708"
                    android:endColor="#FFA708"
                    android:startColor="#FFA708"
                    android:type="sweep" />
            </shape>
        </rotate>
    </item>
</layer-list>

Here's the image background:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <size
                android:width="60dp"
                android:height="60dp" />
            <stroke
                android:width="3dp"
                android:color="#ccc" />
            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="3dp"
                android:top="3dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">
            <shape
                android:shape="oval">
                <size
                    android:width="60dp"
                    android:height="60dp" />
                <gradient
                    android:centerColor="#000"
                    android:endColor="#000"
                    android:startColor="#000"
                    android:type="sweep" />

                <padding
                    android:bottom="3dp"
                    android:left="3dp"
                    android:right="3dp"
                    android:top="3dp" />
            </shape>
        </rotate>
    </item>
    <item>
        <shape android:shape="oval">
            <size
                android:width="60dp"
                android:height="60dp" />
            <solid android:color="#ccc" />
        </shape>
    </item>
</layer-list>.

So, on my layout preview it appears like this:

我将进度设置为 26 以显示它将是什么样子。通常对于 1:00,进度必须为 0。

In my onCreate I set the progress 0

progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgress(0); 

And then, when my time runs in it's thread, the timer updates itself:

private class TimeRun implements Runnable {

    @Override
    public void run() {
        while (timeLeft > 0) {
            try {
                Thread.sleep(100);
                timeLeft = timeLeft - 100;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setProgress(100 - (int) ((double) timeLeft / ((double) 60000) * 100));
                    if (timeLeft < 10000)
                        textViewTimer.setText("0:0" + timeLeft / 1000);
                    else
                        textViewTimer.setText("0:" + timeLeft / 1000);
                }
            });
        }
}

When I run the activity, the progress bar is showing 100, thought it should have been 0 (black circle). And it shows no change during the run time. The text view updates normally, but progress bar shows no change. The logs show me that the progress is updating, but graphically there are no changes.

进度条不更新 I tried to update it by posting a Runnable, I checked this question too, but the answers didn't help me

android progressBar does not update progress view/drawable

Can you suggest me any solution?

You should use CountDownTimer instead, this will be easier than using a thread:

// here your total time is 1 minute = 1 * 60 * 1000 ms
// and ticks time is 1 sec = 1000 ms
new CountDownTimer(1 * 60 * 1000, 1000) {

    public void onTick(long millisUntilFinished) {
       // this will be alled in interval of 1 sec
       // here you can have your logic to update progressBar or textbar
    }

    public void onFinish() {
       // this will be called when timer will be fixished - afetr 1 min
       // here you can have your logic to update progressBar to 0 again
    }

}.start();

If it still not working for you then you should use Synchized() when updating ProgressBar, because I was getting same issue and this solved that :

synchronized (this) {
    progressBar.setProgress(100 - (int) ((double) timeLeft / ((double) 60000) * 100));
}

or

synchronized (YourActivity.this) {
    progressBar.setProgress(100 - (int) ((double) timeLeft / ((double) 60000) * 100));
}

Take a look this solution. It worked. http://demonuts.com/2017/01/02/android-circular-progress-bar-percentage/ It is due to your progress bar background.

Try like this

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Integer value  =  100 - (int) ((double) timeLeft / ((double) 60000) * 100);
                progressBar.setProgress(value);
                if (timeLeft < 10000)
                    textViewTimer.setText("0:0" + timeLeft / 1000);
                else
                    textViewTimer.setText("0:" + timeLeft / 1000);
            }
        });

Just use Horizontal progress bar style and use circular drawable.

 style="?android:attr/progressBarStyleHorizontal"
 android:progressDrawable="@drawable/circular_progress"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM