您的位置:首页 > 移动开发 > Android开发

Android:霓虹灯

2016-01-19 15:03 369 查看
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll"
    android:orientation="vertical" >

</LinearLayout>


在res/values目录下,创建一个保存颜色资源的color.xml文件

<?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>


Main_Activity.java

package com.example.clolor;

import java.util.Random;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
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 数组
	public static String str="小白,天冷多加衣。BY:苏苏";//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.ll);//获取线性布局管理器
		int height=this.getResources().getDisplayMetrics().heightPixels;//获取屏幕的高度
		for(int i=0;i<tv.length;i++){
			tv[i]=new TextView(this);//创建一个文本对象
			tv[i].setGravity(Gravity.CENTER);//设置文字的显示位置
			tv[i].setText(String.valueOf(str.charAt(i)));//设置文本框上的文字
			tv[i].setWidth(this.getResources().getDisplayMetrics().widthPixels);//设置文本框的高度
			tv[i].setHeight(height/tv.length);//设置文本框的高度
			linearLayout.addView(tv[i]);//添加到布局管理器
		}
		Thread t=new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(!Thread.currentThread().isInterrupted()){
					Message m=handler.obtainMessage();//获取一个Message
					m.what=0x101;//设置消息标识
					handler.sendMessage(m);//发送消息
					try{
						Thread.sleep(new Random().nextInt(1000));//休眠1秒钟
					}catch(InterruptedException e){
						e.printStackTrace();
					}
				}
			}
			
		});
		t.start();//开启线程
		handler =new Handler(){
			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);
			}
		};
	}

}


在AndroidMainFest.xml文件的<activity>标识中,设置android:theme属性,实现全屏显示

android:theme="@android:style/Theme.Black.NoTitleBar"

这是运行结果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: