在帧布局中放上若干TextView,利用handler,可以实现霓虹灯效果。
页面布局文件如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="210px"
android:width="50px"
android:background="#ff0000"
/>
<TextView
android:id="@+id/View02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="180px"
android:width="50px"
android:background="#dd0000"
/>
<TextView
android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="150px"
android:width="50px"
android:background="#bb0000"
/>
<TextView
android:id="@+id/View04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="120px"
android:width="50px"
android:background="#990000"
/>
<TextView
android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="90px"
android:width="50px"
android:background="#770000"
/>
FrameLayout>
xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#ff0000color>
<color name="color2">#dd0000color>
<color name="color3">#bb0000color>
<color name="color4">#990000color>
<color name="color5">#770000color>
resources>
mainactivity.java如下:
public class MainActivity extends Activity {
private int currentColor = 0;
//定义一个颜色数组
final int[] colors = new int[]
{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
};
final int[] names = new int[]
{
R.id.View01,
R.id.View02,
R.id.View03,
R.id.View04,
R.id.View05
};
TextView[] views = new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0 ; i < 5 ; i++)
{
views[i] = (TextView)findViewById(names[i]);
}
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
//表明消息来自本程序锁发送
if(msg.what == 0x1122)
{
//依次改变5个TextView的背景色
for(int i = 0 ; i < 5 - currentColor ; i++)
{
views[i].setBackgroundResource(colors[i+currentColor]);
}
for(int i = 5 - currentColor , j = 0 ; i < 5 ; i++ , j++)
{
views[i].setBackgroundResource(colors[j]);
}
}
super.handleMessage(msg);
}
};
//定义一个线程周期性地改变currentColor变量值
new Timer().schedule(new TimerTask()
{
public void run()
{
currentColor++;
if(currentColor >= 4)
{
currentColor = 0;
}
//发送一条消息通知系统改变5个TextView组件的背景色
Message m = new Message();
m.what = 0x1122;
handler.sendMessage(m);
}
} , 0 , 100);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
享受霓虹灯效果吧!