Android幀式布局實現(xiàn)自動切換顏色
本文實例為大家分享了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);
? ? ? ? //利用布局資源設(shè)置用戶界面
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //通過資源標(biāo)識符獲取控件實例
? ? ? ? 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ù)組來設(shè)置三層標(biāo)簽的背景色
? ? ? ? ? ? ? ? ? ? tvBottom.setBackgroundColor(colors[0]);
? ? ? ? ? ? ? ? ? ? tvMiddle.setBackgroundColor(colors[1]);
? ? ? ? ? ? ? ? ? ? tvTop.setBackgroundColor(colors[2]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? }
? ? /**
? ? ?* 【開始】按鈕單擊事件處理方法
? ? ?*/
? ? public void doStart(View view) {
? ? ? ? // 設(shè)置線程運(yùn)行控制變量
? ? ? ? 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) {
? ? ? ? // 設(shè)置線程運(yùn)行控制變量
? ? ? ? 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),但是不知怎么的總是報錯,所有就使用了這個舊方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android進(jìn)階NestedScroll嵌套滑動機(jī)制實現(xiàn)吸頂效果詳解
這篇文章主要為大家介紹了Android進(jìn)階NestedScroll嵌套滑動機(jī)制實現(xiàn)吸頂效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹
這篇文章主要介紹了Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android Studio實現(xiàn)帶邊框的圓形頭像
這篇文章主要為大家詳細(xì)介紹了Android Studio實現(xiàn)帶邊框的圓形頭像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android直播系統(tǒng)平臺搭建之圖片實現(xiàn)陰影效果的方法小結(jié)
這篇文章主要介紹了Android直播系統(tǒng)平臺搭建, 圖片實現(xiàn)陰影效果的若干種方法,本文給大家?guī)砣N方法,每種方法通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

