0

我正在尝试在android中开发一个乐器应用程序。我为每个按钮都实现了OnTouchListener,只点一个按钮就没有问题。但是当我触摸一个按钮并将手指移动到下一个按钮时,不会调用该按钮的 OnTouchListener 并且不会播放它的声音。如何在不抬起手指的情况下在按钮上滑动手指时播放声音?(我读了很多问题,但它们没有用)

我对每个按钮的简单 OnTouchListener :

final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1);
img_1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
              if( event.getAction() ==  MotionEvent.ACTION_DOWN )   {
                snd.play_s_l_1(); 
              }
             return true;
        } 

 });
4

1 回答 1

2

检查 MotionEvent.MOVE:

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    }
    if(event.getAction() == MotionEvent.ACTION_MOVE)
    {
        if(!rect.contains((int)event.getX(), (int)event.getY()))
        {
            // User moved outside bounds
        }
    }
    return false;
}
于 2013-01-26T18:13:27.287 回答