图像中的Android可点击区域

问题描述:

我有一个人的图像,我想播放不同的声音,取决于在图像上是否单击右侧或左侧手势,并且如果单击头部,我会马上添加第三个声音,有没有简单的方法来做到这一点?图像中的Android可点击区域

我认为最好的方法是使用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部分,其余部分应该是非常自我解释的(假设你听到了声音和视图)。 让我知道这是否有帮助。上帝保佑!

将人物图像设置为布局的背景。 在人物图像上放置透明按钮,例如:头部,左/右手,并为透明按钮添加按钮点击侦听器。 在按钮点击监听器中播放你想要的声音。 这是更简单的方法之一。