Android中实现多彩的霓虹灯
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lixc.neonlight.MainActivity"
android:orientation="vertical"> </LinearLayout>
2、color.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#ffff0000</color>
<color name="color2">#ffff6600</color>
<color name="color3">#ffffff00</color>
<color name="color4">#ff00ff00</color>
<color name="color5">#ff00ffff</color>
<color name="color6">#ff0000ff</color>
<color name="color7">#ff6600ff</color>
</resources>
3、在MainActivity中声明成员变量
private Handler handler;
private static LinearLayout linearLayout;
public static TextView[] textViews = new TextView[14];
int[] bgColor = new int[]{R.color.color1, R.color.color2, R.color.color3,
R.color.color4, R.color.color5, R.color.color6, R.color.color7};
private int index = 0;
4、在MainActivity中的onCreate()方法中,添加文本框组件到线性布局管理器中
linearLayout = (LinearLayout)findViewById(R.id.mainll);
int height = this.getResources().getDisplayMetrics().heightPixels;
for (int i=0;i<textViews.length;i++){
textViews[i] = new TextView(this);
textViews[i].setWidth(this.getResources().getDisplayMetrics().widthPixels);
textViews[i].setHeight(height/textViews.length);
linearLayout.addView(textViews[i]);
}
5、创建新线程,重写run()方法
new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()){
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
try {
Thread.sleep(new Random().nextInt(1000));
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
6、创建一个Handler对象,重写handleMessage()方法
handler = new Handler(){
@Override
public void handleMessage(Message msg){
int temp = 0;
switch (msg.what){
case 1:
for (int i=0;i<textViews.length;i++){
temp = new Random().nextInt(bgColor.length);
if (index == temp){
temp++;
if (temp == bgColor.length){
temp = 0;
}
}
index = temp;
textViews[i].setBackgroundColor(getResources().getColor(bgColor[index]));
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
7、实现效果
这里只是一张图片,实际上是一闪一闪的效果。

Android中实现多彩的霓虹灯的更多相关文章
- Android中的LinearLayout布局
LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了, 线性布局是按照垂直方向(vertical)或水平方向 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- 【月入41万】Mono For Android中使用百度地图SDK
借助于Mono For Android技术,.Net开发者也可以使用自己熟悉的C#语言以及.Net来开发Android应用.由于Mono For Android把Android SDK中绝大部分类库都 ...
- mono for android中使用dapper或petapoco对sqlite进行数据操作
在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...
- Android开发学习之路-Android中使用RxJava
RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...
随机推荐
- 从H264码流中获取视频宽高 (SPS帧) 升级篇
之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...
- android:ems的作用
android:ems用来设置EditText或TextView显示的字符宽度. 比如:android:ems="10" 设置TextView或EditText为10个字符的宽度, ...
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
得到后台从数据库中拿到的数据我们希望格式是 2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30 然而我们前台得到的却是一段数字(时间戳,毫秒数) 14773860 ...
- js替换选中的文字
替换html中选中的文字 function replaceSelection() { if (window.getSelection) { var selecter = window.getSelec ...
- 【openresty】获取post请求数据FormInputNginxModule模块
关于openresty的一些介绍看这里. 首先,实验背景为openresty作为后台来处理前台post传递的数据. 在openresty内,有一个FormInputNginxModule模块,作用是解 ...
- c# long转 datetime
; DateTime start = , , , , , , DateTimeKind.Utc); DateTime date = start.AddMilliseconds(time).ToLoca ...
- 关于meta标签
一.Meta标签中的format-detection属性及含义 意为:格式检测 或许你会有这样的经历:当你在制作手机端的页面中,点击了没有加任何链接的格式的数字时,这时手机会进行自动拔号提示操作! 禁 ...
- 漫谈Linux内核哈希表(2)
对照前面介绍过的内核通知链.链表,本章我们将要介绍的哈希表的初始化和定义也是如出一辙的: 点击(此处)折叠或打开 定义并初始化一个名为name的哈希链表表头 #define HLIST_HEAD(na ...
- 常见sizeof 笔试题
最近面试过程中遇到了很多很多sizeof的问题. 现在总结一下: #include <stdio.h> #include <string.h> void fun(int arr ...
- ms sql server 在cmd中执行sqlcmd的时候报错
cmd下直接输入sqlcmd会提示 错误: HResult 0x2,级别 16,状态 1命名管道提供程序: 无法打开与 SQL Server 的连接 [2].Sqlcmd: 错误: Microsoft ...