簡體   English   中英

具有多點觸控和SoundPool運動事件的onTouch

[英]onTouch with Multitouch and SoundPool Motion Events

所以我正在為Android開發木琴應用程序,但遇到了問題,我不確定如何正確使用onTouch。 現在,我可以按一個圖像並播放聲音,但是不能同時播放兩個聲音,也不能將手指滑到木琴上並播放所有聲音,它只會播放所按的第一個圖像。 我知道這是由於onTouch和Multitouch引起的,但是我不確定該怎么做,而且我找不到任何相關的示例代碼,我們將為您提供任何幫助!

這是我的主要活動

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活動

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" />

希望您知道到現在為止。 仍然是我對此的看法:首先,您的第二堂課SoundPool 不是一項活動。

您必須嘗試的是不覆蓋每個按鈕的onTouch方法。


相反,在您的主要活動中,使其實現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());
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM