我想在android中使用onTouch。 在ACTION_DOWN中我想让按钮闪烁一个模式[3秒关闭3秒] 我希望这个模式的3个间隔做到这一点。 如果在3个间隔完成之前收到ACTION_UP或ACTION_CANCEL,则取消并保持按钮可见。 否则调用一个函数。
我将如何做到这一点?
我使用模式使用振动器做了类似的事情。
long pattern[] = new long[] {0, 500, 500, 500, 500, 500, 500, 2000};
vibrator.vibrate(pattern, -1);
并非所有设备都有振动器,所以我不想用闪光灯按钮。 任何帮助将不胜感激。
以下代码有效,但不是很优雅。 必须有更好的方法。
public class AndroidAnimButtonsActivity extends Activity
{
CountDownTimer countDownTimerOn;
CountDownTimer countDownTimerOff;
CountDownTimer countDownTimerSOS;
ImageButton SOSButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countDownTimerOn = new CountDownOn(100, 1000);
countDownTimerOff = new CountDownOff(100, 1000);
countDownTimerSOS = new CountDownSOS(3*1000, 1000);
SOSButton = (ImageButton)findViewById(R.id.sosbutton);
SOSButton.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
SOSButton.setImageResource(R.drawable.panic2);
countDownTimerOff.start();
countDownTimerSOS.start();
break;
case MotionEvent.ACTION_UP :
SOSButton.setImageResource(R.drawable.panic1);
countDownTimerOn.cancel();
countDownTimerOff.cancel();
countDownTimerSOS.cancel();
SOSButton.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_CANCEL :
SOSButton.setImageResource(R.drawable.panic1);
countDownTimerOn.cancel();
countDownTimerOff.cancel();
countDownTimerSOS.cancel();
SOSButton.setVisibility(View.VISIBLE);
break;
}
return false;
}
});
}
public class CountDownOn extends CountDownTimer
{
public CountDownOn(long totalTime, long interval)
{
super(totalTime, interval);
}
@Override
public void onFinish()
{
SOSButton.setVisibility(View.VISIBLE);
countDownTimerOff.start();
}
@Override
public void onTick(long leftTimeInMilliseconds)
{
long seconds = leftTimeInMilliseconds / 1000;
}
}
public class CountDownOff extends CountDownTimer
{
public CountDownOff(long totalTime, long interval)
{
super(totalTime, interval);
}
@Override
public void onFinish()
{
SOSButton.setVisibility(View.INVISIBLE);
countDownTimerOn.start();
}
@Override
public void onTick(long leftTimeInMilliseconds)
{
long seconds = leftTimeInMilliseconds / 1000;
}
}
// CountDownTimer class
public class CountDownSOS extends CountDownTimer
{
public CountDownSOS(long totalTime, long interval)
{
super(totalTime, interval);
}
@Override
public void onFinish()
{
SOSButton.setImageResource(R.drawable.panic2);
SOSButton.setVisibility(View.VISIBLE);
countDownTimerOn.cancel();
countDownTimerOff.cancel();
sendMsg();
}
@Override
public void onTick(long leftTimeInMilliseconds)
{
long seconds = leftTimeInMilliseconds / 1000;
}
}
public void sendMsg()
{
Toast.makeText(getApplicationContext(),"SOS", Toast.LENGTH_SHORT).show();
}
}