Android Custom view realizes the round rotating plate _android in QQ music

Source: Internet
Author: User

Round rotating plate in QQ music

Thinking Analysis:
1, in the onmeasure to measure the entire view width and height, set the width and height
2, get our res picture resources, in the OnDraw method to draw a circular picture
3, through the handler send runnable to start the rotation of the thread (if you want to do a circular head, this step can be removed)
4. Use our view in the layout

Effect Chart:


Post our variable information:

View width and height 
int mheight = 0; 
int mwidth = 0; 
Circular picture 
Bitmap Bitmap = null; 
The true radius of the circular picture 
int radius = 0; 
Rotating animation of the rectangular 
matrix matrix = new Matrix (); 
Rotate animation angle 
int degrees = 0; 

Step One: measure the width and height of the whole view and set the width and height

@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, he 
 IGHTMEASURESPEC); 
 Measure the width and height of the entire view mwidth = Measuredwidth (Widthmeasurespec); 
 mheight= measuredheight (HEIGHTMEASURESPEC); 
Setmeasureddimension (Mwidth, mheight); 
 private int measuredwidth (int widthmeasurespec) {int Mode = Measurespec.getmode (Widthmeasurespec); 
 int Size = measurespec.getsize (Widthmeasurespec); 
 if (Mode = = measurespec.exactly) {mwidth = Size; 
  else {//is determined by the picture width int value = getpaddingleft () + getpaddingright () + bitmap.getwidth (); 
  if (Mode = = Measurespec.at_most) {//is determined by the picture and padding width, but cannot exceed view's width Mwidth = math.min (value, Size); 
} return mwidth; 
 private int measuredheight (int heightmeasurespec) {int Mode = Measurespec.getmode (Heightmeasurespec); 
 int Size = measurespec.getsize (Heightmeasurespec); 
 if (Mode = = measurespec.exactly) {mheight = Size; else {//determined by picture height int value = GetpadDingtop () + getpaddingbottom () + bitmap.getheight (); 
  if (Mode = = Measurespec.at_most) {//The height is determined by the picture and padding, but not above the view's high mheight = Math.min (value, Size); 
} return mheight;  }

Step Two: draw a circular picture in the OnDraw method after getting the picture resource for our res

/Get res's picture resource bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon); 
 @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 Canvas.concat (matrix); 
 The true radius must be the view's width and height minimum radius = math.min (mwidth, mheight); 
 If the picture itself is too wide and too large, zoom bitmap = Bitmap.createscaledbitmap (bitmap, Radius, radius, false); 
 Draw round picture Canvas.drawbitmap (createcircleimage (bitmap, radius), 0, 0, NULL); 
Matrix.reset (); 
 Private Bitmap Createcircleimage (Bitmap source, int radius) {Paint Paint = new Paint (); 
 Paint.setantialias (TRUE); 
 Bitmap target = bitmap.createbitmap (radius, radius, Bitmap.Config.ARGB_8888); 
 Produces a canvas of the same size Canvas Canvas = new Canvas (target); 
 First Draw round canvas.drawcircle (RADIUS/2, RADIUS/2, RADIUS/2, paint); 
 Use the src_in mode to display the intersection of the following paint Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN)); 
 Draw pictures from (0,0) canvas.drawbitmap (source, 0, 0, paint); 
return target; } 

Step Three: start the spinning thread by handler sending runnable

Start rotating 
mhandler.post (runnable); 
[Java] View plain copy on the code to see the snippet derived from my Code slice
//-----------rotate animation----------- 
Handler mhandler = new Handler (); 
Runnable Runnable = new Runnable () { 
 @Override public 
 void Run () { 
  matrix.postrotate (degrees++, RADIUS/2, RA DIUS/2); 
  Redraw 
  invalidate (); 
  Mhandler.postdelayed (runnable, M); 
 } 
; 

Step Four: use our view in the layout

<com.handsome.cycle.mycycleview 
 android:layout_width= "wrap_content" 
 android:layout_height= "Wrap_ Content "/> 

The following is the source of the entire class

public class Mycycleview extends View {//view width and height int mheight = 0; 
 int mwidth = 0; 
 Circular picture Bitmap Bitmap = null; 
 The true radius of the circular picture int radius = 0; 
 Rotating animation of the rectangular matrix matrix = new Matrix (); 
 
 rotate animation angle int degrees = 0; 
 -----------Rotary animation-----------Handler Mhandler = new Handler (); Runnable Runnable = new Runnable () {@Override public void run () {matrix.postrotate (degrees++, RADIUS/2, Radi 
   US/2); 
   Redraw invalidate (); 
  Mhandler.postdelayed (runnable, 50); 
 
 } 
 }; 
  Public Mycycleview {Super (context); 
 Initview (); 
  Public Mycycleview (context, AttributeSet attrs) {Super (context, attrs); 
 Initview (); 
  Public Mycycleview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); 
 Initview (); 
  The public void Initview () {//Gets the picture resource of res bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon); 
 Start rotating mhandler.post (runnable); }
 
 @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespe 
  c, Heightmeasurespec); 
  Measure the width and height of the entire view mwidth = Measuredwidth (Widthmeasurespec); 
  Mheight = Measuredheight (Heightmeasurespec); 
 Setmeasureddimension (Mwidth, mheight); 
  private int measuredwidth (int widthmeasurespec) {int Mode = Measurespec.getmode (Widthmeasurespec); 
  int Size = measurespec.getsize (Widthmeasurespec); 
  if (Mode = = measurespec.exactly) {mwidth = Size; 
   else {//is determined by the picture width int value = getpaddingleft () + getpaddingright () + bitmap.getwidth (); 
   if (Mode = = Measurespec.at_most) {//is determined by the picture and padding width, but cannot exceed view's width Mwidth = math.min (value, Size); 
 } return mwidth; 
  private int measuredheight (int heightmeasurespec) {int Mode = Measurespec.getmode (Heightmeasurespec); 
  int Size = measurespec.getsize (Heightmeasurespec); 
  if (Mode = = measurespec.exactly) {mheight = Size; 
 } else {  Determined by the picture height int value = getpaddingtop () + getpaddingbottom () + bitmap.getheight (); 
   if (Mode = = Measurespec.at_most) {//The height is determined by the picture and padding, but not above the view's high mheight = Math.min (value, Size); 
 } return mheight; 
  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
  Canvas.concat (matrix); 
  The true radius must be the view's width and height minimum radius = math.min (mwidth, mheight); 
  If the picture itself is too wide and too large, zoom bitmap = Bitmap.createscaledbitmap (bitmap, Radius, radius, false); 
  Draw round picture Canvas.drawbitmap (createcircleimage (bitmap, radius), 0, 0, NULL); 
 Matrix.reset (); 
  Private Bitmap Createcircleimage (Bitmap source, int radius) {Paint Paint = new Paint (); 
  Paint.setantialias (TRUE); 
  Bitmap target = bitmap.createbitmap (radius, radius, Bitmap.Config.ARGB_8888); 
  Produces a canvas of the same size Canvas Canvas = new Canvas (target); 
  First Draw round canvas.drawcircle (RADIUS/2, RADIUS/2, RADIUS/2, paint); Use the src_in mode to display the intersection of the paint at Paint.setxfermode (new PORTERDUFFXFermode (PorterDuff.Mode.SRC_IN)); 
  Draw pictures from (0,0) canvas.drawbitmap (source, 0, 0, paint); 
 return target;  } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.