<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 依次定义7个TextView,先定义的TextView位于底层
后定义的TextView位于上层-->
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210dp"
android:height="50dp"
android:background="#ff0000"
/>
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:height="50dp"
android:background="#dd0000"
/>
<TextView
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:height="50dp"
android:background="#bb0000"
/>
<TextView
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120dp"
android:height="50dp"
android:background="#990000"
/>
<TextView
android:id="@+id/view5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="90dp"
android:height="50dp"
android:background="#770000"
/>
<TextView
android:id="@+id/view6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:height="50dp"
android:background="#550000"
/>
<TextView
android:id="@+id/view7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="30dp"
android:height="50dp"
android:background="#330000"
/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="color1">#FF0000</color>
<color name="color2">#dd0000</color>
<color name="color3">#bb0000</color>
<color name="color4">#990000</color>
<color name="color5">#770000</color>
<color name="color6">#550000</color>
<color name="color7">#330000</color>
</resources>
package com.example.shangqiuju.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by shangqiuju on 2021/7/30.
*/
public class FrameLayoutColor extends AppCompatActivity {
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.view2,
R.id.view3,
R.id.view4,
R.id.view5,
R.id.view6,
R.id.view7
};
TextView[] views = new TextView[7];
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_layout);
for(int i = 0;i < 7; i++){
views[i] = (TextView)findViewById(names[i]);
}
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息来自本程序所发送
if(msg.what == 0x1122){
//依次改变7个TextView 的背景色
for(int i = 0 ;i < 7 - currentColor;i++){
views[i].setBackgroundResource(colors[i + currentColor]);
}
for(int i = 7 - currentColor,j = 0 ; i < 7; i++,j++){
views[i].setBackgroundResource(colors[i]);
}
}
super.handleMessage(msg);
}
};
//定义一个线程周期性的改变currentColor变量值
new Timer().schedule(new TimerTask() {
@Override
public void run() {
currentColor++;
if(currentColor >= 6){
currentColor = 0;
}
//发送一条消息通知系统改变7个TextView组件的背景色
Message m = new Message();
//给该消息定义一个标识
m.what = 0x1122;
handler.sendMessage(m);
}
},0,100);
}
}