【问题标题】:Countdown progress bar in Android StudioAndroid Studio 中的倒计时进度条
【发布时间】:2020-05-26 15:38:51
【问题描述】:

我正在尝试以进度条的形式制作一个倒数计时器,这就是我迄今为止所尝试的:但它根本不起作用。

布局 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:

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

最后是可绘制样式文件(不确定是否有帮助):

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

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    而不是使用动画来获取倒数计时器,您可以通过编程方式将倒数时间长度设置为ProgressBar 的最大进度,并使用计划每秒运行的Handler 对象来更新@ 的当前进度987654323@如下图:

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

    【讨论】:

      【解决方案2】:

      我找到了解决办法:

      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 代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多