Android 帧布局FrameLayout之霓虹灯效果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 帧布局FrameLayout之霓虹灯效果相关的知识,希望对你有一定的参考价值。

FrameLayout的常用XML属性及相关方法

XML 属性       相关属性 说明
android:foreground setForeground(Drawable) 设置该帧布局容器的前景图像
android:foregroundGravity setForegroundGravity(int) 定义绘制前景图像的gravity属性

帧布局的页面定义文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6     <TextView
 7         android:id = "@+id/vieww01"
 8         android:layout_width= "wrap_content"
 9         android:layout_height= "wrap_content"
10         android:layout_gravity = "center"
11         android:width="200pt"
12         android:height="200pt"
13         android:background="#f00" />
14     <TextView
15         android:id="@+id/vieww02"
16         android:layout_width= "wrap_content"
17         android:layout_height= "wrap_content"
18         android:layout_gravity = "center"
19         android:width = "160pt"
20         android:height = "160pt"
21         android:background="#0f0"/>
22     <TextView
23         android:id = "@+id/vieww03"
24         android:layout_width = "wrap_content"
25         android:layout_height = "wrap_content"
26         android:layout_gravity = "center"
27         android:width="110pt"
28         android:height="110pt"
29         android:background="#00f" />
30     <TextView
31         android:id = "@+id/vieww04"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:layout_gravity = "center"
35         android:width="80pt"
36         android:height="80pt"
37         android:background="#ff0"
38         />
39     <TextView
40         android:id = "@+id/vieww05"
41         android:layout_width="wrap_content"
42         android:layout_height="wrap_content"
43         android:layout_gravity = "center"
44         android:width="50pt"
45         android:height="50pt"
46         android:background="#f0f"
47         />
48     <TextView
49         android:id = "@+id/vieww06"
50         android:layout_width="wrap_content"
51         android:layout_height="wrap_content"
52         android:layout_gravity = "center"
53         android:width="20pt"
54         android:height="20pt"
55         android:background="#0ff"
56         />
57 
58 </FrameLayout>

主程序代码:

 1 package com.example.think.myapplication;
 2 
 3         import android.os.Handler;
 4         import android.os.Message;
 5         import android.os.Bundle;
 6         import android.widget.TextView;
 7         import android.app.Activity;
 8         import java.util.Timer;
 9         import java.util.TimerTask;
10 
11 public class MainActivity extends Activity {
12 
13     private int currentColor = 0;
14 //  定义一个颜色数组
15     final int[] colors = new int[]{
16             R.color.color1,
17             R.color.color2,
18             R.color.color3,
19             R.color.color4,
20             R.color.color5,
21             R.color.color6
22     };
23 
24     final int[] names = new int[]{
25             R.id.vieww01,
26             R.id.vieww02,
27             R.id.vieww03,
28             R.id.vieww04,
29             R.id.vieww05,
30             R.id.vieww06
31     };
32     TextView [] views = new TextView[names.length];
33     Handler handler = new Handler(){
34         @Override
35         public void handleMessage(Message msg){
36             //表明消息来自本程序所发送的
37             if (msg.what == 0x123){
38                 for (int i = 0;i < names.length; i++){
39                     views[i].setBackgroundResource(colors[(i+currentColor) % names.length]);
40                 }
41                 currentColor++;
42             }
43             super.handleMessage(msg);
44         }
45     };
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.activity_main);
50         for (int i = 0; i < names.length ; i++){
51             views[i] = (TextView) findViewById(names[i]);
52         }
53         //定义一个线程周期性地改变currentColor变量值
54         new Timer().schedule(new TimerTask() {
55             @Override
56             public void run() {
57                 //发送一条空消息通知系统改变6个TextView组件的背景色
58                 handler.sendEmptyMessage(0x123);
59             }
60         },0,200);
61     }
62 }

/res/values目录下新建colors.xml文件,配置如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <color name="colorPrimary">#3F51B5</color>
 4     <color name="colorPrimaryDark">#303F9F</color>
 5     <color name="colorAccent">#FF4081</color>
 6     <color name="color1">#f00</color>
 7     <color name="color2">#0f0</color>
 8     <color name="color3">#00f</color>
 9     <color name="color4">#ff0</color>
10     <color name="color5">#f0f</color>
11     <color name="color6">#0ff</color>
12 </resources>

 

以上是关于Android 帧布局FrameLayout之霓虹灯效果的主要内容,如果未能解决你的问题,请参考以下文章

Android零基础入门第30节:两分钟掌握FrameLayout帧布局

FrameLayout帧布局

Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)

Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

FrameLayout(帧布局)

Android之四大组件六大布局五大存储