2012-08-08 35 views
-1

對於我的應用程序,我想要多種顏色的閃光燈來播放,我該怎麼做?如何在Android上製作閃光燈效果?

+0

你想要你的應用程序的完整源代碼? – 2012-08-08 19:36:34

+1

歡迎來到Stack Overflow! [你有什麼嘗試?](http://whathaveyoutried.com) – 2012-08-08 19:37:13

回答

-1

如果你想讓你的屏幕以不同的顏色閃爍,那隻需要製作一個定時器,讓主視圖每隔一段時間都會改變背景顏色。

javax.swing.Timer中可用於每隔一段時間改變屏幕:

Timer colorChanger = new Timer(500 /*milis between each color change*/, new TimeListener(this)); 
colorChanger.start(); 

TimeListener將是一個ActionListener改變指定活動的背景色。 TimerListener看起來是這樣的:

public class TimerListener implements ActionListener { 
    public TimerListener(Activity activity) { 
     this.backgroundToChange = activity; 
    } 
    private Activity backgroundToChange = null; // the activity who's background we will change 
    private int numFrames = 0; //the number of frames that have passed 
    public void actionPerformed(ActionEvent evt) { //happens when the timer will go off 
     numFrames++; 
     switch (numFrames % 2) { // every other time it will make the background red or green 
      case 0: backgroundToChange.getContentView().setBackgroundColor(Color.RED); 
      case 1: backgroundToChange.getContentView().setBackgroundColor(Color.GREEN); 
     } 
    } 
} 

你會需要進口javax.swing.Timer中和的ActionListener和動作事件是java.awt.event中。

但是,如果您使用的是android,則可能需要考慮使用另一個專爲android以外的類設計的類。計時器是專爲擺動而設計的,如果您在android上使用它,可能無法正常工作。像類的任何其他計時器將工作類似於計時器雖然。

0

如果你想要不同的顏色等,那麼你可以在你的XML中創建一個View佔用整個屏幕寬度。然後基於AlarmManager,您可以使用setBackground()使其成爲您選擇的顏色。

使用Handler而不是AlarmManager可能更有利,但您可以查看兩者以查看是否適合您的需求。