zoukankan      html  css  js  c++  java
  • 进度条(ProgressBar)的功能与用法

         进度条也是UI界面中一种非常实用的组件,通常用于向用户显示某个耗时操作完成的的百分比。进度条可以动态的显示进度,因此避免长时间的执行某个耗时的操作,让用户感觉程序失去了响应,从而更好的提高用户界面的友好性。

         Android支持几种风格的进度条,通过style属性可以为ProgressBar指定风格。该属性克支持如下几个属性值:

    • @android:style/Widget.ProgressBar.Horizontal:水平进度条。
    • @android:style/Widget.ProgressBar.Inverse:普通大小的环形进度条。
    • @android:style/Widget.RpogressBar.Large:大环形进度条。
    • @android:style/Widget.ProgressBar.Large.Inverse:大环形进度条。
    • @android:style/Wdget.ProgressBar.Small:小环形进度条。
    • @android:style/Widget.ProgressBar.Small.Inverse:小环形进度条。  

         其中android:progressDrawable用于指定进度条的轨道的绘制形式,该属性可指定为一个LayerDrawable对象(该对象可通过在XML文件中用<layer-list>元素进行配置)的引用。

         ProgressBar提供如下方法来操作进度。

    • setProgress(int):设置进度的完成百分比。
    • incrementProgressBy(int):设置进度条的进度增加或减少。当参数为正数时进度增加;当参数为负数时进度减少。

         下面的程序简单示范了进度条的用法,该程序的界面布局文件只是定义了几个简单的进度条,并指定style属性为@android:style/Widget.ProgressBar.Horizontal,即水平进度条。界面布局文件如下。

              

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
    <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="fill_parent"
            android:layout_height="wrap_content"
            android:text="任务完成的进度" />
        <!-- 定义一个水平进度条 -->
        <ProgressBar android:id="@+id/bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="100"
            style="@android:style/Widget.ProgressBar.Horizontal"  />
        <!-- 定义一个水平进度条,并改变轨道外观 -->
        <ProgressBar android:id="@+id/bar2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progressDrawable="@drawable/my_bar"
            style="@android:style/Widget.ProgressBar.Horizontal" />
    </LinearLayout>

          上面的布局文件中先定义了三个环形进度条,这种荤腥进度条无法显示进度,它只是显示一个不断旋转的图片。布局文件的后面定义的两个进度条的最大值为100,第一个进度条的样式为水平进度条;第二个进度条的外观被定义为@drawble/my_bar,因此还需要在drawable-mdpi中定义如下文件。、
        

    <?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>

        下面主程序用一个填充数组的任务模拟了耗时操作,并以进度条来标识任务的完成百分比,主程序如下。

    package org.crazyit.helloworld;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ProgressBar;
    
    public class ProgessBarTest extends Activity {
       //该程序模拟填充长度为100的数组
        private int[] data=new int[100];
        int hasData=0;
        //记录ProgressBar的完成进度
        int status=0;
        ProgressBar bar,bar2;
        //创建一个负责更新进度的Handler
        @SuppressLint("HandlerLeak")
        Handler mHandler=new Handler()
        {
        
            public void handleMessage(Message msg)
            {
                //表明消息是由该程序发送的
                if(msg.what==0x111)
                {
                    bar.setProgress(status);
                    bar2.setProgress(status);
                }
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.progess_bar_test);
            bar=(ProgressBar)findViewById(R.id.bar);
            bar2=(ProgressBar)findViewById(R.id.bar2);
            
            //启动线程来执行任务
            new Thread()
            {
                public void run()
                {
                    while(status<100)
                    {
                        //获取耗时操作的完成百分比
                        status=doWork();
                        //发送消息
                        mHandler.sendEmptyMessage(0x111);
                    }
                    
                }
            }.start();
            
        }
       //模拟一个耗时的操作
        public int doWork()
        {
            //为数组元素赋值
            data[hasData++]=(int)(Math.random()*100);
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
           return hasData;
        }
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.progess_bar_test, menu);
            return true;
        }
    
    }

        上面的程序中粗体字代码用于修改进度条的完成进度。运行上面的程序将看到如下效果:

       

  • 相关阅读:
    [轉]windows下mysql 启动 mysqlbinlog二进制日志文件
    [轉]MySQL创建、删除、重建和查看索引命令
    [轉]PHP权限控制系统PHPGACL
    [轉]mysql5存储过程语法
    Web Application Stress Tool(WAS) & SQLIOSim
    information_schema資料庫表信息
    [轉]MySQL系统变量应用探究
    [轉]httping 1.5.2 发布,HTTP连接响应测试
    [轉]批处理命令手册
    Google Native Client介紹
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3383667.html
Copyright © 2011-2022 走看看