一个简单的Loading过程

来源:互联网 发布:慕尼黑惨案 知乎 编辑:程序博客网 时间:2024/05/27 10:44


实现起来还是比较简单的.看下面的代码.
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(){@Overridepublic 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() {@Overridepublic 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 blocke.printStackTrace();}}}}}.start();}       }


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