Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2. LinearLayout pianoKeysContainer;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  8. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  9. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  10.  
  11. pianoKeysContainer = (LinearLayout) findViewById(R.id.key_container);
  12. pianoKeysContainer.setOnTouchListener(onYourViewTouchListener);
  13.  
  14. }
  15.  
  16.  
  17. //Here we load the view positions after render it and fill the array with the positions
  18. private List<Integer> positionsLeft_whiteKeys = new ArrayList<Integer>();
  19. private List<Integer> positionsRight_whiteKeys = new ArrayList<Integer>();
  20.  
  21. public void onWindowFocusChanged(boolean hasFocus)
  22. {
  23. super.onWindowFocusChanged(hasFocus);
  24.  
  25. for (int i = 0; i < pianoKeysContainer.getChildCount(); i++)
  26. {
  27. //positionsLeft_whiteKeys holds the start x of each view.
  28. positionsLeft_whiteKeys.add(pianoKeysContainer.getChildAt(i).getLeft());
  29. //positionsRight_whiteKeys holds the end x of each view.
  30. positionsRight_whiteKeys.add(pianoKeysContainer.getChildAt(i).getRight());
  31. }
  32. }
  33.  
  34. public View.OnTouchListener onYourViewTouchListener = new View.OnTouchListener()
  35. {
  36. float positionX;
  37. FrameLayout pianoKey;
  38. FrameLayout lastPlayedKey;
  39. ArrayList<FrameLayout> pressedKeys = new ArrayList<FrameLayout>();
  40.  
  41. @Override
  42. public boolean onTouch(View view, MotionEvent motionEvent)
  43. {
  44.  
  45. positionX = motionEvent.getX();
  46.  
  47. float pitch;
  48.  
  49. //Looping on the child of the layout which contains the piano keys
  50. for (int x = 0; x < ((LinearLayout) view).getChildCount(); x++)
  51. {
  52. // Calculating the pitch to get good chords
  53. pitch = (float) Math.pow(Math.pow(2.0, 1 / 12.0), (float) x);
  54.  
  55. pianoKey = (FrameLayout) ((LinearLayout) view).getChildAt(x);
  56.  
  57. if (positionsLeft_whiteKeys.size() >= 0 && positionsRight_whiteKeys.size() >= 0)
  58. {
  59. if (positionX > positionsLeft_whiteKeys.get(x) && positionX < positionsRight_whiteKeys.get(x))
  60. {
  61. pianoKey = (FrameLayout) ((LinearLayout) view).getChildAt(x);
  62.  
  63. if (pianoKey != null)
  64. {
  65. pianoKey.setBackgroundResource(R.drawable.dinger14);
  66. pressedKeys.add(pianoKey);
  67. }
  68. if (lastPlayedKey != pianoKey)
  69. playKey(pitch);
  70.  
  71. lastPlayedKey = pianoKey;
  72. break;
  73. }
  74.  
  75. if (lastPlayedKey != null)
  76. {
  77. pianoKey.setBackgroundResource(R.drawable.dinger14);
  78. lastPlayedKey.setBackgroundResource(R.drawable.dinger14);
  79.  
  80. }
  81. }
  82. }
  83.  
  84. if (motionEvent.getAction() == MotionEvent.ACTION_UP)
  85. {
  86. lastPlayedKey = null;
  87.  
  88. for (FrameLayout pressedKey : pressedKeys)
  89. {
  90. pressedKey.setBackgroundResource(R.drawable.dinger14);
  91. }
  92.  
  93.  
  94. }
  95.  
  96. return false;
  97. }
  98. };
  99.  
  100.  
  101. //This is sound play method
  102. SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 1);
  103. public void playKey(final float pitch)
  104. {
  105.  
  106. //here you should store your piano sound at res/raw then load it
  107. sp.load(this, R.raw.chitare3, 1);
  108.  
  109. sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()
  110. {
  111. @Override
  112. public void onLoadComplete(SoundPool soundPool, int i, int i2)
  113. {
  114. soundPool.play(i, 0.99f, 0.99f, 1, 0, pitch);
  115. }
  116. });
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement