黑马程序员技术交流社区

标题: Android笔记 之 旋转木马的音乐效果 [打印本页]

作者: 陈君    时间: 2014-9-16 18:41
标题: Android笔记 之 旋转木马的音乐效果
一、前言——
大家一定在百度音乐上在线听过歌,有没有注意到那个旋转唱片——
就上面那个,当音乐在播放的时候,那个光碟轮子在转,就想旋转木马一般。感觉好好玩啊。
碰巧想起前阵子做音乐播放器,哎,那这个也可以做在手机的音乐播放器上,这样就代替了进度条了。
一想到,就兴奋,于是,首先画圆形,然后放置背景图片,然后使用动画旋转。当音乐播放时,同时
开始播放圆形图片的动画,当音乐暂停时,暂停旋转;当音乐停止播放时,就停止动画,图片回到原点。

二、效果—— 三、源码——
(1)MainActivity
  1. <span style="font-size:18px;">public class MainActivity extends Activity {
  2. MediaPlayer m1;
  3. ImageView infoOperatingIV;

  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. infoOperatingIV = (ImageView) findViewById(R.id.infoOperating);

  9. Button play = (Button) findViewById(R.id.play);
  10. Button stop = (Button) findViewById(R.id.stop);

  11. play.setOnClickListener(new OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. playMusic();
  15. }
  16. });

  17. stop.setOnClickListener(new OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. stopMusic();
  21. }
  22. });

  23. }

  24. private void playMusic() {
  25. m1 = MediaPlayer.create(this, R.raw.quiet);
  26. m1.start();

  27. Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
  28. LinearInterpolator lin = new LinearInterpolator();
  29. operatingAnim.setInterpolator(lin);
  30. if (operatingAnim != null) {
  31. infoOperatingIV.startAnimation(operatingAnim);
  32. }

  33. m1.setOnCompletionListener(new OnCompletionListener() {
  34. @Override
  35. public void onCompletion(MediaPlayer mp) {
  36. mp.stop();
  37. infoOperatingIV.clearAnimation();
  38. }
  39. });
  40. }

  41. private void stopMusic() {
  42. m1.stop();
  43. infoOperatingIV.clearAnimation();
  44. }

  45. }</span><span style="font-size: 16pt;">
  46. </span>
复制代码

(2)画圆的控件,这部分代码参考了网友的。
  1. <span style="font-size:18px;">public class RoundImageView extends ImageView {
  2. private int mBorderThickness = 0;
  3. private Context mContext;
  4. private int defaultColor = 0xFFFFFFFF;
  5. // 如果只有其中一个有值,则只画一个圆形边框
  6. private int mBorderOutsideColor = 0;
  7. private int mBorderInsideColor = 0;
  8. // 控件默认长、宽
  9. private int defaultWidth = 0;
  10. private int defaultHeight = 0;

  11. public RoundImageView(Context context) {
  12. super(context);
  13. mContext = context;
  14. }

  15. public RoundImageView(Context context, AttributeSet attrs) {
  16. super(context, attrs);
  17. mContext = context;
  18. setCustomAttributes(attrs);
  19. }

  20. public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
  21. super(context, attrs, defStyle);
  22. mContext = context;
  23. setCustomAttributes(attrs);
  24. }

  25. private void setCustomAttributes(AttributeSet attrs) {
  26. TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.roundedimageview);
  27. mBorderThickness = a.getDimensionPixelSize(R.styleable.roundedimageview_border_thickness, 0);
  28. mBorderOutsideColor = a.getColor(R.styleable.roundedimageview_border_outside_color, defaultColor);
  29. mBorderInsideColor = a.getColor(R.styleable.roundedimageview_border_inside_color, defaultColor);
  30. }

  31. @Override
  32. protected void onDraw(Canvas canvas) {
  33. Drawable drawable = getDrawable();
  34. if (drawable == null) {
  35. return;
  36. }

  37. if (getWidth() == 0 || getHeight() == 0) {
  38. return;
  39. }
  40. this.measure(0, 0);
  41. if (drawable.getClass() == NinePatchDrawable.class)
  42. return;
  43. Bitmap b = ((BitmapDrawable) drawable).getBitmap();
  44. Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
  45. if (defaultWidth == 0) {
  46. defaultWidth = getWidth();

  47. }
  48. if (defaultHeight == 0) {
  49. defaultHeight = getHeight();
  50. }
  51. // 保证重新读取图片后不会因为图片大小而改变控件宽、高的大小(针对宽、高为wrap_content布局的imageview,但会导致margin无效)
  52. // if (defaultWidth != 0 && defaultHeight != 0) {
  53. // LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  54. // defaultWidth, defaultHeight);
  55. // setLayoutParams(params);
  56. // }
  57. int radius = 0;
  58. if (mBorderInsideColor != defaultColor && mBorderOutsideColor != defaultColor) {// 定义画两个边框,分别为外圆边框和内圆边框
  59. radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - 2 * mBorderThickness;
  60. // 画内圆
  61. drawCircleBorder(canvas, radius + mBorderThickness / 2, mBorderInsideColor);
  62. // 画外圆
  63. drawCircleBorder(canvas, radius + mBorderThickness + mBorderThickness / 2, mBorderOutsideColor);
  64. } else if (mBorderInsideColor != defaultColor && mBorderOutsideColor == defaultColor) {// 定义画一个边框
  65. radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - mBorderThickness;
  66. drawCircleBorder(canvas, radius + mBorderThickness / 2, mBorderInsideColor);
  67. } else if (mBorderInsideColor == defaultColor && mBorderOutsideColor != defaultColor) {// 定义画一个边框
  68. radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - mBorderThickness;
  69. drawCircleBorder(canvas, radius + mBorderThickness / 2, mBorderOutsideColor);
  70. } else {// 没有边框
  71. radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2;
  72. }
  73. Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
  74. canvas.drawBitmap(roundBitmap, defaultWidth / 2 - radius, defaultHeight / 2 - radius, null);
  75. }

  76. /**
  77. * 获取裁剪后的圆形图片
  78. *
  79. * @param radius
  80. * 半径
  81. */
  82. public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
  83. Bitmap scaledSrcBmp;
  84. int diameter = radius * 2;

  85. // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
  86. int bmpWidth = bmp.getWidth();
  87. int bmpHeight = bmp.getHeight();
  88. int squareWidth = 0, squareHeight = 0;
  89. int x = 0, y = 0;
  90. Bitmap squareBitmap;
  91. if (bmpHeight > bmpWidth) {// 高大于宽
  92. squareWidth = squareHeight = bmpWidth;
  93. x = 0;
  94. y = (bmpHeight - bmpWidth) / 2;
  95. // 截取正方形图片
  96. squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
  97. } else if (bmpHeight < bmpWidth) {// 宽大于高
  98. squareWidth = squareHeight = bmpHeight;
  99. x = (bmpWidth - bmpHeight) / 2;
  100. y = 0;
  101. squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
  102. } else {
  103. squareBitmap = bmp;
  104. }

  105. if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
  106. scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);

  107. } else {
  108. scaledSrcBmp = squareBitmap;
  109. }
  110. Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(), Config.ARGB_8888);
  111. Canvas canvas = new Canvas(output);

  112. Paint paint = new Paint();
  113. Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

  114. paint.setAntiAlias(true);
  115. paint.setFilterBitmap(true);
  116. paint.setDither(true);
  117. canvas.drawARGB(0, 0, 0, 0);
  118. canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2, paint);
  119. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  120. canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);

  121. bmp = null;
  122. squareBitmap = null;
  123. scaledSrcBmp = null;
  124. return output;
  125. }

  126. /**
  127. * 边缘画圆
  128. */
  129. private void drawCircleBorder(Canvas canvas, int radius, int color) {
  130. Paint paint = new Paint();
  131. /* 去锯齿 */
  132. paint.setAntiAlias(true);
  133. paint.setFilterBitmap(true);
  134. paint.setDither(true);
  135. paint.setColor(color);
  136. /* 设置paint的 style 为STROKE:空心 */
  137. paint.setStyle(Paint.Style.STROKE);
  138. /* 设置paint的外框宽度 */
  139. paint.setStrokeWidth(mBorderThickness);
  140. canvas.drawCircle(defaultWidth / 2, defaultHeight / 2, radius, paint);
  141. }

  142. }</span><span style="font-size: 16pt;">
  143. </span>
复制代码









欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2