Android超简单的进度条源码

相信大家都知道,打开程序的时候,需要通过进度条或者图片显示。这个时候很方便给应用程序优质的效果。下面我们来看看是如何实现进度条的代码,原理非常简单。
如图:


下面我们来看一下代码的实现。
package com.smart;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class Main extends Activity {

    private ProgressBar pro1;
   
    private ProgressBar pro2;
    private Handler handler=new Handler();
    private int count1=0;
    private int count2=0;//进度条的从0开始
   
    private Runnable doUpdateProgressBar1=new Runnable() {
       
        @Override
        public void run() {
            for ( count1 = 0; count1 < pro1.getMax(); count1++) {
                handler.post(new Runnable() {
                    //增加进度
                    @Override
                    public void run() {
                        pro1.setProgress(count1);
                    }
                });
            }
        }
    };
   
   
private Runnable doUpdateProgressBar2=new Runnable() {
       
        @Override
        public void run() {
            for ( count2 = 0; count2 < pro2.getMax(); count2++) {
                handler.post(new Runnable() {
                   
                    @Override
                    public void run() {
                        pro2.setProgress(count2);
                    }
                });
            }
        }
    };
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pro1=(ProgressBar)findViewById(R.id.progressBar1);//得到组件
        pro2=(ProgressBar)findViewById(R.id.progressBar2);
        Thread thread1=new Thread(doUpdateProgressBar1,"thread1");
        thread1.start();//开始
        Thread thread2=new Thread(doUpdateProgressBar2,"thread2");
        thread2.start();
   
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
   android:max="20000"
    />
       <ProgressBar
    android:id="@+id/progressBar2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="20000"
    android:layout_marginTop="10dp"
    />
   
   
   
   

</LinearLayout>

posted on 2011-03-17 14:18  llb988  阅读(1253)  评论(0编辑  收藏  举报

导航