Simple loading box for Android custom controls

Source: Internet
Author: User

OK, long time not to move Android, feel that they are fast is a salted fish, take advantage of this week's development task has been completed, next week's development plan is not down, to review the Android custom control, so there is the following this ugly thing

  

Implementation is also very simple, the following directly on the code;

  

public class Ringloading extends View {
Private final Context Mcontext;
Private Paint Mpaint;
private int Outradius;
private int Innerradius;
private int ringwidth = 20;
Private int[] Center = new INT[2];
Private float StartIndex =-90;
private float EndIndex = 0;
Private Handler Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Invalidate ();
}
};

Public ringloading (Context context) {
This (context, NULL);
}

Public ringloading (context context, AttributeSet Attrs) {
This (context, attrs, 0);
}

Public ringloading (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
Mcontext = context;
Init ();
}

private void init () {
Mpaint = new Paint ();
Mpaint.setcolor (Color.White);
Mpaint.setstyle (Paint.Style.STROKE);
Mpaint.setantialias (TRUE);
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int width = measurespec.getsize (widthmeasurespec);
int height = measurespec.getsize (heightmeasurespec);
Center[0] = WIDTH/2;
CENTER[1] = HEIGHT/2;
Outradius = (Width > Height HEIGHT/2: WIDTH/2)-ringwidth;
Innerradius = Outradius-ringwidth;
LOGGER.I ("Size", Outradius, Innerradius);
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}

@Override
protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {
Super.onlayout (changed, left, top, right, bottom);
}

@Override
protected void OnDraw (canvas canvas) {
Mpaint.setstrokewidth (Ringwidth);
Mpaint.setargb (0, 0, 0, 0);
Canvas.drawcircle (Center[0], center[1], Innerradius, mpaint);
Mpaint.setargb (255, 0, 0, 255);
Canvas.drawcircle (Center[0], center[1], Outradius, mpaint);
Mpaint.setargb (255, 255, 255, 255);
RECTF rect = new RECTF (Ringwidth, Ringwidth, Ringwidth + 2 * outradius, Ringwidth + 2 *
Outradius);
Canvas.drawarc (Rect, StartIndex, EndIndex, False, Mpaint);
Super.ondraw (canvas);
}

public void startloading () {
Timer timer = new timer ();
TimerTask timertask = new TimerTask () {
@Override
public void Run () {
startindex++;
if (StartIndex < 0) {
EndIndex + +;
} else if (StartIndex > 180) {
endindex--;
if (StartIndex = = 270) {
StartIndex =-90;
EndIndex = 0;
}
} else {
EndIndex = 90;
}
Mhandler.obtainmessage (1). Sendtotarget ();
}
};
Timer.schedule (TimerTask, 100, 5);
}
}

Then do a simple analysis, lest you look back and then take a moment.
From the implementation of the effect, the entire control is a ring, the ring on the length of the change of white stripes, constantly turn ah turn. If the length of the white bars, it is simple, directly put a picture, write a rotation animation OK, the key is change ah, then have to get a canvas,
Himself to draw, because the ring and white stripes are drawn out, so long as the inheritance of view is good;
The init () method Initializes some things to the brush, which is nothing to say, and the main implementation code is concentrated in the three methods of Onmeasure,ondraw and Startloading, where onmeasure obtains the radius of the outer circle and the inner circle and coordinates of the center point, OnDraw
Method The circle and rotation arc are drawn according to the radius and coordinates, and the Startloading method constantly changes the center angle of the circle's starting radian and arc length of the arc, then notifies the control to redraw (the terminating method I did not write here, call the timer Cancel method is good);
Knowing the role of each method, here are some details of how to enter these methods:
The first is onmeasure (int widthmeasurespec, int heightmeasurespec), this method is the system callback, it will be at the appropriate time, the control of the width of the information back to us, that is, these two parameters, Both of these parameters are 32-bit shaped,
Where the first two representations of the pattern can be obtained by measurespec.getmode (int ...), the pattern is divided into three types,
Measurespec.unspecified, default, Width height is not limited by the parent control;
Measurespec.at_most, this mode when the layout file is declared as Android:layout_width = "wrap_content";
measurespec.exactly, the layout file specifies the specific DP, or the Match_parent value is this mode;
Measurespec.getsize (int ...) Gets the size of the control;

Then is the OnDraw method, canvas.drawcircle (float centerx,floatcentery,int radius,paint) This is the method of drawing round,
Canvas.drawarc (Rectf,startangle,lenghtangle,boolean usercenter,paint), this is the way to draw an arc,
Understanding these two methods, OnDraw's understanding is not a problem.

Finally is startloading, this method only involves the logic change, this logic is not complex, does not say, this record is here, bye~~~

Simple loading box for Android custom controls

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.