码迷,mamicode.com
首页 > 其他好文 > 详细

通过动态广播机制写一个彩虹动态效果!

时间:2015-11-20 10:26:55      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

package com.lixu.caihong;

import com.lixu.caihong.MainActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private String action = "com.lixu";
    private static String KEY = "XX";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Activityb.class);
                startActivity(intent);
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        int[] color1 = { 0xffB71C1C, 0xfff44336, 0xffEEFF41, 0xff00C853, 0xff4CAF50, 0xff03A9F4,
                                0xff6200EA };
                        int a = 0;
                        int b = 1;
                        int c = 2;
                        int d = 3;
                        int e = 4;
                        int f = 5;
                        int g = 6;
                        while (true) {
                            Intent intent = new Intent();
                            intent.setAction(action);
                            int[] color = { color1[a], color1[b], color1[c], color1[d], color1[e], color1[f],
                                    color1[g] };
                            intent.putExtra(KEY, color);
                            MainActivity.this.sendBroadcast(intent);
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            a++;
                            b++;
                            c++;
                            d++;
                            e++;
                            f++;
                            g++;
                            if (a == color1.length) {
                                a = 0;
                            } else if (b == color1.length) {
                                b = 0;
                            } else if (c == color1.length) {
                                c = 0;
                            } else if (d == color1.length) {
                                d = 0;
                            } else if (e == color1.length) {
                                e = 0;
                            } else if (f == color1.length) {
                                f = 0;
                            } else if (g == color1.length) {
                                g = 0;
                            }
                        }

                    }
                }).start();
            }
        });
    }
}
package com.lixu.caihong;

import java.util.Random;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Activityb extends Activity {
    private String action = "com.lixu";
    private static String KEY = "XX";
    private static final int WHAT = 1;
    Handler handler;
    Mybroadcast mMybroadcast;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activityb);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
        tv5 = (TextView) findViewById(R.id.tv5);
        tv6 = (TextView) findViewById(R.id.tv6);
        tv7 = (TextView) findViewById(R.id.tv7);
        mMybroadcast = new Mybroadcast();//创建广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);//设置过滤器
        registerReceiver(mMybroadcast, filter);
        handler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == WHAT) {
                    int[] color = (int[]) msg.obj;
                    tv1.setBackgroundColor(color[0]);
                    tv2.setBackgroundColor(color[1]);
                    tv3.setBackgroundColor(color[2]);
                    tv4.setBackgroundColor(color[3]);
                    tv5.setBackgroundColor(color[4]);
                    tv6.setBackgroundColor(color[5]);
                    tv7.setBackgroundColor(color[6]);
                }
            }

        };

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMybroadcast);
        super.onDestroy();
    }

    public class Mybroadcast extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            int[] colors = intent.getIntArrayExtra(KEY);
            Message msg = handler.obtainMessage();
            msg.what = WHAT;
            msg.obj = colors;
            handler.sendMessage(msg);
        }
    }
}

 运行效果:

技术分享

通过动态广播机制写一个彩虹动态效果!

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/4940284.html

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