Android幀式布局實現自動切換顏色
更新時間:2022年04月24日 08:21:00 作者:doubleview
這篇文章主要為大家詳細介紹了Android幀式布局實現自動切換顏色,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android幀式布局實現自動切換顏色的具體代碼,供大家參考,具體內容如下
效果:
實現:
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); ? ? ? ? //初始化顏色數組 ? ? ? ? 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; ? ? ? ? ? ? ? ? ? ? // 根據切換后的顏色數組來設置三層標簽的背景色 ? ? ? ? ? ? ? ? ? ? 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">結束</string> </resources>
原本想用Timer定時器實現,但是不知怎么的總是報錯,所有就使用了這個舊方法。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android Studio查看Android 5.x源碼的步驟詳解
Google為Android開發(fā)者帶來Android Studio,用來取代Eclipse。從Android Studio出現起,整機開發(fā)和Android源碼閱讀和編輯一定能用上它。這篇文章小編就帶大家學習下如何使用Android Studio查看Android 5.x源碼,有需要的可以參考借鑒。2016-09-09史上最全Android build.gradle配置詳解(小結)
這篇文章主要介紹了史上最全Android build.gradle配置詳解(小結),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04Android布局中margin與padding的區(qū)別及說明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01基于android中讀取assets目錄下a.txt文件并進行解析的深入分析
本篇文章是對在android需要中讀取assets目錄下a.txt文件進行了詳細的分析介紹,需要的朋友參考下2013-05-05