Android UI控件之ProgressBar进度条的实现方法

发布时间:2021-06-30 14:55:28 作者:小新
来源:亿速云 阅读:156

这篇文章主要介绍Android UI控件之ProgressBar进度条的实现方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体实现首先看布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical"> 
 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="简单进度条的展示"  
    android:layout_gravity="center_horizontal"/> 
 
  <ProgressBar 
    android:id="@+id/progressBar1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:layout_gravity="center_horizontal" 
    /> 
 
  <ProgressBar 
    android:id="@+id/progressBar2" 
     
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
     android:layout_gravity="center_horizontal"/> 
 
  <ProgressBar 
    android:id="@+id/progressBar3" 
     
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal"  
    android:max="100" 
    android:minWidth="180dip" 
    android:minHeight="40dip" 
    /> 
 
</LinearLayout>

之后是MainActivity

package com.kiritor.ui_progressbar; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ProgressBar; 
 
public class MainActivity extends Activity implements Runnable { 
 
  private ProgressBar bar = null; 
  private Thread thread = null;// 声明一个线程 
  private boolean stateChange; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    bar = (ProgressBar) findViewById(R.id.progressBar3); 
    thread = new Thread(this); 
    thread.start(); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
  @Override 
  public void run() { 
    while (true) { 
      int current = bar.getProgress();// 得到当前进度值 
      int currentMax = bar.getMax();// 得到进度条的最大进度值 
      //int secCurrent = bar.getSecondaryProgress();// 得到底层当前进度值 
      // 以下代码实现进度值越来越大,越来越小的一个动态效果 
      if (stateChange == false) { 
        if (current >= currentMax) { 
          stateChange = true; 
        } else { 
          // 设置进度值 
          bar.setProgress(current + 1); 
          // 设置底层进度值 
          bar.setSecondaryProgress(current + 1); 
        } 
      } else { 
        if (current <= 0) { 
          stateChange = false; 
        } else { 
          bar.setProgress(current - 1); 
          bar.setSecondaryProgress(current - 1); 
        } 
      } 
      try { 
        Thread.sleep(50); 
      } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
 
  } 
}

以上是“Android UI控件之ProgressBar进度条的实现方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android AsyncTask详解
  2. Android学习笔记:进度条ProgressBar和线程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android ui progressbar

上一篇:PHP中怎么是僵尸进程

下一篇:PHP中怎么实现垃圾回收机制

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》