当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发使用ProgressBar实现进度条功能示例

Android开发使用ProgressBar实现进度条功能示例

2019年07月23日  | 移动技术网IT编程  | 我要评论

阿忠师,东南卫视 梦雪,美妞论坛网

本文实例讲述了android开发使用progressbar实现进度条功能。分享给大家供大家参考,具体如下:

进度条progressbar的使用主要有两种方向;

1.使用官方默认样式

2.使用自定义样式

先看效果:

详细代码实现文末给出

关于系统自带样式:

style="@android:style 中有许多系统自带样式,大家可以更具自身喜好选择。

如果不选择 style 系统会默认使用上图中红色的样式。

关于自定义样式:

这里我们最好看看源码 很容易理解

主要分为三个部分:当前进度、缓冲进度、以及背景 三个属性

这里我们通过在drawable里新建my_bar.xml来实现

这里有个注意点 很多人写了xml后发现 直接就显示满进度 而不是缓慢增长

由于是替换系统自带样式,所以id必须与系统保持一致:(如:android:id="@android:id/background"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!--定义轨道背景-->
  <item android:id="@android:id/background"
    android:drawable="@drawable/no"/>
  <!--定义轨道上已完成部分的样式-->
  <item android:id="@android:id/progress"
    android:drawable="@drawable/ok"/>
</layer-list>

这里对比下系统源码就很好理解了:

这里的模拟方法采用的是线程结合handler

由于线程不能直接改变控件属性 所以需要用handler来接受线程发出的message

具体方法如下:

public class mainactivity extends activity {
  //记录progressbar的完成进度
  private int sum1=0,sum2 = 0 ;
  progressbar bar1,bar2;
  //创建一个负责更新进度的handler
  handler mhandler = new handler(){
    @override
    public void handlemessage(message msg) {
      //表明消息是本程序发送的
      if (msg.what == 0x111){
        bar1.setprogress(sum1);
        bar2.setprogress(sum2);
      }
    }
  };
  //模拟耗时
  thread thread = new thread(){
    @override
    public void run() {
      while (sum2 < 100){
        //bar1获取完成工作的百分比
        if (sum1 > 100){
          sum1 = 100;
          if (sum2<100){
            sum2 += (int) (math.random()*25);
          }else {
            sum2 = 100;
            thread.stop();
          }
          sum1=0;
        }else {
          sum1 = sum1 + (int) (math.random()*25);
        }
        try{
          thread.sleep(1000);
        }catch (interruptedexception e){
          e.printstacktrace();
        }
        //更新progressbar
        mhandler.sendemptymessage(0x111);
      }
    }
  };
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    bar1 = (progressbar) findviewbyid(r.id.bar);
    bar2 = (progressbar) findviewbyid(r.id.bar2);
    thread.start();
  }
}

最后在给出布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<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"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical">
  <android.support.v7.widget.toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentinsetstart="0dp"
    android:background="#9fb6cd">
    <relativelayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <progressbar
        android:id="@+id/toolbar_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignparentend="true"
        android:layout_alignparentright="true" />
    </relativelayout>
  </android.support.v7.widget.toolbar>
  <linearlayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--定义一个大环型进度条-->
    <progressbar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@android:style/widget.progressbar.large"/>
    <!--定义一个中等大小环形进度条-->
    <progressbar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <!--定义一个小进度条-->
    <progressbar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@android:style/widget.progressbar.small"/>
  </linearlayout>
  <textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="任务完成的进度"/>
  <!--定义一个大水平进度条-->
  <progressbar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    style="@android:style/widget.progressbar.horizontal"/>
  <!--顶一个水平进度条,并改变轨道外观-->
  <progressbar
    android:id="@+id/bar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progressdrawable="@drawable/my_bar"
    style="@android:style/widget.progressbar.horizontal"/>
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网