码迷,mamicode.com
首页 > 移动开发 > 详细

android 霓虹灯效果

时间:2014-07-26 14:50:00      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   java   color   os   io   for   

package com.example.test;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private int currentColor = 0;
    //定义一个颜色数组
    final int[] colors = new int[]
    {
        R.color.color7,
        R.color.color6,
        R.color.color5,
        R.color.color4,    
        R.color.color3,
        R.color.color2,
        R.color.color1,    
    };
    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
    };
    TextView[] views = new TextView[7];
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i = 0 ; i < 7 ; i++)
        {
            views[i] = (TextView)findViewById(names[i]);
        }
        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                //表明消息来自本程序所发送
                if(msg.what == 0x1122)
                {
                    //依次改变7个TextView的背景色
                    for(int i = 0 ; i < 7 - currentColor ; i++)    
                    {
                        views[i].setBackgroundResource(colors[i + currentColor]);
                    }
                    for(int i = 7 - currentColor , j = 0 ; i < 7 ; i++ ,j++)
                    {
                        views[i].setBackgroundResource(colors[j]);
                    }
                }
                super.handleMessage(msg);
            }
        };
        //定义一个线程周期性的改变currentColor变量值
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                currentColor++;
                if(currentColor >= 6)
                {
                    currentColor = 0;
                }
                //发送一条消息通知系统改变7个TextView组件的背景色
                Message m = new Message();
                //给该消息定义一个标识
                m.what = 0x1122;
                handler.sendMessage(m);    
            }        
        }, 0 , 100); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

android 霓虹灯效果,布布扣,bubuko.com

android 霓虹灯效果

标签:android   style   blog   java   color   os   io   for   

原文地址:http://www.cnblogs.com/yufenghou/p/3869990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!