溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中設(shè)置ProgressBar進度

發(fā)布時間:2021-03-30 15:25:53 來源:億速云 閱讀:194 作者:Leah 欄目:移動開發(fā)

怎么在Android中設(shè)置ProgressBar進度?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 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:orientation="vertical"
 tools:context="com.yhx.app.progressbardemo.MainActivity">

 <ProgressBar
  android:layout_marginTop="20dp"
  android:id="@+id/bar"
  android:layout_width="match_parent"
  android:layout_height="8dp"
  
  android:progress="10"
  android:max="100">
 </ProgressBar>

 <Button
  android:hint="開始"
  android:onClick="show"
  android:layout_marginTop="50dp"
  android:layout_width="match_parent"
  android:layout_height="50dp"/>

 <TextView
  android:id="@+id/tv_bar"
  android:layout_marginTop="20dp"
  android:layout_width="100dp"
  android:text="0"
  android:gravity="center"
  android:textSize="38dp"
  android:layout_gravity="center"
  android:layout_height="100dp"/>

 <Button
  android:hint="開始"
  android:onClick="tvshow"
  android:layout_marginTop="50dp"
  android:layout_width="match_parent"
  android:layout_height="50dp"/>

</LinearLayout>

Activity:

public class MainActivity extends AppCompatActivity {

 private ProgressBar mProgressBar;
 TextView mtv_bar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mProgressBar = (ProgressBar) findViewById(R.id.bar);
  mtv_bar = (TextView) findViewById(R.id.tv_bar);
  setAnimation(mProgressBar, 100);
 }

 private void setAnimation(final ProgressBar view, final int mProgressBar) {
  ValueAnimator animator = ValueAnimator.ofInt(0, mProgressBar).setDuration(5000);

  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    view.setProgress((int) valueAnimator.getAnimatedValue());
   }
  });
  animator.start();
 }


 private void setTvAnimation(final TextView view, final int mProgressBar) {
  ValueAnimator animator = ValueAnimator.ofInt(0, mProgressBar).setDuration(5000);

  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    view.setText(String.valueOf(valueAnimator.getAnimatedValue()));
   }
  });
  animator.start();
 }

 public void show(View view) {
  setAnimation(mProgressBar, 100);

 }


 public void tvshow(View view){
  setTvAnimation(mtv_bar,240);
 }
}

關(guān)于怎么在Android中設(shè)置ProgressBar進度問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI