Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 FrameLayout 做出霓虹燈效果:六層色塊與定時輪播

編輯:關於 Android 開發

FrameLayout 的強項:把控件疊起來

FrameLayout(幀布局)最大的特色就是讓子控件彼此重疊堆疊,後加入的自然蓋在前一個上面。利用這個特性,再搭配幾個 TextView 當色塊,就能做出一個很樸素但有趣的霓虹燈效果。

一、XML 布局:六層由大到小堆疊

六個 TextView 都用 layout_gravity="right" 靠右對齊,尺寸依序遞減,顏色各異,於是形成一層層同心方塊。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/textView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="160pt" android:height="160pt"
        android:background="#f00" />
    <TextView android:id="@+id/textView2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="140pt" android:height="140pt"
        android:background="#88ff00" />
    <TextView android:id="@+id/textView3"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="120pt" android:height="120pt"
        android:background="#ffffff" />
    <TextView android:id="@+id/textView4"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="100pt" android:height="100pt"
        android:background="#3be407" />
    <TextView android:id="@+id/textView5"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="80pt" android:height="80pt"
        android:background="#ffff88" />
    <TextView android:id="@+id/textView6"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" android:width="60pt" android:height="60pt"
        android:background="#8329c7" />
</FrameLayout>

二、把顏色抽到 colors.xml

顏色寫死在布局裡不好維護,抽到資源檔再用 setBackgroundResource 切換,程式碼會乾淨很多。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#f00</color>
    <color name="color2">#88ff00</color>
    <color name="color3">#ffffff</color>
    <color name="color4">#3be407</color>
    <color name="color5">#ffff88</color>
    <color name="color6">#8329c7</color>
</resources>

三、用 Handler + Timer 讓顏色跑起來

靜態的色塊只是圖案,要變成霓虹燈,得讓顏色輪流跑。做法是每 200 毫秒送出一則空訊息,Handler 收到後把六層顏色依序往下一格遞移。

package happy.framelayout;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private int currentColor = 0;   // 目前起始色偏移量

    final int[] colors = new int[] {
        R.color.color1, R.color.color2, R.color.color3,
        R.color.color4, R.color.color5, R.color.color6,
    };

    final int[] names = new int[] {
        R.id.textView, R.id.textView2, R.id.textView3,
        R.id.textView4, R.id.textView5, R.id.textView6,
    };

    TextView[] views = new TextView[names.length];

    // Handler 負責接收子線程送來的消息,再回到主線程更新 UI
    android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x234) {
                // 每收到一則消息,就把六層顏色整體往下一格偏移
                for (int i = 0; i < names.length; i++) {
                    views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);
                }
                currentColor++;
            }
            super.handleMessage(msg);   // 交給父類,避免訊息遺失
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        for (int i = 0; i < names.length; i++) {
            views[i] = (TextView) findViewById(names[i]);   // 設定初始顏色
        }

        // 週期性發送空消息,驅動顏色變化
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x234);
            }
        }, 0, 200);
    }
}

這裡的設計重點是「計時在子線程、更新 UI 在主子線程」。Timer 只負責按時送訊息,真正動到 TextView 的 setBackgroundResource 一律在 Handler 裡執行,這樣才不會踩到 Android 的單執行緒 UI 規則。

四、運行結果

如果仔細看會發現一個可以再優化的點:Activity 銷毀時應該 timer.cancel() 並移除 Handler 的待處理訊息,否則畫面離開後計時器還在跑,容易造成背景耗電或記憶體洩漏。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved