android布局之帧布局-----霓虹灯效果实现

时间:2024-04-05 13:29:34

首先我们要理解什么是帧布局,真不剧中每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,组件按定义的先后顺序一次逐屏显示,后面出现的会覆盖前面的画面。下面我们就用该布局实现霓虹灯的效果,如图:

android布局之帧布局-----霓虹灯效果实现


布局文件中定义七个视图组件,最大的在最下面,先定义,最小的在最上面,最后定义


<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/view1" android:width="200px" android:height="200px" android:layout_gravity="center" android:background="#ff0000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/view2" android:width="170px" android:height="170px" android:layout_gravity="center" android:background="#bb0000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/view3" android:width="140px" android:height="140px" android:layout_gravity="center" android:background="#990000"/> <TextView android:id="@+id/view4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#770000" android:height="110px" android:width="110px" /> <TextView android:id="@+id/view5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#f0ff00" android:height="50px" android:width="50px" /> <TextView android:id="@+id/view6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#550000" android:height="80px" android:width="80px" /> <TextView android:id="@+id/view7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#330000" android:height="50px" android:width="50px" /> </FrameLayout>
在values目录下,new一个存放color的xml文件:


<?xml version="1.0" encoding="utf-8"?> <resources> <color name="color1">#330000</color> <color name="color2">#550000</color> <color name="color3">#770000</color> <color name="color4">#990000</color> <color name="color5">#bb0000</color> <color name="color6">#dd0000</color> <color name="color7">#ff0000</color> </resources>
Activity中代码编写:


package cn.sword.activity; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; public class FrameLayoutTaskActivity extends Activity { boolean flag = true; private int currentColor = 0; final int[] colors = new int[] { R.color.color7,R.color.color6,R.color.color5,R.color.color4, R.color.color3, R.color.color2, R.color.color1, }; final int[] names = new int[] { R.id.view1, R.id.view2, R.id.view3, R.id.view4, R.id.view5, R.id.view6, R.id.view7 }; TextView[] views = new TextView[7]; class MyHandler extends Handler { public void handleMessage(Message msg) { show(); sleep((long) 100); } void show() { for (int i = 0; i < 7 - currentColor; i++) { views[i].setBackgroundResource(colors[i + currentColor]); } for (int i = 6 - currentColor, j = 0; i < 7; i++, j++) { views[i].setBackgroundResource(colors[j]); } currentColor++; if(currentColor>=7){ currentColor=0; } } public void sleep(Long delayMillis) { if (flag){ this.sendMessageDelayed(this.obtainMessage(0), delayMillis); } } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); for (int i = 0; i < 7; i++) { views[i] = (TextView) findViewById(names[i]); } MyHandler handler = new MyHandler(); handler.sleep((long) 100); } }