一个简单的Loading过程


一个简单的Loading过程
实现起来还是比较简单的.看下面的代码.
package com.ql.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class App extends Activity {
	private LinearLayout layout;
	private Handler handler;
	private int number=10;
	private ImageView[] imageViews=new ImageView[number];
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler=new Handler(){

			@Override
			public void handleMessage(Message msg) {
				//效率比较低
//				for(int i=0;i<number;i++){
//					imageViews[i].setBackgroundResource(i==msg.what?R.drawable.progress_go_small:R.drawable.progress_bg_small);
//				}
				//这样效率高
				imageViews[msg.what].setBackgroundResource(R.drawable.progress_go_small);
				if(msg.what==0){
					msg.what=number;
				}
				imageViews[msg.what-1].setBackgroundResource(R.drawable.progress_bg_small);
				
			}
		};
        
		initViews();
        
        playAnimation();
    }
    
    private void initViews(){
    	 layout=(LinearLayout)findViewById(R.id.layout);
         
         LinearLayout container=new LinearLayout(this);
         container.setOrientation(LinearLayout.HORIZONTAL);
         container.setGravity(Gravity.CENTER);
         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
         lp.gravity=Gravity.CENTER;
         
         LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
         
         for(int i=0;i<number;i++){
         	imageViews[i]=new ImageView(this);
         	imageViews[i].setBackgroundResource(i==0?R.drawable.progress_go_small:R.drawable.progress_bg_small);
         	container.addView(imageViews[i], params);
         }
         
         layout.addView(container,lp);
    }
    
  //不断发送消息,切换图片
	private void playAnimation() {
		new Thread() {
			@Override
			public void run() {
				while (true) {
					for (int i = 0; i < number; i++) {
						handler.sendEmptyMessage(i);
						try {
							this.sleep(300);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}

			}

		}.start();
	}
   
    		
}


一个比较笨的实现:
http://gundumw100.iteye.com/admin/blogs/1052266

你可能感兴趣的:(thread,android,OS,ITeye,Go)