开源PianoView原理研究
https://gitee.com/h128/PianoView
处理键盘按下逻辑部分代码:PianoView.java
private void handleWhiteKeyDown(int which, MotionEvent event, PianoKey key) { key.getKeyDrawable().setState(new int[] { android.R.attr.state_pressed }); key.setPressed(true); if (event != null) { key.setFingerID(event.getPointerId(which)); } pressedKeys.add(key); invalidate(key.getKeyDrawable().getBounds()); //utils.playMusic(key); if (pianoListener != null) { pianoListener.onPianoClick(key.getType(), key.getVoice(), key.getGroup(), key.getPositionOfGroup()); } } /** * 处理黑键点击 * * @param which 那个触摸点 * @param event 事件 * @param key 钢琴按键 */ private void handleBlackKeyDown(int which, MotionEvent event, PianoKey key) { key.getKeyDrawable().setState(new int[] { android.R.attr.state_pressed }); key.setPressed(true); if (event != null) { key.setFingerID(event.getPointerId(which)); } pressedKeys.add(key); invalidate(key.getKeyDrawable().getBounds()); //utils.playMusic(key); if (pianoListener != null) { pianoListener.onPianoClick(key.getType(), key.getVoice(), key.getGroup(), key.getPositionOfGroup()); } }
钢琴按下监听
OnPianoListener
/** * 点击钢琴键 * * @param type 钢琴键类型(黑、白) * @param voice 钢琴音色(DO,RE,MI,FA,SO,LA,SI) * @param group 钢琴键所在组(白:9组;黑:7组) * @param positionOfGroup 钢琴在组内位置 */ void onPianoClick(Piano.PianoKeyType type, Piano.PianoVoice voice, int group, int positionOfGroup); }
钢琴键类型和音高定义
public static enum PianoKeyType { @SerializedName("0") BLACK(0), @SerializedName("1") WHITE(1); private int value; private PianoKeyType(int value) { this.value = value; } public int getValue() { return this.value; } public String toString() { return "PianoKeyType{value=" + this.value + '}'; } } public static enum PianoVoice { DO, RE, MI, FA, SO, LA, SI;
浙公网安备 33010602011771号