帧布局实现霓虹灯效果
2015-11-25 16:37
288 查看
Framelayout即帧布局,使用这种布局可以把几个控件叠加在一起。使用Framelayout结合textview就可以实现一个简单的霓虹灯效果。
一、首先在XML中使用了FrameLayout布局并添加六个TestView文件,并设定了颜色和位置。
XML布局如下:
二、便于管理及调用将定义的六种颜色在colors.xml中定义
三、MainActivity中主程序代码
四、动态霓虹灯效果
一、首先在XML中使用了FrameLayout布局并添加六个TestView文件,并设定了颜色和位置。
XML布局如下:
<?xmlversion="1.0"encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_gravity="right" android:width="160pt" android:height="160pt" android:background="#f00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_gravity="right" android:width="140pt" android:height="140pt" android:background="#88ff00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_gravity="right" android:width="120pt" android:height="120pt" android:background="#ffffff"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_gravity="right" android:width="100pt" android:height="100pt" android:background="#3be407"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView5" android:layout_gravity="right" android:width="80pt" android:height="80pt" android:background="#ffff88"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView6" android:layout_gravity="right" android:width="60pt" android:height="60pt" android:background="#8329c7"/> </FrameLayout>
二、便于管理及调用将定义的六种颜色在colors.xml中定义
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<colorname="color1">#f00</color>
<colorname="color2">#88ff00</color>
<colorname="color3">#ffffff</color>
<colorname="color4">#3be407</color>
<colorname="color5">#ffff88</color>
<colorname="color6">#8329c7</color>
</resources>
三、MainActivity中主程序代码
packagehappy.framelayout;
importandroid.os.Bundle;
importandroid.os.Message;
importandroid.support.v7.app.AppCompatActivity;
importandroid.widget.TextView;
importjava.util.Timer;
importjava.util.TimerTask;
publicclassMainActivityextendsAppCompatActivity{
privateintcurrentColor=0;
//定义一个颜色数组
finalint[]colors=newint[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
};
finalint[]names=newint[]{
R.id.textView,
R.id.textView2,
R.id.textView3,
R.id.textView4,
R.id.textView5,
R.id.textView6,
};
TextView[]views=newTextView[names.length];
//handler主要接收主线程发送的数据,并用此数据配合主线程更新UI
android.os.Handlerhandler=newandroid.os.Handler()
{
@Override
publicvoidhandleMessage(Messagemsg)
{
//表明消息来自本程序所发送
if(msg.what==0x234)
{
//依次改变testView的背景
for(inti=0;i<names.length;i++)
{
//收到一条msg就开始改变一次views的颜色(六个都依照colors的+currentcolor颜色来改)
views[i].setBackgroundResource(colors[(i
+currentColor)%names.length]);
}
currentColor++;
}
//将消息抛给父类,避免丢失
super.handleMessage(msg);
}
};
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
for(inti=0;i<names.length;i++)
{
//为views设置初始颜色,(六个TestView)
views[i]=(TextView)findViewById(names[i]);
}
//定义一个线程周期性地改变currentColor变量值
newTimer().schedule(newTimerTask(){
@Override
publicvoidrun(){
//发送一条空消息通知系统改变6个TextView组件的背景色
handler.sendEmptyMessage(0x234);
}
},0,200);
}
}
四、动态霓虹灯效果
相关文章推荐
- IOS UIView 之实例方法 Instance Methods
- CentOS下修复grub引导程序
- java 实现https请求
- linux shell实现随机数多种方法(date,random,uuid)
- UITextField点击textField外任意区域键盘回收(一)---- [UIAppllication shareApplication]
- [Servlet]请求封装器、响应封装器
- poj 1201 Intervals
- 1076. Forwards on Weibo
- VB.NET 用循环判断文本框是否为空
- 1074. Reversing Linked List
- 移动开发 反劫持
- 关于一些动画和特效的收集,碰到了就收集
- Legolas工业自动化平台案例 —— 水源地自动化监控系统
- iOS开发多线程-GCD的常见用法
- 走进Java(五)JSTL和EL表达式
- Android动画之属性动画(二)
- ajax接口之一:登录退出系统
- Activty启动通过Action练习
- oracle存储过程、声明变量、for循环
- android编译分析之9—dumpvar.mk