标签:ack etc his neon size context public 文本 一闪一闪
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lixc.neonlight.MainActivity"
android:orientation="vertical">
</LinearLayout>
2、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>
3、在MainActivity中声明成员变量
private Handler handler;
private static LinearLayout linearLayout;
public static TextView[] textViews = new TextView[14];
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;
4、在MainActivity中的onCreate()方法中,添加文本框组件到线性布局管理器中
linearLayout = (LinearLayout)findViewById(R.id.mainll);
int height = this.getResources().getDisplayMetrics().heightPixels;
for (int i=0;i<textViews.length;i++){
textViews[i] = new TextView(this);
textViews[i].setWidth(this.getResources().getDisplayMetrics().widthPixels);
textViews[i].setHeight(height/textViews.length);
linearLayout.addView(textViews[i]);
}
5、创建新线程,重写run()方法
new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()){
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
try {
Thread.sleep(new Random().nextInt(1000));
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
6、创建一个Handler对象,重写handleMessage()方法
handler = new Handler(){
@Override
public void handleMessage(Message msg){
int temp = 0;
switch (msg.what){
case 1:
for (int i=0;i<textViews.length;i++){
temp = new Random().nextInt(bgColor.length);
if (index == temp){
temp++;
if (temp == bgColor.length){
temp = 0;
}
}
index = temp;
textViews[i].setBackgroundColor(getResources().getColor(bgColor[index]));
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
7、实现效果
这里只是一张图片,实际上是一闪一闪的效果。
标签:ack etc his neon size context public 文本 一闪一闪
原文地址:http://www.cnblogs.com/ccdd1314/p/6208480.html