Android clickable areas in image [closed]
I have an image off an human and i want to play different sounds depending on if right or left hand is clicked on the image, and i will maby want to add a third sound if head is clicked, is there a simple way to do this?
I think the best way is by using an OnTouchEvent, and using getX() and getY(). This might help explain it a bit better
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;
Also, you should implement the OnTouchListener. Obviously, this is covering only the OnTouch portion of the code, and the rest should be pretty self explanatory (assuming you got the sounds and view down). Let me know if this helps. God Bless!
set the human image as a background of the layout. place transparent buttons on the human images for eg: on head , left/right hand and add the button click listener for transparent buttons. In the button click listener play the sound you wanted too. This is one of the simpler approach.
精彩评论