开发者

Different responses per defferent touch events

I finally figured out how to enable multitouch, after about a week of trial and error. I've encountered another problem, however. First of all, let me give an explanation of my app.

This is a pretty basic app. When you touch AREA_A on the screen, it will play SOUND_A. When you touch AREA_B on the screen, it will play SOUND_B. Pretty simple. I wanted this to work with multitouch, so I used an OnTouch event. I am now able to touch AREA_A and AREA_B on the screen at the same time, and get a sound out of both areas (proof that multitouch is working), but here's the problem. If I start touching AREA_A, and hold my finger there, then touch AREA_B (With my first finger still touching AREA_A), instead of hearing SOUND_B, I hear SOUND_A being played. I am baffled at why this is happening. I think things will be more clear if I paste the code, so here it is. (I went ahead and added my entire class, just so you could examine it from start to finish).

package com.tst.tanner;

import android.app.Activity;
import android.graphics.Color;
import android.media.AudioManager; 
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public class sp extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int bdsound, sdsound;
float x, y;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Set the hardware buttons to control the music
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    // Load the sound
    View v = (View) findViewById(R.id.view1);

    v.setOnTouchListener(this);

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    bdsound = soundPool.load(this, R.raw.kickdrum1, 1);
    sdsound = soundPool.load(this, R.raw.sd, 1);

}

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


    v.setBackgroundColor(Color.rgb(236,234,135));
    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:

        if (x开发者_C百科 > 1 & x < 200 & y > 1 & y < 200) {
            soundPool.play(bdsound, 10, 10, 1, 0, 1);
        }
        if (x > 1 & x < 200 & y > 200 & y < 400) {
            soundPool.play(sdsound, 10, 10, 1, 0, 1);
        }

        break;

    case MotionEvent.ACTION_POINTER_1_DOWN:

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

        break;


    case MotionEvent.ACTION_UP:


        v.setBackgroundColor(Color.BLACK);

    }

    return true;
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    soundPool.release();
    finish();
}
}

Does anyone know what I am doing wrong? Thanks in advance!


The getX() and getY() functions always return the position of the first pointer. I suspect that this is causing your problem.

When you touch your second finger to the screen in Area B, the MotionEvent is of type ACTION_DOWN, so a sound will be played (just as you intended). However, since you touched Area A first, and are still touching it, that position is the one returned by getX() and getY().

To get the positions of specific pointers try using the getX(int) and getY(int) functions. For instance, if you touch the screen with two fingers and want the position of the second finger, you would use

x2 = getX(1);
y2 = getY(1);

getX() is the same as getX(0) and getY() is the same as getY(0)

You can check out the documentation for more information.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜