亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用動畫設置ProgressBar進度的方法

 更新時間:2018年01月24日 16:25:10   作者:還有星星  
這篇文章主要為大家詳細介紹了Android使用動畫設置ProgressBar進度的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

需求場景:當我們在使用ProgressBar的時候,希望有進度加載的效果,此時我們傳統(tǒng)的做法是使用Thread線程來實現(xiàn),下面我們用屬性動畫來實現(xiàn),簡單粗暴。。哈哈哈

布局文件:

<?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"
  style="?android:attr/progressBarStyleHorizontal"
  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);
 }
}

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論