开发者

Android clickable areas in image [closed]

开发者 https://www.devze.com 2023-04-05 11:18 出处:网络
It&#开发者_JAVA百科39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
It&#开发者_JAVA百科39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号