/src/com/android/musicvis/vis1/Visualization1.java

https://github.com/MIPS/packages-wallpapers-MusicVisualization · Java · 186 lines · 139 code · 29 blank · 18 comment · 13 complexity · c5f08b085b821a5b3c59b2e81919a38a MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.musicvis.vis1;
  17. import android.graphics.Canvas;
  18. import android.graphics.Paint;
  19. import android.graphics.Rect;
  20. import android.media.MediaPlayer;
  21. import android.os.Handler;
  22. import android.os.SystemClock;
  23. import android.service.wallpaper.WallpaperService;
  24. import android.util.Log;
  25. import android.view.MotionEvent;
  26. import android.view.SurfaceHolder;
  27. public class Visualization1 extends WallpaperService {
  28. private final Handler mHandler = new Handler();
  29. @Override
  30. public void onCreate() {
  31. super.onCreate();
  32. }
  33. @Override
  34. public void onDestroy() {
  35. super.onDestroy();
  36. }
  37. @Override
  38. public Engine onCreateEngine() {
  39. return new CubeEngine();
  40. }
  41. class CubeEngine extends Engine {
  42. private final Paint mPaint = new Paint();
  43. private float mOffset;
  44. private float mTouchX = -1;
  45. private float mTouchY = -1;
  46. private long mStartTime;
  47. private int mWidth;
  48. private float mCenterX;
  49. private float mCenterY;
  50. private short [] mAudioData = new short[1024];
  51. private final Runnable mDrawCube = new Runnable() {
  52. public void run() {
  53. drawFrame();
  54. }
  55. };
  56. private boolean mVisible;
  57. CubeEngine() {
  58. }
  59. @Override
  60. public void onCreate(SurfaceHolder surfaceHolder) {
  61. super.onCreate(surfaceHolder);
  62. final Paint paint = mPaint;
  63. paint.setColor(0xffffffff);
  64. paint.setAntiAlias(true);
  65. paint.setStrokeWidth(2);
  66. paint.setStrokeCap(Paint.Cap.ROUND);
  67. paint.setStyle(Paint.Style.STROKE);
  68. mStartTime = SystemClock.elapsedRealtime();
  69. }
  70. @Override
  71. public void onDestroy() {
  72. super.onDestroy();
  73. mHandler.removeCallbacks(mDrawCube);
  74. }
  75. @Override
  76. public void onVisibilityChanged(boolean visible) {
  77. mVisible = visible;
  78. if (visible) {
  79. drawFrame();
  80. } else {
  81. mHandler.removeCallbacks(mDrawCube);
  82. }
  83. }
  84. @Override
  85. public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  86. super.onSurfaceChanged(holder, format, width, height);
  87. mWidth = width;
  88. mCenterX = width/2.0f;
  89. mCenterY = height/2.0f;
  90. drawFrame();
  91. }
  92. @Override
  93. public void onSurfaceCreated(SurfaceHolder holder) {
  94. super.onSurfaceCreated(holder);
  95. }
  96. @Override
  97. public void onSurfaceDestroyed(SurfaceHolder holder) {
  98. super.onSurfaceDestroyed(holder);
  99. mVisible = false;
  100. mHandler.removeCallbacks(mDrawCube);
  101. }
  102. @Override
  103. public void onOffsetsChanged(float xOffset, float yOffset,
  104. float xStep, float yStep, int xPixels, int yPixels) {
  105. mOffset = xOffset;
  106. drawFrame();
  107. }
  108. @Override
  109. public void onTouchEvent(MotionEvent event) {
  110. // touch events don't actually work for wallpapers, but if they did,
  111. // we'd be using them to draw a circle around the touch point
  112. if (event.getAction() == MotionEvent.ACTION_MOVE) {
  113. mTouchX = event.getX();
  114. mTouchY = event.getY();
  115. } else {
  116. mTouchX = -1;
  117. mTouchY = -1;
  118. }
  119. super.onTouchEvent(event);
  120. }
  121. void drawFrame() {
  122. final SurfaceHolder holder = getSurfaceHolder();
  123. final Rect frame = holder.getSurfaceFrame();
  124. final int width = frame.width();
  125. final int height = frame.height();
  126. Canvas c = null;
  127. try {
  128. c = holder.lockCanvas();
  129. if (c != null) {
  130. // draw something
  131. drawCube(c);
  132. drawTouchPoint(c);
  133. }
  134. } finally {
  135. if (c != null) holder.unlockCanvasAndPost(c);
  136. }
  137. mHandler.removeCallbacks(mDrawCube);
  138. if (mVisible) {
  139. mHandler.postDelayed(mDrawCube, 1000 / 25);
  140. }
  141. }
  142. void drawCube(Canvas c) {
  143. c.save();
  144. c.drawColor(0xff000000);
  145. MediaPlayer.snoop(mAudioData, 0);
  146. for (int i = 0; i < mWidth; i++) {
  147. c.drawPoint(i, mCenterY + mAudioData[i] / 256, mPaint);
  148. }
  149. c.restore();
  150. }
  151. void drawTouchPoint(Canvas c) {
  152. if (mTouchX >=0 && mTouchY >= 0) {
  153. c.drawCircle(mTouchX, mTouchY, 50, mPaint);
  154. }
  155. }
  156. }
  157. }