Android 自动切换颜色
- 形式:单独完成
题目:切换颜色
要求:按照讲课笔记实现颜色的切换。
1、创建安卓应用
- 基于Empty Activity创建安卓应用 - SwitchColor
- 单击【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、字符串资源文件
<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、主界面实现功能
- 定义变量
- 通过资源标识符获取控件实例
- 编写切换颜色单击事件处理方法
- 完整代码
package net.cgh.switchcolor;
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);
}
/**切换颜色单击事件处理方法....*/
public void doSwitchColor(View view){
// 累加按钮单击次数
clickCount++;
//只有三种颜色切换,因此单击次数对3求余
clickCount = clickCount % 3;
// 根据单击次数确定颜色方案{底层,中层,顶层}
switch (clickCount){
case 0: // 颜色方案:【红,绿,蓝】
colors = new int[] {Color.RED, Color.GREEN,Color.BLUE};
break;
case 1: // 颜色方案:【绿,蓝,红】
colors = new int[] {Color.GREEN, Color.BLUE,Color.RED};
break;
case 2: // 颜色方案:【蓝,红,绿】
colors = new int[] {Color.BLUE, Color.RED,Color.GREEN};
break;
}
}
}
5、启动应用,查看效果
6、优化切换颜色算法
- 采用左移算法切换颜色
- 启动查看效果
- 当切换颜色比较多,这个算法还得优化,采用循环结构来切换颜色
- 重启查看效果