博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java多线程霓虹灯,Android开发实现布局帧布局霓虹灯效果示例
阅读量:4318 次
发布时间:2019-06-06

本文共 3345 字,大约阅读时间需要 11 分钟。

本文实例讲述了Android开发实现布局帧布局霓虹灯效果。分享给大家供大家参考,具体如下:

效果图:

4662fe4a64f26d833d8fd4c20f4c255e.gif

实现方式:

FrameLayout中,设置8个TextView,在主函数中,设计颜色数组,通过有序替换他们颜色,实现渐变效果。

java代码:MainActivity

public class MainActivity extends AppCompatActivity {

private int currentColor = 0;

/*

定义颜色数组 实现颜色切换 类似鱼片切换

*/

final int[] colors = new int[]{

R.color.color1,

R.color.color2,

R.color.color3,

R.color.color4,

R.color.color5,

R.color.color6,

R.color.color7,

R.color.color8

};

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,

R.id.view08

};

TextView[] views = new TextView[names.length];

Handler handler = new Handler(){

@Override

public void handleMessage(Message msg){

//表明消息由本日程发送

if(msg.what == 0x123){

for(int i = 0; i < names.length; i++){//更换颜色

views[i].setBackgroundResource(colors[ (i + currentColor) % names.length]);

}

currentColor++;

}

super.handleMessage(msg);

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

for(int i = 0; i < names.length; i++){//更换颜色

views[i] = (TextView) findViewById(names[i]);

}

//定义一个线程改变current变量值

new Timer().schedule(new TimerTask() {

@Override

public void run() {

//发送一条空消息通知系统改变6个TextView颜色

handler.sendEmptyMessage(0x123);

}

}, 0, 300);

}

}

xml文件

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/root"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/view01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="320dp"

android:height="320dp"

android:background="#ea7500"/>

android:id="@+id/view02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="280dp"

android:height="280dp"

android:background="#ff8000"/>

android:id="@+id/view03"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="240dp"

android:height="240dp"

android:background="#ff9224"/>

android:id="@+id/view04"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="200dp"

android:height="200dp"

android:background="#ffa042"/>

android:id="@+id/view05"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="160dp"

android:height="160dp"

android:background="#ffaf60"/>

android:id="@+id/view06"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="120dp"

android:height="120dp"

android:background="#ffa042"/>

android:id="@+id/view07"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="80dp"

android:height="80dp"

android:background="#ff9224"/>

android:id="@+id/view08"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:width="40dp"

android:height="40dp"

android:background="#ff8000"/>

color资源文件设置:

#008577

#00574B

#D81B60

#844200

#d26900

#ff9224

#ffbb77

#ffd1a4

#ffaf60

#ff8000

#bb5e00

希望本文所述对大家Android程序设计有所帮助。

转载地址:http://qwgzs.baihongyu.com/

你可能感兴趣的文章
Mybatis Batch 批量操作
查看>>
Ubuntu server搭建Java web服务器
查看>>
WSGI学习系列WSME
查看>>
java读取xml配置文件和properties配置文件
查看>>
HDU 4300 Contest 1
查看>>
POJ 3311
查看>>
Button MouseEvent颜色变化
查看>>
Volist标签
查看>>
浅谈模块化
查看>>
14个免费访客行为分析工具
查看>>
beego orm关联查询之多对多(m2m)
查看>>
(转)arguments.callee移除AS3匿名函数的侦听
查看>>
onNewIntent调用时机
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
Fail to start neutron-server
查看>>
景安快运挂在磁盘-支持宝塔
查看>>
word中交叉引用不能更新的解决方法
查看>>
高性能HTTP加速器Varnish(概念篇)
查看>>
Linux 如何写makefile文件
查看>>
flutter_webview_plugin 无法加载网页的异常处理
查看>>