线程初体验之跑马灯

今天,在学习线程时看到了一个运用线程的实例,代码写的很好,有点出乎我的意料,第一次在activity类里面创建布局,这对于一个android新手来说很重要。

现在贴一下代码,并写了一些总结。


首先,在values中建一个名为colors的xml文件,在里面定义7个颜色

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#ffff0000</color>
    <color name="color2">#ffff6600</color>
    <color name="color3">#ffffff00</color>
    <color name="color4">#ff00ff00</color>
    <color name="color5">#ff00ffff</color>
    <color name="color6">#ff0000ff</color>
    <color name="color7">#ff6600ff</color>
</resources>
接着就写主Activity了,总体思想就是首先通过代码分配控件布局(以前从没有这样用过),把整体框架做好后,接下来就是设置颜色了。

首先通过Thread t = new Thread(new Runnable(){});方法开线程,因为跑马灯的效果颜色是一直变,不人为中断线程的话会一直跑下去。所以通过while(!Thread.currentThread().isInterrupted())实现一直循环下去,定义一个Message对象,发送出去信息,然后通过handler处理信息,也就是设置颜色,这里采用随机数的方式产生需要一个颜色数组的下标,用这种方式来为每个文本框设置颜色。

主Activity代码:

package com.example.light;

import java.util.Random;

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.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    
	private Handler handler;   //创建Handler对象
	private static LinearLayout linearLayout;   //整体布局
	public static TextView [] tv = new TextView[14];   //TextView数组
	int[] bgcolor = new int[]{R.color.color1,R.color.color2,R.color.color3,R.color.color4
		,R.color.color5,R.color.color6,R.color.color7};   //使用颜色资源
	private int index=0;        //当前颜色值
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		linearLayout=(LinearLayout) findViewById(R.id.main);  //或取线形布局
		int height= this.getResources().getDisplayMetrics().heightPixels; //获取屏幕的高度
		int width = this.getResources().getDisplayMetrics().widthPixels;   //获取屏幕宽度
		for(int i=0;i<tv.length;i++)             //创建一个文本框对象
		{
			tv[i] =new TextView(this);//创建一个文本框对象
			tv[i].setWidth(width);
			tv[i].setHeight(height/tv.length);
			linearLayout.addView(tv[i]);           //将TextView组件添加到线形布局管理器中
		}
		
         //开启一个新线程
		Thread t= new Thread(new Runnable() {
			@Override
			public void run() {
				while(!Thread.currentThread().isInterrupted()){
					Message m = new Message();
					m.what=0x101;
					handler.sendMessage(m);
					
						try {
							Thread.sleep(new Random().nextInt(1000));//休眠一秒钟
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}    
				}
			}
		});
		 t.start();           //开启线程
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
			     int temp=0;
			     if(msg.what==0x101){
			    	 for(int i=0;i<tv.length;i++){
			    		 temp = new Random().nextInt(bgcolor.length);  //产生一个随机数
			    		 //去掉重复的并相邻的颜色
			    		 if(index==temp)
			    		 {
			    			 temp++;
			    			 if(temp==bgcolor.length)
			    			 {
			    				 temp=0;
			    			 }
			    		 }
			    		 index = temp;
			    		 //为文本框设置背景
			    		 tv[i].setBackgroundColor(getResources().getColor(bgcolor[index])); 
			    	 }
			     }
				super.handleMessage(msg);
			}
			};
	}
    
}


activity_main文件:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main"
    android:orientation="vertical" >
  
</LinearLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值