布局代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="170pt"
android:height="170pt"
android:background="#f00"/>
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="140pt"
android:height="140pt"
android:background="#0f0"/>
<TextView
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="110pt"
android:height="110pt"
android:background="#00f"/>
<TextView
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="80pt"
android:height="80pt"
android:background="#ff0"/>
<TextView
android:id="@+id/view5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="50pt"
android:height="50pt"
android:background="#f0f"/>
<TextView
android:id="@+id/view6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="20pt"
android:height="20pt"
android:background="#0ff"/>
</FrameLayout>
逻辑代码:
package com.example.lab.android.nuc.crizyandroid;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class LightActivity extends AppCompatActivity {
private int currentColor = 0;
//定义一个颜色数组
final int [] colors = new int[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6
};
final int[] names = new int[]{
R.id.view1,
R.id.view2,
R.id.view3,
R.id.view4,
R.id.view5,
R.id.view6
};
TextView[] views = new TextView[names.length];
@SuppressLint("HandlerLeak")
Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123){
for (int i = 0; i < names.length; i++) {
views[i].setBackgroundResource(colors[(i + currentColor) % names.length] );
}
currentColor ++;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
for (int i = 0; i < names.length; i++) {
views[i] = (TextView) findViewById(names[i]);
}
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
},0,50);
}
}
效果如下:
11.gif
时间比较紧,先写简洁一些吧
网友评论