網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android幀式布局實現(xiàn)自動切換顏色的具體代碼,供大家參考,具體內(nèi)容如下
效果:
實現(xiàn):
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"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvBottom" ? ? ? ? ? ? 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/tvMiddle" ? ? ? ? ? ? android:layout_width="200dp" ? ? ? ? ? ? android:layout_height="200dp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:background="#0000ff" ? ? ? ? ? ? android:text="@string/middle" ? ? ? ? ? ? android:textColor="#ffff00" ? ? ? ? ? ? android:textSize="30sp" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvTop" ? ? ? ? ? ? android:layout_width="100dp" ? ? ? ? ? ? android:layout_height="100dp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:background="#00ff00" ? ? ? ? ? ? android:text="@string/top" ? ? ? ? ? ? android:textColor="#ffff00" ? ? ? ? ? ? android:textSize="30sp" /> ? ? </FrameLayout> ? ? <LinearLayout ? ? ? ? android:layout_marginTop="20dp" ? ? ? ? android:layout_width="300dp" ? ? ? ? android:layout_height="50dp" ? ? ? ? android:gravity="center"> ? ? ? ? <Button ? ? ? ? ? ? android:layout_width="100dp" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="@string/start" ? ? ? ? ? ? android:textSize="20sp" ? ? ? ? ? ? android:onClick="doStart" ? ? ? ? ? ? android:layout_marginRight="50dp" ? ? ? ? ? ? android:background="#04b102"/> ? ? ? ? <Button ? ? ? ? ? ? android:layout_width="100dp" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="@string/stop" ? ? ? ? ? ? android:textSize="20sp" ? ? ? ? ? ? android:onClick="doStop" ? ? ? ? ? ? android:background="#04b102"/> ? ? </LinearLayout> </LinearLayout>
ActivityMain.java
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
? ? private TextView tvBottom;
? ? private TextView tvMiddle;
? ? private TextView tvTop;
? ? private int[] colors;
? ? private Handler handler;
? ? private Thread thread;
? ? private boolean isRunning;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? //利用布局資源設置用戶界面
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //通過資源標識符獲取控件實例
? ? ? ? tvBottom = findViewById(R.id.tvBottom);
? ? ? ? tvMiddle = findViewById(R.id.tvMiddle);
? ? ? ? tvTop = findViewById(R.id.tvTop);
? ? ? ? //初始化顏色數(shù)組
? ? ? ? colors = new int[]{Color.RED, Color.BLUE, Color.GREEN};
? ? ? ? handler = new Handler() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? ? ? if (msg.what == 0x0001) {
? ? ? ? ? ? ? ? ? ? //切換顏色
? ? ? ? ? ? ? ? ? ? int temp = colors[0];
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < colors.length - 1; i++) {
? ? ? ? ? ? ? ? ? ? ? ? colors[i] = colors[i + 1];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? colors[colors.length - 1] = temp;
? ? ? ? ? ? ? ? ? ? // 根據(jù)切換后的顏色數(shù)組來設置三層標簽的背景色
? ? ? ? ? ? ? ? ? ? tvBottom.setBackgroundColor(colors[0]);
? ? ? ? ? ? ? ? ? ? tvMiddle.setBackgroundColor(colors[1]);
? ? ? ? ? ? ? ? ? ? tvTop.setBackgroundColor(colors[2]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? }
? ? /**
? ? ?* 【開始】按鈕單擊事件處理方法
? ? ?*/
? ? public void doStart(View view) {
? ? ? ? // 設置線程運行控制變量
? ? ? ? isRunning = true;
? ? ? ? // 創(chuàng)建子線程,定時發(fā)送消息
? ? ? ? thread = new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? while (isRunning) {
? ? ? ? ? ? ? ? ? ? // 向主線程發(fā)送消息
? ? ? ? ? ? ? ? ? ? handler.sendEmptyMessage(0x0001);
? ? ? ? ? ? ? ? ? ? // 讓線程睡眠500毫秒
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 啟動線程
? ? ? ? thread.start();
? ? }
? ? /**
? ? ?* 【停止】按鈕單擊事件處理方法
? ? ?*/
? ? public void doStop(View view) {
? ? ? ? // 設置線程運行控制變量
? ? ? ? isRunning = false;
? ? ? ? // 銷毀子線程
? ? ? ? thread = null;
? ? }
}
string.xml
<resources> ? ? <string name="app_name">幀式布局:顏色切換</string> ? ? <string name="bottom">底層</string> ? ? <string name="middle">中層</string> ? ? <string name="top">頂層</string> ? ? <string name="start">開始</string> ? ? <string name="stop">結(jié)束</string> </resources>
原本想用Timer定時器實現(xiàn),但是不知怎么的總是報錯,所有就使用了這個舊方法。
原文鏈接:https://blog.csdn.net/doubleview/article/details/111461931
相關(guān)推薦
- 2022-09-12 IOS開發(fā)自定義view方法規(guī)范示例_IOS
- 2022-07-12 微信小程序(條件渲染和列表渲染)
- 2022-06-26 詳解Python數(shù)據(jù)類型、進制轉(zhuǎn)換、字符串格式化的問題_python
- 2022-12-07 Rust?for循環(huán)語法糖背后的API場景分析_Rust語言
- 2023-07-03 Docker之容器導出為鏡像問題_docker
- 2022-05-11 Synchronized鎖優(yōu)化
- 2022-06-21 分享python?寫?csv?文件的兩種方法_python
- 2022-09-15 C語言編寫實現(xiàn)學生管理系統(tǒng)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支