这个程序的主要思想就是在一个FrameLayout中定义多个TextView,分别设置不同的背景色。因为帧布局的特性,所以这些控件都是叠加起来的。然后,通过定时器循环给handler发送消息,改变控件的背景色。最后就能实现霓虹灯的效果了,本实例不怎么实用,仅仅能做一般练习而已。

布局文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
> <!-- 先定义的TextView在下面,后定义的在上面 -->
<TextView
android:id="@+id/textView01_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320dp"
android:height="320dp"
android:layout_gravity="center"
android:background="#f00"/> <TextView
android:id="@+id/textView02_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="280dp"
android:height="280dp"
android:layout_gravity="center"
android:background="#0f0"/> <TextView
android:id="@+id/textView03_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="240dp"
android:height="240dp"
android:layout_gravity="center"
android:background="#00f"/> <TextView
android:id="@+id/textView04_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200dp"
android:height="200dp"
android:layout_gravity="center"
android:background="#ff0"/> <TextView
android:id="@+id/textView05_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="160dp"
android:height="160dp"
android:layout_gravity="center"
android:background="#f0f"/> <TextView
android:id="@+id/textView06_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120dp"
android:height="120dp"
android:layout_gravity="center"
android:background="#0ff"/> </FrameLayout>

values中的

color.xml

<resources>
<color name="color01">#f00</color>
<color name="color02">#0f0</color>
<color name="color03">#00f</color>
<color name="color04">#ff0</color>
<color name="color05">#f0f</color>
<color name="color06">#0ff</color> </resources>

MainActivity.java

package com.kale.framelayout;

import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView; public class MainActivity extends ActionBarActivity { private int currentColor = 0;
final int[] colors = new int[] {
R.color.color01,
R.color.color02,
R.color.color03,
R.color.color04,
R.color.color05,
R.color.color06
};
final int[] names = new int[] {
R.id.textView01_id,
R.id.textView02_id,
R.id.textView03_id,
R.id.textView04_id,
R.id.textView05_id,
R.id.textView06_id,
};
TextView[] views = new TextView[names.length];
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息来自本程序
System.out.println(msg.what);
if(msg.what == 0x123) {
for(int i = 0 ;i<names.length;i++) {
views[i].setBackgroundResource(colors[(i+currentColor)%names.length]);
}
currentColor++;
}
super.handleMessage(msg);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < names.length; i++) {
views[i] = (TextView)findViewById(names[i]); }
//定义一个线程周期性的改变currentColor变量的值
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//发送一条空消息来通知系统改变颜色
handler.sendEmptyMessage(0x123);
System.out.println("发送了条消息");
}
}, 0,1000); } }

FrameLayout和handle实现霓虹灯效果的更多相关文章

  1. 【Android】使用FrameLayout布局实现霓虹灯效果

    FrameLayout是五大布局中最简单的一个布局. 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置. 它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 ...

  2. canvas绘制简单的霓虹灯效果

    canvas简单动画分为三个步骤: 1.清除画布区域的内容: 2.重绘: 3.执行requestAnimationFrame(); 这个霓虹灯效果的demo,我没有用requestAnimationF ...

  3. android小Demo--七彩霓虹灯效果

    七彩霓虹灯效果,基于网上的小Demo进行修改. 在android项目values文件夹下创建文件colors.xml,配置七种颜色: <?xml version="1.0" ...

  4. Vegas干货分享,如何制作霓虹灯效果

    在各色各样的展会中,各种炫彩华丽的灯光和光影一直都能吸引到人们大量的关注.同样,在视频制作中,光线的气氛渲染也是常用的方法,常用也就代表着效果明显,也是很多刚学视频剪辑小伙伴们想要学习的一种方法. 今 ...

  5. android脚步---使用framelayout实现霓虹灯效果

    轮换帧布局中7个TextView的背景颜色,会出现上面颜色渐变不断变换. 首先在main.xml文件中进行布局 总体布局为framelayout 中间有7个Textview,代表7种不同的颜色,可以看 ...

  6. IOS 作业项目(3) 霓虹灯效果

    先上效果图 #import "CHViewController.h"@interface CHViewController (){    int i;    int j;}@pro ...

  7. Android 自学之帧布局 FrameLayout

    帧布局(FrameLayout)直接继承了ViewGroup组件: 帧布局容器为每一个加入其中的组件都创建了一个空白的区域,这个区域我们称之为一帧,所有每个组件都占据一帧,这些都会根据gravity属 ...

  8. 【css】文本效果

    一.字体属性 在css字体样式中常见的字体属性有以下几种 p{ font-size: 50px; /*字体大小*/ line-height: 30px; /*行高*/ font-family: 幼圆, ...

  9. canvas多重阴影发光效果

    canvas多重阴影发光效果 前言 在一个项目中,客户提了一个发光的效果,效果图如下: 阴影 有的人可能会说,这个用阴影其实就可以实现.但是从图中可以看出,是一个比较强烈的发光效果.实际的应用过程中我 ...

随机推荐

  1. 【PAT】1103 Integer Factorization(30 分)

    The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  2. 树状数组解决LIS---O(nlogn)

    树状数组解决LIS---O(nlogn)之前写过二分查找的LIS,现在不怎么记得了,正好用Bit来搞一波.f[i]表示以a[i]结尾的LIS的长度.t[x]表示以数值x结尾的LIS的长度.即t[x]= ...

  3. tqdm:Python 进度条

    Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息.用户只需要封装任意的迭代器,是一个快速.扩展性强的进度条工具库. 用法:tqdm(iterator) 代码地址 ...

  4. 自制 COCO api 直接读取类 COCO 的标注数据的压缩文件

    第6章 COCO API 的使用 COCO 数据库是由微软发布的一个大型图像数据集,该数据集专为对象检测.分割.人体关键点检测.语义分割和字幕生成而设计.如果你要了解 COCO 数据库的一些细节,你可 ...

  5. 命令:history

    简介 shell进程会在其会话中保存此前用户执行过的命令. 历史列表(history list):当前shell所使用的历史命令存储位置. 历史文件(history file):每次登入shell,就 ...

  6. Codeforces-1077C

    title: Codeforces-1077C date: 2018-11-24 15:22:31 tags: acm 刷题 categories: Codeforces 题意 题目链接 给你一个数组 ...

  7. [同步脚本]mysql-elasticsearch同步

    公司项目搜索部分用的elasticsearch,那么这两个之间的数据同步就是一个问题. 网上找了几个包,但都有各自的缺点,最后决定还是自己写一个脚本,大致思路如下: 1.在死循环中不断的select指 ...

  8. JAVA初学练手项目,学生管理系统

    github地址:https://github.com/qscqesze/StudentManager 简单描述一下: UI层面用于接受用户的处理信息,然后移交给StudentDao去处理数据. 其中 ...

  9. OpenNI2 + NiTE2开发教程

    发现了一个非常不错的关于自然交互OpeNI2+NiTE2的资源,非常感谢Heresy,这里分享链接: OpenNI 2.x 教学文章(转载自:Heresy博客,地址:https://kheresy.w ...

  10. Slickflow.NET 开源工作流引擎高级开发(四) -- 硬核编码:代码式快速构建流程图

    前言:通过设计器交互来创建流程图是比较常见的方式,这种方式是比较方便业务人员对流程的操作.然而,在需要流程模板,或者技术开发阶段以及一些自动化流程的处理过程中,使用代码快速创建流程图也是一种非常有必要 ...