android脚步---使用framelayout实现霓虹灯效果
轮换帧布局中7个TextView的背景颜色,会出现上面颜色渐变不断变换。

首先在main.xml文件中进行布局
总体布局为framelayout 中间有7个Textview,代表7种不同的颜色,可以看到高度相同,宽度逐渐减少,则最新添加的textvIEW不会被完全遮挡,设置了颜色渐变
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 依次定义7个TextView,先定义的TextView位于底层
后定义的TextView位于上层 -->
<TextView android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210px"
android:height="50px"
android:background="#ff0000"
/>
<TextView android:id="@+id/View02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180px"
android:height="50px"
android:background="#dd0000"
/>
<TextView android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:height="50px"
android:background="#bb0000"
/>
<TextView android:id="@+id/View04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:height="50px"
android:background="#990000"
/>
<TextView android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="90px"
android:height="50px"
android:background="#770000"
/>
<TextView android:id="@+id/View06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60px"
android:height="50px"
android:background="#550000"
/>
<TextView android:id="@+id/View07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="30px"
android:height="50px"
android:background="#330000"
/>
</FrameLayout>
在mainactivity里定义
package com.example.framlayout; import java.util.Timer;
import java.util.TimerTask; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView; public class MainActivity extends Activity
{
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,
};
//颜色显示数组,view为TextView控件
final int[] names = new int[]
{
R.id.View01,
R.id.View02,
R.id.View03,
R.id.View04,
R.id.View05,
R.id.View06,
R.id.View07
};
TextView[] views = new TextView[7];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0 ; i < 7 ; i++)
{
views[i] = (TextView)findViewById(names[i]);
}
//使用Handler进行消息处理
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
//表明消息来自本程序所发送
if(msg.what == 0x1122)
{
//依次改变7个TextView的背景色
for(int i = 0 ; i < 7 - currentColor ; i++)
{
views[i].setBackgroundResource(colors[i + currentColor]); //改变背景色
}
for(int i = 7 - currentColor , j = 0 ; i < 7 ; i++ ,j++)
{
views[i].setBackgroundResource(colors[j]);
}
}
super.handleMessage(msg);
}
};
//定义一个线程周期性的改变currentColor变量值
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
currentColor++;
if(currentColor >= 6)
{
currentColor = 0;
}
//发送一条消息通知系统改变7个TextView组件的背景色
Message m = new Message();
//给该消息定义一个标识
m.what = 0x1122;
handler.sendMessage(m);
}
}, 0 , 100); //周期为100毫秒
}
}
使用handler来处理textview背景色的更新,定义一个线程周期性0.1秒执行一次任务,该任务仅仅改变currentcolor变量的值,然后向handler发送一条消息,通知它更新7个textview的背景色。
出现问题:colors数组那个地方在EC里面提示的是color cannot be resolved or is not a field???
colors数组那个地方在EC里面提示的是color cannot be resolved or is not a field???
因为:你缺少颜色的资源文件,在String.xml中添加如下<color.../>子元素即可!!!
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">framlayout</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<color name="color1">#0f0</color>
<color name="color2">#00f</color>
<color name="color3">#0ff</color>
<color name="color4">#f0f</color>
<color name="color5">#ff0</color>
<color name="color6">#07f</color>
<color name="color7">#70f</color>
</resources>
android脚步---使用framelayout实现霓虹灯效果的更多相关文章
- 【Android】使用FrameLayout布局实现霓虹灯效果
FrameLayout是五大布局中最简单的一个布局. 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置. 它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 ...
- FrameLayout和handle实现霓虹灯效果
这个程序的主要思想就是在一个FrameLayout中定义多个TextView,分别设置不同的背景色.因为帧布局的特性,所以这些控件都是叠加起来的.然后,通过定时器循环给handler发送消息,改变控件 ...
- android小Demo--七彩霓虹灯效果
七彩霓虹灯效果,基于网上的小Demo进行修改. 在android项目values文件夹下创建文件colors.xml,配置七种颜色: <?xml version="1.0" ...
- android 自定义scrollview 仿QQ空间效果 下拉伸缩顶部图片,上拉回弹 上拉滚动顶部title 颜色渐变
首先要知道 自定义scrollview 仿QQ效果 下拉伸缩放大顶部图片 的原理是监听ontouch事件,在MotionEvent.ACTION_MOVE事件时候,使用不同倍数的系数,重置布局位置[ ...
- Android 布局之FrameLayout
Android 布局之FrameLayout 1 FrameLayout简介 对于FrameLayout,官方介绍是:FrameLayout is designed to block out an a ...
- Android中的FrameLayout帧布局
帧布局由FrameLayout所代表,FrameLayout直接继承了ViewGoup组件. 帧布局容器为每一个增加当中的组件创建一个空白的区域(称为一个帧),每一个子组件占领一帧,这些帧都会依据gr ...
- Android UI - 实现广告Banner旋转木马效果
Android UI - 实现广告Banner旋转木马效果 前言 本篇博客要分享的一个效果是实现广告Banner轮播效果,这个效果也比較常见,一些视频类应用就常常有,就拿360影视大全来举例吧: 用红 ...
- Android利用温度传感器实现带动画效果的电子温度计
概述 Android利用温度传感器实现带动画效果的电子温度计. 详细 代码下载:http://www.demodashi.com/demo/10631.html 一.准备工作 需要准备一部带有温度传感 ...
- android 水纹上涨与水滴滴下效果
这两天项目比较紧,本来打算完成以后再写博客的,今天终于实现了一个炫的功能,怀着激动的心情,趁热打铁,把项目经验记录一下,效果图如下: 对功能的几点说明: 1.圆形边框旋转 2.水纹上涨 3.水滴滴下 ...
随机推荐
- jQuery.on() 函数详解[http://www.365mini.com/page/jquery-on.htm]
中文手册 2014年09月01日 暂无评论 on()函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数 ...
- mahout与nosql的两幅经典图形
- pudn下载地址的规律
A:http://download.pudn.com/downloads15/sourcecode/app/354278Cams.rar(随机数字6个)B:http://www.pudn.com/do ...
- Kubernetes 1.5.1 部署
> kubernetes 1.5.0 , 配置文档 # 1 初始化环境 ## 1.1 环境: | 节 点 | I P ||--------|-------------||n ...
- servlet第3讲(中集)----同一用户的不同页面共享数据
5.session 5.1session概述 5.2.session应用举例
- iOS使用NSMutableAttributedString
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- reflow和repaint
Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验 简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都 ...
- nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】
转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...
- 搭建Ubuntu下c/c++编译环境【转】
1. 安装Ubuntu. 2. 安装gcc 方法一: sudo apt-get install build-essential 安装完了可以执行 gcc--version的 ...
- 作为Web开发人员,我为什么喜欢Google Chrome浏览器
来源: http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.html 在Google Chrome浏览器出来之前,我一直使用FireF ...