欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现旋转木马的音乐效果

程序员文章站 2024-03-31 18:11:10
一、百度在线音乐旋转木马效果 就上面那个,当音乐在播放的时候,那个光碟*在转,就想旋转木马一般。感觉好好玩啊。 碰巧想起前阵子做音乐播放器,哎,那这个也可以做在手...

一、百度在线音乐旋转木马效果

Android 实现旋转木马的音乐效果

就上面那个,当音乐在播放的时候,那个光碟*在转,就想旋转木马一般。感觉好好玩啊。
碰巧想起前阵子做音乐播放器,哎,那这个也可以做在手机的音乐播放器上,这样就代替了进度条了。
一想到,就兴奋,于是,首先画圆形,然后放置背景图片,然后使用动画旋转。当音乐播放时,同时
开始播放圆形图片的动画,当音乐暂停时,暂停旋转;当音乐停止播放时,就停止动画,图片回到原点。

二、效果

Android 实现旋转木马的音乐效果

三、实现代码

(1)mainactivity  

<span style="font-size:18px;">public class mainactivity extends activity {
  mediaplayer m1;
  imageview infooperatingiv;
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    infooperatingiv = (imageview) findviewbyid(r.id.infooperating);
 
    button play = (button) findviewbyid(r.id.play);
    button stop = (button) findviewbyid(r.id.stop);
 
    play.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        playmusic();
      }
    });
 
    stop.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        stopmusic();
      }
    });
 
  }
 
  private void playmusic() {
    m1 = mediaplayer.create(this, r.raw.quiet);
    m1.start();
 
    animation operatinganim = animationutils.loadanimation(this, r.anim.tip);
    linearinterpolator lin = new linearinterpolator();
    operatinganim.setinterpolator(lin);
    if (operatinganim != null) {
      infooperatingiv.startanimation(operatinganim);
    }
 
    m1.setoncompletionlistener(new oncompletionlistener() {
      @override
      public void oncompletion(mediaplayer mp) {
        mp.stop();
        infooperatingiv.clearanimation();
      }
    });
  }
 
  private void stopmusic() {
    m1.stop();
    infooperatingiv.clearanimation();
  }
 
}</span><span style="font-size: 16pt;">
</span>

(2)画圆的控件,这部分代码参考了网友的。

  <span style="font-size:18px;">public class roundimageview extends imageview {
  private int mborderthickness = 0;
  private context mcontext;
  private int defaultcolor = 0xffffffff;
  // 如果只有其中一个有值,则只画一个圆形边框
  private int mborderoutsidecolor = 0;
  private int mborderinsidecolor = 0;
  // 控件默认长、宽
  private int defaultwidth = 0;
  private int defaultheight = 0;
 
  public roundimageview(context context) {
    super(context);
    mcontext = context;
  }
 
  public roundimageview(context context, attributeset attrs) {
    super(context, attrs);
    mcontext = context;
    setcustomattributes(attrs);
  }
 
  public roundimageview(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
    mcontext = context;
    setcustomattributes(attrs);
  }
 
  private void setcustomattributes(attributeset attrs) {
    typedarray a = mcontext.obtainstyledattributes(attrs, r.styleable.roundedimageview);
    mborderthickness = a.getdimensionpixelsize(r.styleable.roundedimageview_border_thickness, 0);
    mborderoutsidecolor = a.getcolor(r.styleable.roundedimageview_border_outside_color, defaultcolor);
    mborderinsidecolor = a.getcolor(r.styleable.roundedimageview_border_inside_color, defaultcolor);
  }
 
  @override
  protected void ondraw(canvas canvas) {
    drawable drawable = getdrawable();
    if (drawable == null) {
      return;
    }
 
    if (getwidth() == 0 || getheight() == 0) {
      return;
    }
    this.measure(0, 0);
    if (drawable.getclass() == ninepatchdrawable.class)
      return;
    bitmap b = ((bitmapdrawable) drawable).getbitmap();
    bitmap bitmap = b.copy(bitmap.config.argb_8888, true);
    if (defaultwidth == 0) {
      defaultwidth = getwidth();
 
    }
    if (defaultheight == 0) {
      defaultheight = getheight();
    }
    // 保证重新读取图片后不会因为图片大小而改变控件宽、高的大小(针对宽、高为wrap_content布局的imageview,但会导致margin无效)
    // if (defaultwidth != 0 && defaultheight != 0) {
    // linearlayout.layoutparams params = new linearlayout.layoutparams(
    // defaultwidth, defaultheight);
    // setlayoutparams(params);
    // }
    int radius = 0;
    if (mborderinsidecolor != defaultcolor && mborderoutsidecolor != defaultcolor) {// 定义画两个边框,分别为外圆边框和内圆边框
      radius = (defaultwidth < defaultheight ? defaultwidth : defaultheight) / 2 - 2 * mborderthickness;
      // 画内圆
      drawcircleborder(canvas, radius + mborderthickness / 2, mborderinsidecolor);
      // 画外圆
      drawcircleborder(canvas, radius + mborderthickness + mborderthickness / 2, mborderoutsidecolor);
    } else if (mborderinsidecolor != defaultcolor && mborderoutsidecolor == defaultcolor) {// 定义画一个边框
      radius = (defaultwidth < defaultheight ? defaultwidth : defaultheight) / 2 - mborderthickness;
      drawcircleborder(canvas, radius + mborderthickness / 2, mborderinsidecolor);
    } else if (mborderinsidecolor == defaultcolor && mborderoutsidecolor != defaultcolor) {// 定义画一个边框
      radius = (defaultwidth < defaultheight ? defaultwidth : defaultheight) / 2 - mborderthickness;
      drawcircleborder(canvas, radius + mborderthickness / 2, mborderoutsidecolor);
    } else {// 没有边框
      radius = (defaultwidth < defaultheight ? defaultwidth : defaultheight) / 2;
    }
    bitmap roundbitmap = getcroppedroundbitmap(bitmap, radius);
    canvas.drawbitmap(roundbitmap, defaultwidth / 2 - radius, defaultheight / 2 - radius, null);
  }
 
  /**
   * 获取裁剪后的圆形图片
   *
   * @param radius
   *      半径
   */
  public bitmap getcroppedroundbitmap(bitmap bmp, int radius) {
    bitmap scaledsrcbmp;
    int diameter = radius * 2;
 
    // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
    int bmpwidth = bmp.getwidth();
    int bmpheight = bmp.getheight();
    int squarewidth = 0, squareheight = 0;
    int x = 0, y = 0;
    bitmap squarebitmap;
    if (bmpheight > bmpwidth) {// 高大于宽
      squarewidth = squareheight = bmpwidth;
      x = 0;
      y = (bmpheight - bmpwidth) / 2;
      // 截取正方形图片
      squarebitmap = bitmap.createbitmap(bmp, x, y, squarewidth, squareheight);
    } else if (bmpheight < bmpwidth) {// 宽大于高
      squarewidth = squareheight = bmpheight;
      x = (bmpwidth - bmpheight) / 2;
      y = 0;
      squarebitmap = bitmap.createbitmap(bmp, x, y, squarewidth, squareheight);
    } else {
      squarebitmap = bmp;
    }
 
    if (squarebitmap.getwidth() != diameter || squarebitmap.getheight() != diameter) {
      scaledsrcbmp = bitmap.createscaledbitmap(squarebitmap, diameter, diameter, true);
 
    } else {
      scaledsrcbmp = squarebitmap;
    }
    bitmap output = bitmap.createbitmap(scaledsrcbmp.getwidth(), scaledsrcbmp.getheight(), config.argb_8888);
    canvas canvas = new canvas(output);
 
    paint paint = new paint();
    rect rect = new rect(0, 0, scaledsrcbmp.getwidth(), scaledsrcbmp.getheight());
 
    paint.setantialias(true);
    paint.setfilterbitmap(true);
    paint.setdither(true);
    canvas.drawargb(0, 0, 0, 0);
    canvas.drawcircle(scaledsrcbmp.getwidth() / 2, scaledsrcbmp.getheight() / 2, scaledsrcbmp.getwidth() / 2, paint);
    paint.setxfermode(new porterduffxfermode(mode.src_in));
    canvas.drawbitmap(scaledsrcbmp, rect, rect, paint);
     
    bmp = null;
    squarebitmap = null;
    scaledsrcbmp = null;
    return output;
  }
 
  /**
   * 边缘画圆
   */
  private void drawcircleborder(canvas canvas, int radius, int color) {
    paint paint = new paint();
    /* 去锯齿 */
    paint.setantialias(true);
    paint.setfilterbitmap(true);
    paint.setdither(true);
    paint.setcolor(color);
    /* 设置paint的 style 为stroke:空心 */
    paint.setstyle(paint.style.stroke);
    /* 设置paint的外框宽度 */
    paint.setstrokewidth(mborderthickness);
    canvas.drawcircle(defaultwidth / 2, defaultheight / 2, radius, paint);
  }
 
}</span><span style="font-size: 16pt;">
</span>

以上就是android 实现旋转木马的音乐效果,有需要的朋友可以参考下。