Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
onTouchEvent method for the PianoView on the SSaurel's Channel
@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