我有制作Android应用程序音乐乐器的项目,应用程序像钢琴一样工作,如果我点击带有SingleTouch的ImageButton他的播放声音但我希望应用程序可以使用MultiTouch,我不知道在我的代码中实现?
以下是我的代码,
private int mSoundID;
private int mSoundID2;
private int mSoundID3;
private int mSoundID4;
private int mSoundID5;
private int mSoundID6;
private int mSoundID7;
private int mSoundID8;
private int mSoundID9;
private int mSoundID10;
private int mSoundID11;
private int mSoundID12;
private int mSoundID13;
private int mSoundID14;
private int mSoundID15;
private int mSoundID16;
private boolean mSoundLoaded = true;
private float mVolume = 0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.talempong);
View view = findViewById(R.id.imagebutton1);
view.setOnTouchListener(this);
View view2 = findViewById(R.id.imagebutton2);
view2.setOnTouchListener(this);
View view3 = findViewById(R.id.imagebutton3);
view3.setOnTouchListener(this);
View view4 = findViewById(R.id.imagebutton4);
view4.setOnTouchListener(this);
View view5 = findViewById(R.id.imagebutton5);
view5.setOnTouchListener(this);
View view6 = findViewById(R.id.imagebutton6);
view6.setOnTouchListener(this);
View view7 = findViewById(R.id.imagebutton7);
view7.setOnTouchListener(this);
View view8 = findViewById(R.id.imagebutton8);
view8.setOnTouchListener(this);
View view9 = findViewById(R.id.imagebutton9);
view9.setOnTouchListener(this);
View view10 = findViewById(R.id.imagebutton10);
view10.setOnTouchListener(this);
View view11 = findViewById(R.id.imagebutton11);
view11.setOnTouchListener(this);
View view12 = findViewById(R.id.imagebutton12);
view12.setOnTouchListener(this);
View view13 = findViewById(R.id.imagebutton13);
view13.setOnTouchListener(this);
View view14 = findViewById(R.id.imagebutton14);
view14.setOnTouchListener(this);
View view15 = findViewById(R.id.imagebutton15);
view15.setOnTouchListener(this);
View view16 = findViewById(R.id.imagebutton16);
view16.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
{
mSoundLoaded = true;
}
}
});
mSoundID = mSoundPool.load(this, R.raw.doo, 1);
mSoundID2 = mSoundPool.load(this, R.raw.re, 1);
mSoundID3 = mSoundPool.load(this, R.raw.mi, 1);
mSoundID4 = mSoundPool.load(this, R.raw.fa, 1);
mSoundID5 = mSoundPool.load(this, R.raw.sol, 1);
mSoundID6 = mSoundPool.load(this, R.raw.la, 1);
mSoundID7 = mSoundPool.load(this, R.raw.si, 1);
mSoundID8 = mSoundPool.load(this, R.raw.do_tinggi, 1);
mSoundID9 = mSoundPool.load(this, R.raw.sol_rendah, 1);
mSoundID10 = mSoundPool.load(this, R.raw.la_rendah, 1);
mSoundID11 = mSoundPool.load(this, R.raw.si_rendah, 1);
mSoundID12 = mSoundPool.load(this, R.raw.re_kres, 1);
mSoundID13 = mSoundPool.load(this, R.raw.fa_kres, 1);
mSoundID14 = mSoundPool.load(this, R.raw.sol_kres, 1);
mSoundID15 = mSoundPool.load(this, R.raw.la_kres, 1);
mSoundID16 = mSoundPool.load(this, R.raw.re_tinggi, 1);
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVolume = actualVolume / maxVolume;
}
//single touch
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ){
//multitouch
// Is the sound loaded already?
if (mSoundLoaded) {
if (v.getId() == R.id.imagebutton1) {
playSound(mSoundID);
Log.e("Test", "Played mSoundID");
} else if (v.getId() == R.id.imagebutton2) {
playSound(mSoundID2);
Log.e("Test", "Played mSoundID2");
} else if (v.getId() == R.id.imagebutton3) {
playSound(mSoundID3);
Log.e("Test", "Played mSoundID3");
} else if (v.getId() == R.id.imagebutton4) {
playSound(mSoundID4);
Log.e("Test", "Played mSoundID4");
} else if (v.getId() == R.id.imagebutton5) {
playSound(mSoundID5);
Log.e("Test", "Played mSoundID5");
} else if (v.getId() == R.id.imagebutton6) {
playSound(mSoundID6);
Log.e("Test", "Played mSoundID6");
} else if (v.getId() == R.id.imagebutton7) {
playSound(mSoundID7);
Log.e("Test", "Played mSoundID7");
} else if (v.getId() == R.id.imagebutton8) {
playSound(mSoundID8);
Log.e("Test", "Played mSoundID8");
} else if (v.getId() == R.id.imagebutton9) {
playSound(mSoundID9);
Log.e("Test", "Played mSoundID9");
} else if (v.getId() == R.id.imagebutton10) {
playSound(mSoundID10);
Log.e("Test", "Played mSoundID10");
} else if (v.getId() == R.id.imagebutton11) {
playSound(mSoundID11);
Log.e("Test", "Played mSoundID11");
} else if (v.getId() == R.id.imagebutton12) {
playSound(mSoundID12);
Log.e("Test", "Played mSoundID12");
} else if (v.getId() == R.id.imagebutton13) {
playSound(mSoundID13);
Log.e("Test", "Played mSoundID13");
} else if (v.getId() == R.id.imagebutton14) {
playSound(mSoundID14);
Log.e("Test", "Played mSoundID14");
} else if (v.getId() == R.id.imagebutton15) {
playSound(mSoundID15);
Log.e("Test", "Played mSoundID15");
} else if (v.getId() == R.id.imagebutton16) {
playSound(mSoundID16);
Log.e("Test", "Played mSoundID16");
}
}
}
return false;
}
private void playSound(int soundId) {
mSoundPool.play(soundId, mVolume, mVolume, 1, 0, 1f);
}
}
答案 0 :(得分:1)
好吧,我做了一些这样的事情,我强迫在不支持多点触控的设备上进行多点触控我的实现是我创建的数组将保存你点击的键并循环每个他们在数组中发出声音,不要忘记在最后
中清空数组 View view = findViewById(R.id.imagebutton1);
view.setOnTouchListener(this);
view.setTag(1);
View view2 = findViewById(R.id.imagebutton2);
view2.setOnTouchListener(this);
view2.setTag(2);
依旧为每个视图添加标签
制作globe数组
boolean touchedViews [] = new boolean [viewsNumber];
并在触摸事件中生成以下代码
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction()
synchronized (this) {
switch (action) {
case MotionEvent.ACTION_DOWN:// when you touch down
touchedViews[ (Integer) v.getTag()] = true;
break ;
case case MotionEvent.ACTION_UP: // when you touch up
break;
}
}
for(int i=0 ; i < touchedViews.length ; i++){
// play sound of each view with true touch
}
}
答案 1 :(得分:0)
您应该阅读Handling Multi-Touch Gestures
Android work multitouch button似乎也解决了同样的问题。
答案 2 :(得分:0)
注意:某些变量不会在我的代码中声明,但它的名称对于您添加它们非常明显 声明
首先,您将实现自定义视图,该视图将保存钢琴键盘并添加您自己风格的所有键并加载声音并声明油漆
** protected void onLayout(boolean changed, int left, int top, int right, int bottom)**
并使用绘画方法绘制你的钥匙
protected void onDraw(Canvas canvas)
这里是Ontouch事件的实现,它支持多重触摸键
@Override
public boolean onTouchEvent(MotionEvent event) {
int pointerCount = event.getPointerCount();
int cappedPointerCount = pointerCount > MAX_FINGERS ? MAX_FINGERS : pointerCount;
int actionIndex = event.getActionIndex();
int action = event.getActionMasked();
int id = event.getPointerId(actionIndex);
if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) && id < MAX_FINGERS) {
mFingerPoints[id] = new Point((int) event.getX(actionIndex), (int) event.getY(actionIndex));
} else if ((action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_UP) && id < MAX_FINGERS) {
mFingerPoints[id] = null;
invalidateKey(mFingerTones[id]);
mFingerTones[id] = -1;
}
for (int i = 0; i < cappedPointerCount; i++) {
int index = event.findPointerIndex(i);
if (mFingerPoints[i] != null && index != -1) {
mFingerPoints[i].set((int) event.getX(index), (int) event.getY(index));
int tone = getToneForPoint(mFingerPoints[i]);
if (tone != mFingerTones[i] && tone != -1) {
invalidateKey(mFingerTones[i]);
mFingerTones[i] = tone;
invalidateKey(mFingerTones[i]);
if (!isKeyDown(i)) {
int poolIndex = mToneToIndexMap.get(mFingerTones[i]);
event.getPointerCoords(index, mPointerCoords);
float volume = mPointerCoords.getAxisValue(MotionEvent.AXIS_PRESSURE);
volume = volume > 1f ? 1f : volume;
mSoundPool.play(poolIndex, volume, volume, 0, 0, 1f);
}
}
}
}
return true;
}