简体   繁体   English

Android Studio中的倒计时进度条

[英]Countdown progress bar in Android Studio

I'm trying to make a countdown timer in form of a progress bar, and that's what i tried so far: but it don't work at all.我正在尝试以进度条的形式制作倒数计时器,这就是我到目前为止所尝试的:但它根本不起作用。

Layout XML File:布局 XML 文件:

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".thetest">

    <ProgressBar
        android:id="@+id/progressBarToday"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="79dp"
        android:layout_height="101dp"
        android:layout_centerInParent="true"
        android:indeterminate="false"
        android:max="30"
        android:progress="29"
        android:progressDrawable="@drawable/circular_progress_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java: Java:

public class thetest extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thetest);
        ProgressBar pb = (ProgressBar) findViewById(R.id.progressBarToday);

        Animation an = new RotateAnimation(0.0f, 90.0f, 250f, 273f);
        an.setFillAfter(true);
        pb.startAnimation(an);
    }
}

And finally the drawable style file(not sure if it might help but yeah):最后是可绘制样式文件(不确定它是否有帮助,但是是的):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0"
            android:useLevel="true">
            <gradient
                android:startColor="#fb0000"
                android:endColor="#00FF00"
                android:centerColor="#fbf400"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

Rather than using animation to get a countdown timer, you can programmatically set the countdown time length as maximum progress of ProgressBar , and use a Handler object scheduled to run every second, to update the current progress of the ProgressBar as shown below:与其使用 animation 获取倒数计时器,不如以编程方式将倒数时间长度设置为ProgressBar的最大进度,并使用调度程序Handler每秒运行一次,以更新ProgressBar的当前进度,如下所示:

        final ProgressBar pb = findViewById(R.id.progressBarToday);

        // for eg: if countdown is to go for 30 seconds
        pb.setMax(30);

        // the progress in our progressbar decreases with the decrement 
        // in the remaining time for countdown to be over
        pb.setProgress(30);

        // keep track of current progress
        final int[] currentProgress = {30};
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                currentProgress[0] -= 1;
                pb.setProgress(currentProgress[0]);
                if(currentProgress[0] != 0){
                    new Handler().postDelayed(this, 1000);
                }else
                    Toast.makeText(requireContext(), "Count Down is OVER!!", Toast.LENGTH_LONG).show();
            }
        }, 1000);

I found the solution:我找到了解决方案:

Xml: Xml:

<ProgressBar
    android:id="@+id/progressbar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Java code: Java代码:

public class thetest extends AppCompatActivity {
ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_thetest);
    mProgressBar=findViewById(R.id.progressbar);
    mProgressBar.setProgress(i);
    mCountDownTimer=new CountDownTimer(30000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
            i++;
            mProgressBar.setProgress((int)i*100/(30000/1000));

        }

        @Override
        public void onFinish() {
            //Do what you want
            i++;
            mProgressBar.setProgress(100);
        }
    };
    mCountDownTimer.start();
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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