前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android使用动画设置ProgressBar进度的方法

Android使用动画设置ProgressBar进度的方法

作者头像
砸漏
发布2020-11-01 15:15:28
1.9K0
发布2020-11-01 15:15:28
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

需求场景:当我们在使用ProgressBar的时候,希望有进度加载的效果,此时我们传统的做法是使用Thread线程来实现,下面我们用属性动画来实现,简单粗暴。。哈哈哈

布局文件:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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);
 }
}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档