Android开发之初级开发_实现霓虹灯的效果
//有时候需要在界面当中加入一些霓虹灯效果,使用户的体验更加优良
效果图:

布局:
<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" > <TextView android:id="@+id/tv1" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" /> <TextView android:id="@+id/tv2" android:layout_width="240dp" android:layout_height="240dp" android:layout_gravity="center" /> <TextView android:id="@+id/tv3" android:layout_width="180dp" android:layout_height="180dp" android:layout_gravity="center" /> <TextView android:id="@+id/tv4" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center" /> <TextView android:id="@+id/tv5" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center" /> </FrameLayout>
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; public class MainActivity extends Activity implements Runnable { // 五个颜色数组 private int[] colors = new int[] { 0xffff0000, 0xff00ff00, 0x00ff00ff, 0xf0f0f0f0, 0x0f0f0f0f }; // 五个索引指针 private int[] pointer = new int[] { 1, 2, 3, 4, 0 }; // 保存五个控件 private View[] views; // 当前位置 private int index = 0; private Handler mhHandler; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); // 查找五个控件并装入view数组中 views = new View[] { findViewById(R.id.tv5), findViewById(R.id.tv4), findViewById(R.id.tv3), findViewById(R.id.tv2), findViewById(R.id.tv1) }; mhHandler = new Handler(); mhHandler.postDelayed(this, 500); } public void run() { int nextIndex = index; for (int i = views.length - 1; i >= 0; i--) { // 为文本框添加颜色 views[i].setBackgroundColor(colors[pointer[nextIndex]]); nextIndex = pointer[nextIndex]; } index++; if (index == 5) { index = 0; } mhHandler.postDelayed(this, 500); } }

浙公网安备 33010602011771号