自动切换颜色
案例演示:切换颜色
1、创建安卓应用
- 基于Empty Activity创建安卓应用 - AutoSwitchColor
- 单击【Finish】按钮
2、主布局资源文件
主布局资源文件 - activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<TextView
android:id="@+id/tv_bottom"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#ff0000"
android:text="@string/bottom"
android:textColor="#ffff00"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_middle"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="#00ff00"
android:text="@string/middle"
android:textColor="#ffff00"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_top"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#0000ff"
android:text="@string/top"
android:textColor="#ffff00"
android:textSize="30sp" />
</FrameLayout>
<Button
android:id="@+id/btn_switch_color"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:onClick="doSwitchColor"
android:text="@string/switch_color"
android:textSize="20sp" />
</LinearLayout>
3、字符串资源文件
- 字符串资源文件 - strings.xml
<resources>
<string name="app_name">帧式布局:切换颜色</string>
<string name="top">顶层</string>
<string name="middle">中层</string>
<string name="bottom">底层</string>
<string name="switch_color">切换颜色</string>
</resources>
- 此时,查看界面预览效果
4、主界面实现功能
- 主界面类 - MainActivity
- 定义变量
- 通过资源标识符获取控件实例
5、优化切换颜色算法
- 采用循环结构切换颜色
- 查看完整代码
package dirnet.zh.auto_switch_color;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvTop;
private TextView tvMiddle;
private TextView tvBottom;
private int clickCount;//按钮单击次数
private int[] colors;//颜色数组
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
//通过资源标识符获取控件实例
tvTop = findViewById(R.id.tv_top);
tvMiddle = findViewById(R.id.tv_middle);
tvBottom = findViewById(R.id.tv_bottom);
// 初始化颜色数组
colors = new int[] {Color.RED, Color.GREEN, Color.BLUE};
}
/**
* 切换颜色单击事件处理方法
*
* @param view
*/
public void doSwitchColor(View view) {
// 通过颜色数组切换颜色 [循环]
int temp = colors[0];
for (int i = 0; i < colors.length - 1; i++) {
colors[i] = colors[i + 1];
}
colors[colors.length - 1] = temp;
// 根据颜色方案来设置三层标签背景色
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]);
}
}
6.实现自动跳转
(1)修改主布局资源文件
- 添加两个按钮
(2) 修改主界面代码
package dirnet.zh.auto_switch_color;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//声明变量
private TextView tvBottom;
private TextView tvMiddle;
private TextView tvTop;
private int clickCount;
private int[] colors;
private boolean isRunning;
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
//通过资源标识符获取控件实例
tvBottom = findViewById(R.id.tv_bottom);
tvMiddle = findViewById(R.id.tv_middle);
tvTop = findViewById(R.id.tv_top);
//切换颜色初始数组
colors = new int[]{Color.RED, Color.GREEN, Color.BLUE};
}
//单击切换颜色处理事件
public void doSwitchColor(View view) {
isRunning = true;
//创建线程,修改颜色数组,发送消息
MyThread myThread = new MyThread();
thread = new Thread(myThread);
thread.start();
}
class MyThread implements Runnable {
@Override
public void run() {
while (isRunning) {
//切换颜色
int temp = colors[0];
for (int i = 0; i < colors.length - 1; i++) {
colors[i] = colors[i + 1];
}
colors[colors.length - 1] = temp;
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]);
}
}
}
public void doStopColor(View view) {
isRunning = false;
}
}
这里是可以实现颜色发生自动跳转的,但是,如果连续点两次开始按钮程序就会出错
- 需要加上异常处理 ,设置休眠时间