【问题标题】:Android - ProgressBar setProgress() method doesn't update the progress bar drawableAndroid - ProgressBar setProgress() 方法不更新可绘制的进度条
【发布时间】:2017-04-19 07:38:58
【问题描述】:

我设置了一个圆形进度条(从 0 到 100)来显示时间运行进度(从 1:00 到 0:00)。当时间为 1:00 时,进度必须为 0,当时间为 0:00 时,进度必须为 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" />

这是进度条背景:

    <?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>

这是图片背景:

<?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>.

所以,在我的布局预览中,它看起来像这样:

在我的onCreate 中,我将进度设置为 0

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

然后,当我的时间在它的线程中运行时,计时器会自行更新:

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);
                }
            });
        }
}

当我运行活动时,进度条显示 100,以为应该是 0(黑色圆圈)。并且它在运行期间没有显示任何变化。文本视图正常更新,但进度条显示没有变化。 日志显示进度正在更新,但图形上没有任何变化。

我试图通过发布 Runnable 来更新它,我也检查了这个问题,但答案对我没有帮助

android progressBar does not update progress view/drawable

你能建议我任何解决方案吗?

【问题讨论】:

  • 你如何调用TimeRun?
  • 我通过 timeRun.start();该线程工作正常,因为我看到了文本和进度更新(通过日志)。
  • 您是否尝试在每次迭代时打印您提供给setProgress 的值,以确保它按照您的预期从 100 变为 0?
  • 是的,日志显示我从 0 到 100,正如我所料。
  • @HaykAbelyan,你可以发布一个具有这种行为的简单项目,我会看看。

标签: java android multithreading


【解决方案1】:

看看这个解决方案。有效。 http://demonuts.com/2017/01/02/android-circular-progress-bar-percentage/ 这是由于您的进度条背景造成的。

【讨论】:

    【解决方案2】:

    你应该改用CountDownTimer,这比使用线程更容易:

    // 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();
    

    如果它仍然不适合你,那么你应该在更新 ProgressBar 时使用 Synchized(),因为我遇到了同样的问题,这解决了:

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

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

    【讨论】:

    • 这并不能说明为什么 ProgressBar 没有被更新。
    • 仍然没有变化。肯定问题出在进度条本身。
    【解决方案3】:

    这样试试

    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);
                }
            });
    

    【讨论】:

      【解决方案4】:

      只需使用水平进度条样式并使用圆形可绘制对象。

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

      【讨论】:

        猜你喜欢
        • 2011-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        相关资源
        最近更新 更多