The music effect of the Carousel of Android notes

Source: Internet
Author: User

First, preface--

Everyone must be in the Baidu music online listen to the song, have not noticed that revolving record--


On top of that, when the music is playing, the disc wheel is turning, like a merry-go-round. Feel good to play.

I happen to think of the former playing music player, hey, that this can also be done on the mobile phone music player, this replaces the progress bar.

When you think about it, you get excited, so first draw the circle, then place the background picture, and then use the animation to rotate. While the music is playing,

Starts the animation of the circular picture, pauses the rotation when the music is paused, stops the animation when the music stops playing, and the picture goes back to the original point.

Second, the effect--


Third, the source code--

(1) mainactivity

<span style= "FONT-SIZE:18PX;" >public class Mainactivity extends Activity {MediaPlayer m1;imageview infooperatingiv; @Overrideprotected 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);p Lay.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {playmusic ();}}); Stop.setonclicklistener (New Onclicklistener () {@Overridepublic 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 () {@Overridepublicvoid Oncompletion (MediaPlayer MP) {mp.stop (); Infooperatingiv.clearanimation ();});} private void Stopmusic () {m1.stop (); Infooperatingiv.clearanimation ();}} </span><span style= "FONT-SIZE:16PT;" ></span>

(2) Draw round the control, this part of the code refers to the Netizen's.

<span style= "FONT-SIZE:18PX;" >public class Roundimageview extends ImageView {private int mborderthickness = 0;private Context mcontext;private int D  Efaultcolor = 0xffffffff;//If only one of them has a value, draw only a circular border private int mborderoutsidecolor = 0;private int mborderinsidecolor = 0;// Control default long, wide 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 = cont Ext;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);} @Overrideprotected 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 ();} Ensure that the image will not change the width and height of the control after re-reading the picture (ImageView for a wide, high-wrap_content layout, but will cause margin to be invalid)/if (DefaultWidth! = 0 && DefaultHeight! = 0) {//Linearlayout.layoutparams params = new Linearlayout.layoutparams (//DefaultWidth, DefaultHeight) ;//Setlayoutparams (params);//}int radius = 0;if (mborderinsidecolor! = DefaultColor && Mborderoutsidecolor! = de Faultcolor) {//define draw two borders, respectively, for the outer border and the inner circle radius = (DefaultWidth < DefaulthEight? Defaultwidth:defaultheight)/2-2 * mborderthickness;//Draw inner circle Drawcircleborder (canvas, Radius + MBORDERTHICKNESS/2, MBo Rderinsidecolor);//Draw Outer circle Drawcircleborder (canvas, radius + mborderthickness + MBORDERTHICKNESS/2, mborderoutsidecolor);} else if (mborderinsidecolor! = DefaultColor && Mborderoutsidecolor = = DefaultColor) {//define draw a border radius = (defaultwi DTH < DefaultHeight? Defaultwidth:defaultheight)/2-mborderthickness;drawcircleborder (canvas, Radius + MBORDERTHICKNESS/2, mBorderInside Color);} else if (Mborderinsidecolor = = DefaultColor && mborderoutsidecolor! = DefaultColor) {//define draw a border radius = (defaultwi DTH < DefaultHeight? Defaultwidth:defaultheight)/2-mborderthickness;drawcircleborder (canvas, Radius + MBORDERTHICKNESS/2, MBorderOutsid EColor);} else {//no border radius = (DefaultWidth < defaultheight? defaultwidth:defaultheight)/2;} Bitmap Roundbitmap = Getcroppedroundbitmap (Bitmap, RADIUS); Canvas.drawbitmap (Roundbitmap, DefaultwidTH/2-radius, DEFAULTHEIGHT/2-radius, null);} /** * Get cropped round picture * * @param radius * radius */public Bitmap getcroppedroundbitmap (Bitmap bmp, int radius) {Bitmap Scaledsrcbmp;int diameter = radius * 2;//In order to prevent the width of high inequality, resulting in a circular picture deformation, so that the rectangle in the middle of the largest square picture of the image int bmpwidth = Bmp.getwidth (); int Bmpheight = Bmp.getheight (); int squarewidth = 0, squareheight = 0;int x = 0, y = 0; Bitmap squarebitmap;if (Bmpheight > Bmpwidth) {//tall to wide squarewidth = Squareheight = Bmpwidth;x = 0;y = (BMPHEIGHT-BMPW Idth)/2;//intercept square picture Squarebitmap = Bitmap.createbitmap (bmp, X, Y, Squarewidth, squareheight);} else if (Bmpheight < bmpwidth) {//large to high squarewidth = Squareheight = Bmpheight;x = (bmpwidth-bmpheight)/2;y = 0;squa Rebitmap = 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 ());p Aint.setantialias (true); Paint.setfilterbitmap (True);p Aint.setdither (true); Canvas.drawargb (0, 0, 0, 0); Canvas.drawcircle ( Scaledsrcbmp.getwidth ()/2, Scaledsrcbmp.getheight ()/2, Scaledsrcbmp.getwidth ()/2, paint);p Aint.setxfermode (new Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (scaledsrcbmp, rect, rect, paint); bmp = Null;squarebitmap = null; Scaledsrcbmp = Null;return output;} /** * Edge Draw circle */private void Drawcircleborder (canvas canvas, int radius, int color) {Paint paint = new paint ();/* antialiasing */paint . Setantialias (True);p Aint.setfilterbitmap (True);p Aint.setdither (True);p aint.setcolor (color);/* Set Paint style For STROKE: Hollow */paint.setstyle (Paint.Style.STROKE);/* Sets the outer frame width of the paint */paint.setstrokewidth (mborderthickness); Canvas.drawcircle (defAULTWIDTH/2, DEFAULTHEIGHT/2, radius, paint);}} </span><span style= "FONT-SIZE:16PT;" ></span>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.