Android 帧布局FrameLayout之霓虹灯效果

一万年来谁著史,三千里外欲封侯。这篇文章主要讲述Android 帧布局FrameLayout之霓虹灯效果相关的知识,希望能为你提供帮助。
【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" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5> 6< TextView 7android:id = "@+id/vieww01" 8android:layout_width= "wrap_content" 9android:layout_height= "wrap_content" 10android:layout_gravity = "center" 11android:width="200pt" 12android:height="200pt" 13android:background="#f00" /> 14< TextView 15android:id="@+id/vieww02" 16android:layout_width= "wrap_content" 17android:layout_height= "wrap_content" 18android:layout_gravity = "center" 19android:width = "160pt" 20android:height = "160pt" 21android:background="#0f0"/> 22< TextView 23android:id = "@+id/vieww03" 24android:layout_width = "wrap_content" 25android:layout_height = "wrap_content" 26android:layout_gravity = "center" 27android:width="110pt" 28android:height="110pt" 29android:background="#00f" /> 30< TextView 31android:id = "@+id/vieww04" 32android:layout_width="wrap_content" 33android:layout_height="wrap_content" 34android:layout_gravity = "center" 35android:width="80pt" 36android:height="80pt" 37android:background="#ff0" 38/> 39< TextView 40android:id = "@+id/vieww05" 41android:layout_width="wrap_content" 42android:layout_height="wrap_content" 43android:layout_gravity = "center" 44android:width="50pt" 45android:height="50pt" 46android:background="#f0f" 47/> 48< TextView 49android:id = "@+id/vieww06" 50android:layout_width="wrap_content" 51android:layout_height="wrap_content" 52android:layout_gravity = "center" 53android:width="20pt" 54android:height="20pt" 55android:background="#0ff" 56/> 57 58 < /FrameLayout>

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

 

    推荐阅读