2011-09-14 184 views
0

我有一個人的圖像,我想播放不同的聲音,取決於在圖像上是否單擊右側或左側手勢,並且如果單擊頭部,我會馬上添加第三個聲音,有沒有簡單的方法來做到這一點?圖像中的Android可點擊區域

回答

1

我認爲最好的方法是使用OnTouchEvent,並使用getX()和getY()。這可能有助於解釋它更好一點

public boolean onTouch(View v, MotionEvent e) { 
    // TODO Auto-generated method stub 
    float x = e.getX(); 
    float y = e.getY(); 


    switch (e.getActionMasked()) { 
    case MotionEvent.ACTION_DOWN: 

     if (x > 1 & x < 200 & y > 1 & y < 200) { 
      ///Pretty much, what the X and Y are, are coordinates. 
         soundPool.play(PLAY SOUND 1, 10, 10, 1, 0, 1); 
          ///I Use soundPool, but you could use whatever 


////I think you'll have to figure out where each of the coordinates are at to the corresponding 
///body part. E.G if the hand lies between (X= 220 to 240 and Y= 120 to 140) then 
///simply say: **(x >220 & x<240 & y >120 & y <140)** 



} 
     if (x > 1 & x < 200 & y > 200 & y < 400) { 
      soundPool.play(PLAY SOUND 2, 10, 10, 1, 0, 1); 

/// this is just another example of another set of coordinates. 
     } 

     break; 

另外,您應該實現OnTouchListener。顯然,這隻覆蓋了代碼中的OnTouch部分,其餘部分應該是非常自我解釋的(假設你聽到了聲音和視圖)。 讓我知道這是否有幫助。上帝保佑!

1

將人物圖像設置爲佈局的背景。 在人物圖像上放置透明按鈕,例如:頭部,左/右手,併爲透明按鈕添加按鈕點擊偵聽器。 在按鈕點擊監聽器中播放你想要的聲音。 這是更簡單的方法之一。