touch several buttons with a finger

1

I am wanting to make a piano in parts, how could I do the effect of when I play a key and dezlizing to the left or right (without lifting my finger) the other notes sound?

Almost use the same keypad code. Reading out there would I have to put another if with the event of MotionEvent.ACTION_MOVE below the MotionEvent.ACTION_DOWN ? and my other question is that if I do not have to use MultiTouch instead of ontouch, since it would be logical or not?

public class MainActivity extends AppCompatActivity {

    ImageButton tecla1,tecla2,tecla3,tecla4;
    private SoundPool soundPool;
    private int son_tecla1, son_tecla2, son_tecla3, son_tecla4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tecla1 = (ImageButton)findViewById(R.id.ib1);
        tecla2 = (ImageButton)findViewById(R.id.ib2);
        tecla3 = (ImageButton)findViewById(R.id.ib3);
        tecla4 = (ImageButton)findViewById(R.id.ib4);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool.Builder().setMaxStreams(4).build();
        }else {
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        }

        son_tecla1=soundPool.load(this, R. raw.t1,1);
        son_tecla2=soundPool.load(this, R. raw.t2,1);
        son_tecla3=soundPool.load(this, R. raw.t3,1);
        son_tecla4=soundPool.load(this, R. raw.t4,1);

        tecla1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla1,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla1);
                }
                return false;
            }
        });


        tecla2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla2,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla2);
                }
                return false;
            }
        });


        tecla3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla3,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla3);
                }
                return false;
            }
        });


        tecla4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla4,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla4);
                }
                return false;
            }
        });
    
asked by luis 14.02.2018 в 19:28
source

1 answer

1

As you comment, in this case you must implement multi touch event detection:

link

  

How it could do the effect of when a key is touched and dezlized   to the left or right

According to the image you must map the position of the keys (images) and by detecting the coordinate on the X axis you could know which key you are touching:

public boolean onTouchEvent(MotionEvent event) {
    ....
    //Obtiene posición.
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);
}
    
answered by 14.02.2018 в 20:47