2013-10-18 23 views
0

當用戶觸摸按鈕時,應該播放聲音。有很多按鈕,每個按鈕都有不同的聲音。當用戶在所有按鈕上移動手指時,應播放所有聲音。像鋼琴應用程序的東西。我該怎麼做?我嘗試過使用ontouch列表,但它不起作用。在Android應用中的按鈕上移動手指

回答

1

在您的onTouch方法中,您應該從用戶TouchDown,TouchMove和最終TouchUp開始捕獲不同的事件。尋找所有x,y座標是否在你的按鈕區域,如果是的話,播放聲音。

確保當前按鈕選擇比前一個不同的,否則,如果你的手指移動在同一個按鈕,即會觸發相同的按鈕另一個事件,你會在這裏噸的聲音,同時移動手指:

psudo代碼:

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 

    switch(event.getAction()) 
    { 

    case MotionEvent.ACTION_UP:  // stop here 
     break; 
    case MotionEvent.ACTION_DOWN: // start here 
     break; 
    case MotionEvent.ACTION_MOVE: 
      // See if new x y co-ordinates in your buttonRect area 
      // RectF [] buttonRect = new buttonRect[10] ; 
      for(int i = 0; i < 10; i++) 
      { 
       if(buttonRect[i].contains(event.getX(), event.getY())) 
       { 
        // if it's a new button found than previously touch, play a sound 

        // store the button number that's been tapped by user 

       } 
      } 


     break; 
    } 
} 

希望它能幫助。

+0

plz,接受&upvote如果答案有幫助。 –