检测到连续按下View

时间:2013-09-11 17:21:02

标签: android

只要用户按下ImageView,我就需要播放某个声音,并在用户停止按住时暂停它。在使用MediaPlayer做任何事情之前,我尝试用Vibrator(请不要嘲笑这一点)对象来测试它。我已经实现并设置了OnTouchListener

    @Override
public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:

        vib.vibrate(50);

        break;

    case MotionEvent.ACTION_MOVE:
        /*--- no action required ---*/
        break;

    case MotionEvent.ACTION_UP:

        vib.cancel();
        break;
    }
    return false;

}

但是,无论我按住ImageView多长时间,振动动作都只执行一次。我想MediaPlayer会以同样的方式做出反应。我做错了什么?

P.S。正确检测到印刷机,因为Vibrator仅在我松开手指后才记录“取消()”。

1 个答案:

答案 0 :(得分:1)

因为功能

而停止
vib.vibrate(50);

你在振动check

中设置为50毫秒时间
void vibrate(long milliseconds)
Vibrate constantly for the specified period of time.

我认为你的其他代码是正确的。