2012-08-08 36 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可能更有利,但您可以查看两者以查看是否适合您的需求。