編輯:關於 Android 開發
FrameLayout(幀布局)最大的特色就是讓子控件彼此重疊堆疊,後加入的自然蓋在前一個上面。利用這個特性,再搭配幾個 TextView 當色塊,就能做出一個很樸素但有趣的霓虹燈效果。
六個 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>
顏色寫死在布局裡不好維護,抽到資源檔再用 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>
靜態的色塊只是圖案,要變成霓虹燈,得讓顏色輪流跑。做法是每 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 的待處理訊息,否則畫面離開後計時器還在跑,容易造成背景耗電或記憶體洩漏。
Android 應用程式窗口小部件 App Widgets 實作
Android 應用程式窗口小部件 App Widgets 實作 App Widget 是可嵌入桌面等其他應用的小部件,可顯示資訊並接收周期性更新。本文說明建立 Widg
Android應用開發教程之四:TextView 文字控件詳解
TextView 是 Android 中最基礎、最常用的文字顯示控件。本文將從 API 結構出發,透過豐富的實例,帶大家全面掌握 TextView 的各項屬性與用法。 先
用 Universal Image Loader 加載圓形與圓角圖片
用 Universal Image Loader 加載圓形與圓角圖片 前言 說到 Universal Image Loader(以下簡稱 UIL),凡是有兩年安卓經驗的開
Android SharedPreferences 使用方式
Android SharedPreferences 使用方式SharedPreferences 是 Android 中用來儲存簡單鍵值資料的機制,常用於保存使用者設定、登