Created
March 20, 2018 10:35
-
-
Save ssaurel/955f2267f9fbdc6dd70ab9ab36100362 to your computer and use it in GitHub Desktop.
onTouchEvent method for the PianoView on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
int action = event.getAction(); | |
boolean isDownAction = action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE; | |
for (int touchIndex = 0; touchIndex < event.getPointerCount(); touchIndex++) { | |
float x = event.getX(touchIndex); | |
float y = event.getY(touchIndex); | |
Key k = keyForCoords(x,y); | |
if (k != null) { | |
k.down = isDownAction; | |
} | |
} | |
ArrayList<Key> tmp = new ArrayList<>(whites); | |
tmp.addAll(blacks); | |
for (Key k : tmp) { | |
if (k.down) { | |
if (!soundPlayer.isNotePlaying(k.sound)) { | |
soundPlayer.playNote(k.sound); | |
invalidate(); | |
} else { | |
releaseKey(k); | |
} | |
} else { | |
soundPlayer.stopNote(k.sound); | |
releaseKey(k); | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment