Android霓虹灯效果
页面布局文件:
activity_main.xml
<FrameLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_gravity="center" />
<TextView
android:id="@+id/tv2"
android:layout_width="240dip"
android:layout_height="240dip"
android:layout_gravity="center" />
<TextView
android:id="@+id/tv3"
android:layout_width="180dip"
android:layout_height="180dip"
android:layout_gravity="center" />
<TextView
android:id="@+id/tv4"
android:layout_width="120dip"
android:layout_height="120dip"
android:layout_gravity="center" />
<TextView
android:id="@+id/tv5"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_gravity="center" />
</FrameLayout>
主要业务操作代码:
MainActivity.java
public class MainActivity extends Activity {
private int[] colors = new int[] {0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFF00FF,
0xFF00FFFF};
private int[] nextColorsPointers = new int[] {1, 2, 3, 4, 0};
private View[] views;
private int currentColorPointer = 0;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
views = new View[] {findViewById(R.id.tv5), findViewById(R.id.tv4),
findViewById(R.id.tv3), findViewById(R.id.tv2),findViewById(R.id.tv1)};
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
int nextColorPointer = currentColorPointer;
for(int i = views.length - 1; i >= 0; i --) {
views[i].setBackgroundColor(colors[nextColorsPointers[nextColorPointer]]);
nextColorPointer = nextColorsPointers[nextColorPointer];
}
currentColorPointer ++;
if(currentColorPointer == 5) {
currentColorPointer = 0;
}
handler.postDelayed(this, 300);
}
};
handler.postDelayed(runnable, 300);
}
}