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). ...
随机推荐
- PostGIS ShapeFile 导入数据
系统环境:WIN10 相关软件:PostgresSQL+PostGIS+QGIS 1.添加postGIS数据扩展 (1).打开PostgresSQL 客户端pgAdmin,在需要导入shp数据的数据库 ...
- JDBC获取sql server存储过程查询结果集(没有出参)
对于一些较为复杂的统计条件查询,可以通过存储过程来实现,既可以提高效率,减少网络流量,也可以避免sql语句耦合在代码中.但是存储过程返回的结果集如何获取(类似表数据),却着实让我费劲心力. 如下: C ...
- SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0
1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度
- 编译Hadoop
Apache Hadoop 生态圈软件下载地址:http://archive.apache.org/dist/hadoop/hadoop下载地址 http://archive.apache.org/d ...
- 【经验记录】Jconsole Jvisualvm 监控Tomcat
环境:centos 6 1.首先检查hostname是否正确,输入以下命令 hostname -i 如果输出机器ip,则表示正确,如果输出 hostname: Unknown host 查看/etc/ ...
- pxe无人值守安装多网卡注意事项
pxe无人值守安装linux配置这里就不说了,直接看这篇博客http://www.cnblogs.com/mchina/p/centos-pxe-kickstart-auto-install-os.h ...
- Centos | Linux 下安装启动 mysql 出现 8618 [ERROR] Aborting,查看日志:Plugin 'FEDERATED' is disabled.
1.试试启动时指定配置文件 ./bin/mysqld_safe --defaults-file=mysql.cnf 或 ./bin/mysqld_safe --defaults-file=mysql. ...
- postgresql+slony-i安装配置主从
slon软件下载地址:slony1-1.2.6 http://slony.info/downloads/1.2/source/ postgresql下载地址: http://www.postgresq ...
- Test注解的两个属性(转)
Test注解的两个属性:expected和timeout Junit的Test注解支持两个可选的参数expected和timeout.expected声明一个测试方法必须抛出一个异常.如果不抛出异常或 ...
- 求两个集合的交集和并集C#
我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...