android – Touch Processing (guitar strings)

Question:

I'm practicing, I decided to make something like a guitar, but I can't figure out how to properly handle pressing.

There are 6 strings and it is necessary when the finger touches the upper string and goes down (hooking the rest) so that animations are applied to all of them, in turn.

I could do this only with a single touch, if you press the strings in turn, then everything works, if you put your finger on the screen, then no.

 @Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        switch (v.getId()) {
            case R.id.string1:
                str1.startAnimation(touch_string);
                return false;
            case R.id.string2:
                str2.startAnimation(touch_string);
                return false;
            case R.id.string3:
                str3.startAnimation(touch_string);
                return false;
            case R.id.string4:
                str4.startAnimation(touch_string);
                return false;
            case R.id.string5:
                str5.startAnimation(touch_string);
                return false;
            case R.id.string6:
                str6.startAnimation(touch_string);
                return false;
        }
    }
    return false;
}

Played with various MotionEvent actions, but nothing worked, how can I implement it correctly?

I would be grateful for your answer!

Answer:

Because in this case, you need to track not ACTION_DOWN , but ACTION_MOVE . Determine the area you want, hang the listener and process as needed. For example, take a position and, if they match in certain coordinates / range of coordinates, perform the action

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_MOVE:
                x = event.getRawX();
                y = event.getRawY();
                //TODO обработать нужные места 
                ...
                break;
        }
        return true;
    }
Scroll to Top