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.水滴滴下 ...
随机推荐
- C语言_基础代码_01
#include<stdio.h> #include<stdlib.h> #define BUFFERSIZE 1024/*允许处理的最长行有1024个字符*/ /*编译环境v ...
- win8删除无线网络其中的一项配置
netsh wlan delete profile name="无线名称"
- 命令窗口修改编码,CMD编码修改方法
cmd中的编码方式为ANSI,若中文不是此编码方式则会出现乱码.作为程序员,会经常使用命令窗口查看执行日志,但是有时编码格式不对,大部分都是UTF8,在网上搜索了不少方法,很多没什么用,在这里教一个具 ...
- nmon命令用法
用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds > ] [ -c < count ...
- 网页特效-动态加载JavaScript
描述: 把一些逻辑独立的JavaScript脚本文件单独加载,是一种常见的JavaScript动态加载技术.可以减少不必要的JavaScript脚本文件的加载,以提高网页浏览速度 代码: <!D ...
- 更改web project 访问项目名称
1.新建web project 2.右键该项目名称------properties 3.访问该项目的URL http://localhost:8806/ssm/.......... 相比书写整个项目名 ...
- Java变量名命名规则
$ .字母.下划线开头都行,后面的可以是数字.字母.下划线: 匈牙利命名法.Camel命名法与Pascal命名法 匈牙利命名法:在Windows编程中使用非常普遍,由微软的一位匈牙利程序员提出.匈牙利 ...
- 1. 用自己的算法实现startsWith和endsWith功能。
package com.xinjian; public class Chazifu { public static void main(String[] args) { String a=" ...
- JavaScript焦点事件、鼠标事件和滚轮事件使用详解
网址:http://www.jb51.net/article/78094.htm
- listener.ora
EOF YESTERDAY=`cat /database/log/tns_log/yesterday.out` TODAY=`date '+%d-%b-%Y'` echo $YESTERDAY $T ...