android布局之帧布局-----霓虹灯效果实现

首先我们要理解什么是帧布局,真不剧中每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,组件按定义的先后顺序一次逐屏显示,后面出现的会覆盖前面的画面。下面我们就用该布局实现霓虹灯的效果,如图:



布局文件中定义七个视图组件,最大的在最下面,先定义,最小的在最上面,最后定义






    

    

    


    
    
    
    


在values目录下,new一个存放color的xml文件:




	#330000
	#550000
	#770000
	#990000
	#bb0000
	#dd0000
	#ff0000

Activity中代码编写:


package cn.sword.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class FrameLayoutTaskActivity extends Activity {

	boolean flag = true;
	private int currentColor = 0;
	final int[] colors = new int[] { R.color.color7,R.color.color6,R.color.color5,R.color.color4, R.color.color3,
			R.color.color2, R.color.color1, };

	final int[] names = new int[] { R.id.view1, R.id.view2, R.id.view3,
			R.id.view4, R.id.view5, R.id.view6, R.id.view7 };

	TextView[] views = new TextView[7];

	class MyHandler extends Handler {

		public void handleMessage(Message msg) {
			show();
			sleep((long) 100);
		}
 
		void show() {

			for (int i = 0; i < 7 - currentColor; i++) {
				views[i].setBackgroundResource(colors[i + currentColor]);
			}
			for (int i = 6 - currentColor, j = 0; i < 7; i++, j++) {
				views[i].setBackgroundResource(colors[j]);
			}
			currentColor++;
			if(currentColor>=7){
				currentColor=0;
			}
		}

		public void sleep(Long delayMillis) {
			if (flag){
				this.sendMessageDelayed(this.obtainMessage(0), delayMillis);
			}
		}
	}

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

		for (int i = 0; i < 7; i++) {
			views[i] = (TextView) findViewById(names[i]);
		}

		MyHandler handler = new MyHandler();
		handler.sleep((long) 100);
	}
}


你可能感兴趣的:(Android)