欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android手機(jī)顯示多彩霓虹燈效果

 更新時(shí)間:2017年12月26日 09:32:20   作者:光仔December  
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)顯示多彩霓虹燈效果的小實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用之前學(xué)過(guò)的多線程處理技術(shù),我們做一個(gè)利用Android手機(jī)顯示一個(gè)多彩霓虹燈效果的小實(shí)例。

布局文件,這里只留有加了id的線性布局文件
res/layout/mian.xml:

<?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/linearLayout1" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
   
</LinearLayout> 

在res/values目錄下,我們創(chuàng)建一個(gè)保存顏色資源的colors.xml文件,定義七個(gè)顏色資源(赤橙黃綠青藍(lán)紫):

<?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> 

首先獲取線性布局管理器,然后獲取屏幕的高度,再通過(guò)for循環(huán)創(chuàng)建14個(gè)文本框組件,并添加到線形布局管理器中。之后創(chuàng)建并開(kāi)啟一個(gè)新線程,在重寫(xiě)的run()方法中實(shí)現(xiàn)一個(gè)循環(huán),在該循環(huán)中,首先獲取一個(gè)Message對(duì)象,并為其設(shè)置一個(gè)消息標(biāo)示,然后發(fā)送消息,最后讓線程休息1秒鐘。

在onCreat()方法中,創(chuàng)建一個(gè)Handler對(duì)象,在重寫(xiě)的HanlderMessage方法中,為每一個(gè)文本框設(shè)置顏色,該背景顏色從顏色數(shù)組中隨機(jī)獲取。這樣就實(shí)現(xiàn)了多彩霓虹燈效果的小實(shí)例,具體代碼如下:

MainActivity:

package com.example.test;  
  
import java.util.Random; 
 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.Window; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
  
public class MainActivity extends Activity{  
   private Handler handler;//Handler對(duì)象 
   private static LinearLayout linearLayout;//整體布局 
   public static TextView[] tv=new TextView[14];//TextView數(shù)組 
   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;//當(dāng)前顏色值 
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    requestWindowFeature(Window.FEATURE_NO_TITLE);//設(shè)置全屏顯示 
    setContentView(R.layout.main); 
    
    //獲取線性布局管理器 
    linearLayout=(LinearLayout)findViewById(R.id.linearLayout1); 
    //獲取屏幕的高度 
    int height=this.getResources().getDisplayMetrics().heightPixels; 
    for (int i = 0; i < tv.length; i++) { 
      tv[i]=new TextView(this);//創(chuàng)建一個(gè)文本框?qū)ο?
      //設(shè)置文本框的寬度 
      tv[i].setWidth(this.getResources().getDisplayMetrics().widthPixels); 
      //設(shè)置文本框的高度 
      tv[i].setHeight(height/tv.length); 
      //將TextView組件添加到線性布局管理器中 
      linearLayout.addView(tv[i]); 
    } 
     
    Thread t=new Thread(new Runnable(){ 
      @Override 
      public void run() { 
         while(!Thread.currentThread().isInterrupted()){ 
           Message m=handler.obtainMessage();//獲取一個(gè)Message 
           m.what=0x101;//設(shè)置消息標(biāo)識(shí) 
           handler.sendMessage(m);//發(fā)送消息 
           try { 
            Thread.sleep(new Random().nextInt(1000));//休眠1秒鐘 
          } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();//輸出異常信息 
          } 
         } 
      } 
    }); 
    t.start();//開(kāi)啟線程 
     
    handler=new Handler(){ 
 
 
      @Override 
      public void handleMessage(Message msg) { 
        int temp=0; 
        if(msg.what==0x101){ 
          for (int i = 0; i < tv.length; i++) { 
            temp=new Random().nextInt(bgColor.length);//產(chǎn)生一個(gè)隨機(jī)數(shù) 
            //去掉重復(fù)的并相鄰的顏色 
            if(index==temp){ 
              temp++; 
              if(temp==bgColor.length){ 
                temp=0; 
              } 
            } 
            index=temp; 
            //為文本框設(shè)置背景 
            tv[i].setBackgroundColor(getResources().getColor(bgColor[index])); 
          } 
        } 
        super.handleMessage(msg); 
      } 
       
    }; 
  } 
 
 
} 

 運(yùn)行效果如圖

是不是很炫酷!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論