学到一个android小应用-霓虹灯闪烁效果界面
2016-03-31 18:54
507 查看
代码如下:使用的是帧布局
java 代码:
XML文件代码:
其中用到颜色资源,color.xml代码:
很好玩哦,刚开始学android ,蛮好玩的!!!
java 代码:
package com.example.home.nihongdeng; import android.os.Handler; import android.os.Message; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; 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 }; final int[] names=new int[]{ R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5 }; 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]); } //定义一个线程周期性的改变verrentColor的变量值 new Timer().schedule(new TimerTask() { @Override public void run() { //发送一条空消息通知系统改变6个TextView的背景颜色 handler.sendEmptyMessage(0x123); } },0,200);//每0.2秒执行一次 } }
XML文件代码:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.home.nihongdeng.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv0" android:layout_gravity="center" android:width="240pt" android:height="240pt" android:background="#f00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:layout_gravity="center" android:width="200pt" android:height="200pt" android:background="#0f0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv2" android:layout_gravity="center" android:width="160pt" android:height="160pt" android:background="#00f"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv3" android:layout_gravity="center" android:width="120pt" android:height="120pt" android:background="#ff0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv4" android:layout_gravity="center" android:width="80pt" android:height="80pt" android:background="#f0f"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv5" android:layout_gravity="center" android:width="40pt" android:height="40pt" android:background="#0ff"/> </FrameLayout>
其中用到颜色资源,color.xml代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="color6">#0ff</color> <color name="color5">#f0f</color> <color name="color4">#ff0</color> <color name="color3">#00f</color> <color name="color2">#0f0</color> <color name="color1">#f00</color> </resources>
很好玩哦,刚开始学android ,蛮好玩的!!!
相关文章推荐
- Android 属性动画(Property Animation) 完全解析
- Android的性能优化
- Android 短信验证码平台
- android studio 开发常用快捷键使用分享
- Android 应用中使用ContentObserver自动填写短信验证码
- bank掉回弹效果
- Android中软键盘设置
- Android 初使用GreenDAO框架操作数据库
- Android设计模式(1)----单例模式
- 慕课-Android工程师-java入门第一季
- Android初学习 - 基础&进阶总结
- Android设计模式 单例模式(枚举单例)
- Android 5.0以上 获取当前正在运行的应用包名
- [置顶] Android Theme-安卓样式换肤实践方案
- Android初学习 - Activity的四种launchMode
- Android系统性能调优工具介绍
- challenge(六)图片异步加载类库(Android-Universal-Image-Loader)的使用
- 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优化方案
- Android初学习 - 声明和使用权限
- android native hook 源码 及 解析