java - onTouch 与多点触控和 SoundPool 运动事件

标签 java android touch-event ontouchlistener soundpool

所以我正在为 android 制作一个木琴应用程序,但我遇到了一个问题,我不确定如何正确使用 onTouch。现在我可以按下一个图像并播放声音,但我不能同时播放两个声音,也不能将手指向下滑动木琴并播放所有声音,它只会播放按下的第一个图像。我知道这是因为 onTouch 和 Multitouch,但我不确定该怎么做,而且我找不到任何相关的示例代码,感谢您的帮助!

这是我的主要 Activity

public class Xylophone extends Activity {
private Player mSoundManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xylophone);
    mSoundManager = new Player();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.note01);
    mSoundManager.addSound(2, R.raw.note02);
    mSoundManager.addSound(3, R.raw.note03);
    mSoundManager.addSound(4, R.raw.note04);
    mSoundManager.addSound(5, R.raw.note05);
    mSoundManager.addSound(6, R.raw.note06);
    mSoundManager.addSound(7, R.raw.note07);
    mSoundManager.addSound(8, R.raw.note08);

    ImageView img01 = (ImageView) findViewById(R.id.imageView1);
    img01.setOnTouchListener(new OnTouchListener() {    

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mSoundManager.playSound(1);
            return false;
        }           
    });

    ImageView img02 = (ImageView) findViewById(R.id.imageView2);
    img02.setOnTouchListener(new OnTouchListener() {    

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mSoundManager.playSound(2);
            return false;
        }           
    });

这是我的 SoundPool Activity

public class Player {

private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;


public Player()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)
{
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}

public void playSound(int index) { 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
}

public void playLoopedSound(int index) { 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
}

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:cropToPadding="false"
        android:scaleType="fitXY"
        android:src="@drawable/button01" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="fitXY"
        android:src="@drawable/button02" />

最佳答案

希望你到现在为止都明白了。这仍然是我的镜头: 首先,您的第二个类(class) SoundPool 不是 Activity 。

您必须尝试的是不要覆盖每个按钮的 onTouch 方法。


相反,在您的主要 Activity 中,让它实现 OnTouchListener。

当然,您需要实现/覆盖 OnTouchListener 的方法。所以现在,如果您将主视图和/或每个按钮(自己看看哪个有效!)注册到 OnTouchListener,您将获得所有触摸(以及多点触摸)。

看看 MotionEvent上课了解更多详情

public class Xylophone extends Activity implements onTouchLister{
    public static final String TAG = "Xylophone";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        /*Either have your parent view register THIS (Xylophone) as its listener, or have each button register THIS(Xylophone) as its listener.*/
        yourParentView.setOnTouchListener(this);
        //or
        img02.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG, "ID:" + Integer.toString(v.getId()));
        Log.i(TAG, "Pointer count: " + event.getPointerCount());        
        Log.i(TAG, "Action: " + event.getActionMasked());
        Log.i(TAG, "Action index: " + event.getActionIndex());
    }

关于java - onTouch 与多点触控和 SoundPool 运动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221970/

相关文章:

Android:垂直 Recyclerview 内的水平 Recyclerview

ios - 计算旋转速度自然旋转view iOS

android - 使用 SurfaceView 和 4 个 Canvas 图像如何找到哪个图像被触摸了?

java - 访问数据库的最佳方式

java - org.springframework.dao.InvalidDataAccessApiUsageException

java - 无法从 Android 中的 Firebase 存储获取下载网址

JUnit 测试中的 java.util.ConcurrentModificationException

java - double 与 Double 有何不同?

java - 这种确定 gem 棋获胜者的方法不起作用

java - fragment 必须是公共(public)静态类才能从实例状态正确地重新创建