线程初体验之跑马灯

来源:互联网 发布:关键词搜索软件 编辑:程序博客网 时间:2024/06/05 20:15

今天,在学习线程时看到了一个运用线程的实例,代码写的很好,有点出乎我的意料,第一次在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;        //当前颜色值@Overrideprotected 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() {@Overridepublic 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 blocke.printStackTrace();}    }}}); t.start();           //开启线程handler = new Handler(){@Overridepublic 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>



0 0
原创粉丝点击