android架子鼓怎么实现同时点击多个button同时响应操作的方法?
本人很爱玩音乐 看到ios平台很多音乐的模拟软件 吉他的~贝斯的 鼓的~~但是android平台却相当不给力 这些软件都很渣~~排名靠前的架子鼓的软件 竟然只能支持单指操作!就比如一个节奏咚次大次吧 kit(地鼓)和snare(踩镲)必须同时响起来 如果只支持单指操作的话 不能同时点击两个以上的鼓 这样模拟架子鼓的效果就太差了!!.
.
.
出于严重不满android平台音乐类软件质量的差劲 我就想自己捣鼓一个架子鼓的软件出来 造福热爱音乐的朋友们 但是遇到问题了~~~自学了android应用的开发 但是对编程毕竟还很不了解 所以表述的当中可能有纰漏 愿谅解 但是我有热情和激情 希望在广大各路神仙指点迷津 能过有所进步!!
.
.
.
现在我的ui布局、界面设计、按钮元素等全部都已经实现了 架子鼓上的每个部件我都用一个button来显示 然后用button的setOnClickListener的方法来实现点击相应的按钮来发出声音
但是问题来了 这样一次只能点击一个button 不能同时点击多个button来发声 我就郁闷了
于是在网上查找了很多资料 都是关于重写onTouchEvent的做法来支持多指触控操作 最贴近我想要的效果的方法就是 getX()getY()得到点击的坐标然后调用该坐标的view(button)的方法
这样就可以支持多指操作……
.
.
但是我怎么都不知道该如何写 所以在此请教各路大神了 求可以同时点击两个以上的button并且button同时响应(同时发出声音)的方法和思路 谢谢各位了
另外请教soundpool怎么会有延迟?感觉点击和发出声音总是不够同步?soundpool的播放优先已经最高了 音频文件也就只有100k 这么小android还吃不消? 这个怎么破?? 多指触控 button android ontouchevent 多指操作 --------------------编程问答-------------------- 这个要用多点触摸了。复写一个Button,或者View, 在onTounch事件中捕获多点触摸。 --------------------编程问答--------------------
大哥首先谢谢你的回复!!
我照着你的意思试着解决我的问题 但是我没有重写我的button 我让我Activity继承OnTouchListener接口然后重写了OnTouch的方法 但是多触摸还是出问题了 还是不能同时点两个以上的按钮
(我自己的思考:绝对的同时点两个button是不可能的 但是当我尝试相对的同时点两个button的时候 宏观上市同时点击 但细微处必定会有一个button先被点击到 接着才是第二个button被点到 当点击了第一个button后没有什么问题 但就是第二个button接着点的时候 竟然响应到第一个button的事件上了!!我也想了 是不是第一个button点击后产生了v.getID1 但是当第二个button点下时并有产生应有的v.getID2 而还是v.getID1 因为我此时点击第二个button时的状态 第一个button还没有离开 我想请教怎么一点击完立刻清理刷新v.getID 我不知道怎么解决 或者大哥请教新的思路 谢谢了 )
求指教 初学者很多地方不懂 真的很感动你能回答我的疑惑 如果你能抽出时间看看我的代码我会更加感谢的
package com.powerdrum;--------------------编程问答-------------------- public boolean onTouchEvent(MotionEvent event) {
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class LeftMainActivity extends Activity implements OnTouchListener {
Button kick ;
Button snare ;
Button hihat_close;
Button hihat_open;
Button hitom;
Button midtom;
Button lotom;
Button ride;
Button crash;
Button splash;
Animation shake;
SoundPool soundPool;
Object kit;
HashMap<Integer, Integer> soundPoolMap;
@SuppressLint("UseSparseArrays")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_main);
shake = AnimationUtils.loadAnimation(this, R.anim.shake);//使鼓抖动的动画
soundPool = new SoundPool(11, AudioManager.STREAM_MUSIC, 5); //音频播放器初始化
soundPoolMap= new HashMap<Integer, Integer>();
RedySound();//加载音频
RedyButton();//初始化按钮
RedyButtonTouch();//添加按钮触摸监听
}
public void RedySound(){
soundPoolMap.put(1, soundPool.load(this, R.raw.r_kick, 1));
soundPoolMap.put(2, soundPool.load(this, R.raw.r_snare, 1));
soundPoolMap.put(3, soundPool.load(this, R.raw.r__hihat_closed, 1));
soundPoolMap.put(4, soundPool.load(this, R.raw.r_hihat_open, 1));
soundPoolMap.put(5, soundPool.load(this, R.raw.r_hitom, 1));
soundPoolMap.put(6, soundPool.load(this, R.raw.r_midtom, 1));
soundPoolMap.put(7, soundPool.load(this, R.raw.r_lotom, 1));
soundPoolMap.put(8, soundPool.load(this, R.raw.r_ride, 1));
soundPoolMap.put(9, soundPool.load(this, R.raw.r_crash, 1));
soundPoolMap.put(10, soundPool.load(this, R.raw.r_splash, 1));
soundPoolMap.put(11, soundPool.load(this, R.raw.r_ride_bell, 1));
}
public void RedyButton(){
kick = (Button) findViewById(R.id.kick_bt);
snare = (Button) findViewById(R.id.snare_bt);
hihat_close= (Button) findViewById(R.id.hihat_close_bt);
hihat_open= (Button) findViewById(R.id.hihat_open_bt);
hitom= (Button) findViewById(R.id.hitom_bt);
midtom= (Button) findViewById(R.id.midtom);
lotom= (Button) findViewById(R.id.lotom_bt);
ride= (Button) findViewById(R.id.ride_bt);
crash= (Button) findViewById(R.id.crash_bt);
splash= (Button) findViewById(R.id.splash_bt);
}
public void RedyButtonTouch(){
kick.setOnTouchListener(this);
snare.setOnTouchListener(this);
hihat_close.setOnTouchListener(this);
hihat_open.setOnTouchListener(this);
hitom.setOnTouchListener(this);
midtom.setOnTouchListener(this);
lotom.setOnTouchListener(this);
ride.setOnTouchListener(this);
crash.setOnTouchListener(this);
splash.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
if(v.getId()==R.id.kick_bt){
kick.startAnimation(shake);
soundPool.play(soundPoolMap.get(1), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.snare_bt){
snare.startAnimation(shake);
soundPool.play(soundPoolMap.get(2), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hihat_close_bt){
hihat_close.startAnimation(shake);
soundPool.play(soundPoolMap.get(3), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hihat_open_bt){
hihat_open.startAnimation(shake);
soundPool.play(soundPoolMap.get(4), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hitom_bt){
hitom.startAnimation(shake);
soundPool.play(soundPoolMap.get(5), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.midtom){
midtom.startAnimation(shake);
soundPool.play(soundPoolMap.get(6), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.lotom_bt){
lotom.startAnimation(shake);
soundPool.play(soundPoolMap.get(7), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.ride_bt){
ride.startAnimation(shake);
soundPool.play(soundPoolMap.get(8), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.crash_bt){
crash.startAnimation(shake);
soundPool.play(soundPoolMap.get(9), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.splash_bt){
splash.startAnimation(shake);
soundPool.play(soundPoolMap.get(10), 1, 1, 1, 0, 1);
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
if(v.getId()==R.id.kick_bt){
kick.startAnimation(shake);
soundPool.play(soundPoolMap.get(1), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.snare_bt){
snare.startAnimation(shake);
soundPool.play(soundPoolMap.get(2), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hihat_close_bt){
hihat_close.startAnimation(shake);
soundPool.play(soundPoolMap.get(3), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hihat_open_bt){
hihat_open.startAnimation(shake);
soundPool.play(soundPoolMap.get(4), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.hitom_bt){
hitom.startAnimation(shake);
soundPool.play(soundPoolMap.get(5), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.midtom){
midtom.startAnimation(shake);
soundPool.play(soundPoolMap.get(6), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.lotom_bt){
lotom.startAnimation(shake);
soundPool.play(soundPoolMap.get(7), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.ride_bt){
ride.startAnimation(shake);
soundPool.play(soundPoolMap.get(8), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.crash_bt){
crash.startAnimation(shake);
soundPool.play(soundPoolMap.get(9), 1, 1, 1, 0, 1);
}
if(v.getId()==R.id.splash_bt){
splash.startAnimation(shake);
soundPool.play(soundPoolMap.get(10), 1, 1, 1, 0, 1);
}
break;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.left_main, menu);
return true;
}
}
int pointerCt = event.getPointerCount(); //获取多点触摸的数量,
if(pointerCt >= 2){ //假如2个以上都点击了。就响应你的鼓声。
}
}
用多个按钮去控制较为麻烦。 --------------------编程问答-------------------- 不如自己写view,按钮直接画上去,view是可以获取多个点击点的坐标的,然后判断一下坐标是不是在按钮的范围内。可以使用一个数组表示按钮的状态,这样也可以处理长按了。这个比你的方法要简单,不知道能不能帮到你。 --------------------编程问答--------------------
大哥感谢你的点醒!但是我不是很明白 我想过用点击范围的方法 但是要怎么重写button啊 我不知道该怎么下手 我知道view有getx gety的方法 event也有getx gety的方法 但真的不知道怎么办
你有空能写几段伪代码么 辛苦你了 谢谢 我学编程不久的
补充:移动开发 , Android