举例说明如何实现使用线程来实现UC浏览器的等待页面的效果

UC浏览器等待页面中有一行不行轮流闪烁的小圆点,这些小圆点在白色和黄色之间轮流转换,这个效果如何实现呢?下面我用简单的例子来说明一下:

 

源码如下:

package com.my.loadingTest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class LoadingTest extends Activity {
    /** Called when the activity is first created. */
 private Button begin,stop;
 private int[] ids={R.id.iv01,R.id.iv02,R.id.iv03,R.id.iv04,R.id.iv05};//可供选择的ImageView
 private final int SELECTED=0;//被选择到
 private final int NOT_SELECTED=1;//没有被选择到
 private int count=5;//可以选择的图片个数

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       

     final ControlThread ct=new ControlThread();
        for(int id:ids){
         ((ImageView)findViewById(id)).setBackgroundResource(R.drawable.not_selected);         begin=(Button)findViewById(R.id.begin);
         stop=(Button)findViewById(R.id.stop);
         begin.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    ct.flag=true;
    ct.start();
   }
      
     });
     stop.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    ct.flag=false;
   }
      
     });
        }
       
    }
   
    public Handler handler=new Handler(){
     public void handleMessage(Message msg){
      switch(msg.what){
      case SELECTED:
       ((ImageView)findViewById(msg.arg1)).setBackgroundResource(R.drawable.selected);
       break;
      case NOT_SELECTED:
       ((ImageView)findViewById(msg.arg1)).setBackgroundResource(R.drawable.not_selected);
       break;
      }
     }
    };
   
    class ControlThread extends Thread{
     boolean flag=true;
     public void run(){
      Message msg;
      while(flag==true){
          for(int i=0;i<count;i++){
           msg=new Message();
           msg.what=SELECTED;
           msg.arg1=ids[i];
           handler.sendMessage(msg);
           msg=new Message();
           if(i==0){
            msg.what=NOT_SELECTED;
            msg.arg1=ids[count-1];
            handler.sendMessage(msg);
           }
           else{
            msg.what=NOT_SELECTED;
            msg.arg1=ids[i-1];
            handler.sendMessage(msg);
           }
           SystemClock.sleep(500);
          }          
         }
     }
    }
   
}

在上面的例子中我用了两个按钮来演示,一个是begin(开始演示按钮),另一个是stop(停止演示按钮),其中用到两张小圆点图片,一个是白色的小圆点,一个是黄色的小圆点,在这里图片我就不上传了,读者试验的时候可以随便找两张图片来领会其中的意思就行了。关于那两个按钮,那个begin按钮可以按一次,如果想重启这个线程让小圆点继续闪烁,当再次点击begin这个按钮时会出现这个问题:java.lang.IllegalThreadStateException: Thread already started.

原因是由于下面这段代码:

begin.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   
 ct.flag=true;
    ct.start();
   }
      
     });

 

 

 

 

下面贴出来R.layout.main

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
        android:id="@+id/begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:textSize="20dip"
        />
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        android:textSize="20dip"
        />
    </TableRow>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        >
        <ImageView
            android:id="@+id/iv01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/iv02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/iv03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/iv04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/iv05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
       
    </LinearLayout>

</LinearLayout

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值