特性實現一個簡單的霓虹燈效果。
Activity代碼
package com.app.test01;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;public class FrameLayoutActivity extends Activity implements Runnable{//定義5個顏色值private int[] colors = new int[]{0xFFFF0000,0xFF0000FF,0xFF00FFFF,0xFFFF00FF,0xFF00FF00};//每一個顏色值的索引private int[] nextColorPoints = new int[]{1,2,3,4,0};//當前值的索引private int currentColorPoint = 0;private View[] views;private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_frame);views = new View[]{findViewById(R.id.textView5),findViewById(R.id.textView4),findViewById(R.id.textView3),findViewById(R.id.textView2),findViewById(R.id.textView1)};handler = new Handler();handler.postDelayed(this, 300);}@Overridepublic void run() {// TODO Auto-generated method stubint nextColorPoint = currentColorPoint;for (int i = views.length-1; i>=0; i--) {views[i].setBackgroundColor(colors[nextColorPoint]);nextColorPoint = nextColorPoints[nextColorPoint];}currentColorPoint++;if (currentColorPoint == 5) {currentColorPoint = 0;}handler.postDelayed(this, 300);}}
布局檔案
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="300dp" android:layout_height="300dp" android:text="TextView" android:layout_gravity="center"/> <TextView android:id="@+id/textView2" android:layout_width="240dp" android:layout_height="240dp" android:text="TextView" android:layout_gravity="center"/> <TextView android:id="@+id/textView3" android:layout_width="180dp" android:layout_height="180dp" android:text="TextView" android:layout_gravity="center"/> <TextView android:id="@+id/textView4" android:layout_width="120dp" android:layout_height="120dp" android:text="TextView" android:layout_gravity="center"/> <TextView android:id="@+id/textView5" android:layout_width="60dp" android:layout_height="60dp" android:text="TextView" android:layout_gravity="center"/> </FrameLayout>