アプリで、多色のストロボ ライトを再生したいのですが、どうすればよいですか?
2 に答える
異なる色などが必要な場合はView
、XML で を作成して画面幅全体を占めることができます。次に、に基づいて、選択した色にするためにAlarmManager
使用できます。setBackground()
Handler
の代わりにを使用する方が有益かもしれませんがAlarmManager
、両方を見てニーズに合ったものを確認できます。
画面をさまざまな色で点滅させたい場合は、タイマーを作成し、メイン ビューの背景色を頻繁に変更するだけです。
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 と ActionEvent は java.awt.event にあります。
ただし、Android を使用している場合は、Timer 以外の Android 用に設計された別のクラスの使用を検討することをお勧めします。タイマーはスイング用に設計されているため、Android で使用するとうまく動作しない場合があります。ただし、クラスのような他のタイマーはタイマーと同様に機能します。