牛骨文教育服务平台(让学习变的简单)

ProgressBar本身进度条组件,它派生了:SeekBar和RatingBar两个组件,他们的继承关系如下:

1、ProgressBar有两个进度,一个是android:progress,另一个是android:secondaryProgress。后者主要是为缓存需要所涉及的,比如在看网络视频时候都会有一个缓存的进度条以及还要一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。

2、ProgressBar分为确定的和不确定的,确定的是我们能明确看到进度,相反不确定的就是不清楚、不确定一个操作需要多长时间来完成,这个时候就需要用的不确定的ProgressBar了。属性android:indeterminate如果设置为true的话,那么ProgressBar就可能是圆形的滚动条或者水平的滚动条(由样式决定),但是我们一般时候,是直接使用Style类型来区分圆形还是水平ProgressBar的。

进度条的常用xml属性如下:

其中根据Style属性的设置不同,可以指定ProgressBar的3种环形进度条和1中水平进度条:

1,大环形进度条  style="?android:attr/progressBarStyleLarge或者style="?android:attr/progressBarStyleLargeInverse"

2,普通环形进度条:style="android:progressBarStyle"

3,小环形进度条:style="?android:attr/progressBarStyleSmall"

4,普通水平进度条:style=“?android:attr/progressBarStyleHorizontal”

常用属性解释:

android:max:设置进度条的最大值

android:progress:设置已完成进度条的进度值

android:progressDrawable:设置进度条轨道对应的Drawable对象。可以是一个LayerDrawable对象

android:indeterminate:设置属性是true,设置进度条不精确显示的进度,就是环形进度条的进度值

android:indeterminateDrawable:设置绘制不精确显示的精度的Drawable对象,就是环形进度的轨道Drawable对象;

android:indeterminateDuration;设置不精确显示精度的持续时间

mybar.xml

<?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>
    <!-- 设置轨道上已完成背景的样式 ,在上面-->
     <item android:id="@android:id/progress" 
          android:drawable="@drawable/ok"></item>   
</layer-list>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
      <!-- 大环形进度条  style="?android:attr/progressBarStyleLarge"-->

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLargeInverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

     <!-- 普通的中等进度条 -->

        <ProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

     <!-- 小环形进度条  style="?android:attr/progressBarStyleSmall"-->
        <ProgressBar
            android:id="@+id/ProgressBar3"
             style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="任务完成的进度"
        android:textSize="20dp" />
  <!-- 普通的水平进度条 -->

    <ProgressBar
        android:id="@+id/progressBar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="0"
        android:visibility="visible" />

   <!-- 水平进度条,改变轨道外观 ,外观图片是在drawable里面,xml文件的layer-list元素编写-->
    <ProgressBar
        android:id="@+id/progressBar5"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:paddingTop="20dp"
        android:progressDrawable="@drawable/mybar" />

</LinearLayout>

MainActivity.java

package com.hust.progresbartest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
    
	   /*给程序模拟填充长度为100的数组*/
   private int[] data=new int[100];
   int hasData=0;
   //记录ProgressBar的完成进度
   int status=0;
   ProgressBar bar,bar2;
   
   //创建一个负责更新进度的Handler
   Handler h=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.activity_main);
       
        bar=(ProgressBar) findViewById(R.id.progressBar4);
        bar2=(ProgressBar) findViewById(R.id.progressBar5);
        //启动线程来执行任务
        new Thread(){
        	public void run(){
        		while(status<100){
        			//获取耗时操作完成百分比
        			status=dowork();
        			//发送消息
        			h.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.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}