Android drop-down refresh with progress bar effect

Source: Internet
Author: User
Tags getcolor in degrees

First look at the run, the program's dropdown refresh reference video, in the video page also provides the source download,

http://www.imooc.com/learn/135


This article mainly said on this basis to increase the speed of the progress bar rotation and increment processing, in the end of the article will also give the source code, here is the main description of a class used

Roundprogressbar

Package Com.cayden.listview;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rectf;import Android.os.Handler; Import Android.os.message;import android.util.attributeset;import android.view.view;/** * progress bar with progress, thread-safe view, Progress can be updated directly in the thread * @author Cuiran * */public class Roundprogressbar extends View {/** * Brush object reference */private paint paint;/** * ring Color */private int roundcolor;/** * Ring progress color */private int roundprogresscolor;/** * Middle Progress percentage of string color */private int textcolor;/*  * * Middle Progress percent string font */private float textsize;/** * ring width */private float roundwidth;/** * Max Progress */private int max;/** * Current Progress */private Int progress;/** * Whether to show intermediate progress */private Boolean textisdisplayable;/** * Progress style, solid or hollow */private int style;public static final int STROKE = 0;public static final int FILL = 1;/** Loop **/public boolean ISSPinning = False;public Roundprogressbar (context context) {This (context, null);} Public Roundprogressbar (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Roundprogressbar (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, defstyle);p aint = new Pa int (); TypedArray Mtypedarray = context.obtainstyledattributes (Attrs,r.styleable.roundprogressbar);// Get custom properties and default values Roundcolor = Mtypedarray.getcolor (R.styleable.roundprogressbar_roundcolor, color.red); Roundprogresscolor = Mtypedarray.getcolor (R.styleable.roundprogressbar_roundprogresscolor, Color.GREEN); TextColor = Mtypedarray.getcolor (R.styleable.roundprogressbar_textcolor, color.green); textSize = Mtypedarray.getdimension ( R.styleable.roundprogressbar_textsize, roundwidth = Mtypedarray.getdimension (r.styleable.roundprogressbar_ Roundwidth, 5); max = Mtypedarray.getinteger (R.styleable.roundprogressbar_max, textisdisplayable); Mtypedarray.getboolean (r.styleable.roundprogressbar_textisdisplayable, true);style = Mtypedarray.getint (r.styleable.roundprogressbar_style, 0); mtypedarray.recycle ();//Must recycle}@ overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);/** * Draws the outermost large ring */int centre = getwidth ()/2;//Gets the x-coordinate of the center point int radius = (int) (CENTRE-ROUNDWIDTH/2); The radius of the ring Paint.setcolor (Roundcolor); Sets the color of the ring Paint.setstyle (Paint.Style.STROKE); Set Hollow Paint.setstrokewidth (Roundwidth);  Sets the width of the ring Paint.setantialias (true); Anti-aliasing canvas.drawcircle (centre, centre, RADIUS, paint); Draw a circle/** * Picture Progress percentage is now removed to picture *///paint.setstrokewidth (0); Paint.setcolor (TextColor);//paint.settextsize (textSize);//paint.settypeface (Typeface.default_bold);  Set Font//int percent = (int) ((float) progress/(FLOAT) max) * 100);   The percentage of progress in the middle, first converted to float in the division operation, otherwise all for 0//float textWidth = paint.measuretext (Percent + "%"); To measure the font width, we need to set the width of the font in the middle of the ring////if (textisdisplayable && percent! = 0 && style = STROKE) {// Canvas.drawtext (Percent + "%", CENTRE-TEXTWIDTH/2, centre + TEXTSIZE/2, paint); Draw a progress percentage//}if(isspinning) {/** * picture */bitmap Bitmap=bitmapfactory.decoderesource (Getresources (), r.drawable.loading02); int width= Bitmap.getwidth (); int height=bitmap.getheight (); Canvas.drawbitmap (bitmap, CENTRE-WIDTH/2, CENTRE-HEIGHT/2, paint); Bitmap=bitmapfactory.decoderesource (Getresources (), r.drawable.loading06); Width=bitmap.getwidth (); height= Bitmap.getheight (); Canvas.drawbitmap (bitmap, CENTRE-WIDTH/2, CENTRE-HEIGHT/2, paint);/** * Draw arcs, draw the progress of the ring */// Sets whether the progress is solid or hollow paint.setstrokewidth (roundwidth);  Sets the width of the ring Paint.setcolor (Roundprogresscolor);  Set the color of the progress RECTF oval = new RECTF (Centre-radius, Centre-radius, centre+ radius, centre + radius);  The bounds of the shape and size of the arc used to define Paint.setstyle (Paint.Style.STROKE); Canvas.drawarc (oval, progress-90, (), false, Paint); Draw arcs According to progress}else{/** * Picture */bitmap Bitmap=bitmapfactory.decoderesource (Getresources (), r.drawable.loading02); int Width=bitmap.getwidth (); int height=bitmap.getheight (); Canvas.drawbitmap (bitmap, CENTRE-WIDTH/2, CENTRE-HEIGHT/2, Paint); Bitmap=bitmapfactory.Decoderesource (Getresources (), r.drawable.loading05); Width=bitmap.getwidth (); Height=bitmap.getheight (); Canvas.drawbitmap (Bitmap, CENTRE-WIDTH/2, CENTRE-HEIGHT/2, paint);/** * Draw arcs, draw the progress of the ring */// Sets whether the progress is solid or hollow paint.setstrokewidth (roundwidth);  Sets the width of the ring Paint.setcolor (Roundprogresscolor);  Set the color of the progress RECTF oval = new RECTF (Centre-radius, Centre-radius, centre+ radius, centre + radius);  The bounds of the shape and size of the arc used to define Paint.setstyle (Paint.Style.STROKE); Canvas.drawarc (oval, -90, progress, false, Paint); Draw arc}}public synchronized int Getmax () {return max;} according to progress /** * Set the maximum progress value * @param max */public synchronized void Setmax (int max) {if (Max < 0) {throw new IllegalArgumentException ( "Max not less than 0");} This.max = max;}  /** * Get progress. Need to synchronize * @return */public synchronized int getprogress () {return progress;}        /** * Reset the count (in increment mode) */public void Resetcount () {progress = 0;    Invalidate ();  }/** * Turn off spin mode */public void stopspinning () {      Isspinning = false;        progress = 0;    Spinhandler.removemessages (0);        }/** * Puts the View on spin mode */public void Spin () {isspinning = true;    Spinhandler.sendemptymessage (0);         }/** * Increment the progress by 1 (OF) */public void incrementprogress () {isspinning = false;        if (Progress > Progress) = 0;    Spinhandler.sendemptymessage (0); } private Handler Spinhandler = new Handler () {/** * This was the code that would increment the progress Var Iable * And so spin the wheel */@Override public void Handlemessage (Message msg) {i        Nvalidate ();                  if (isspinning) {progress + = 10;                  if (Progress >) {progress = 0;        } spinhandler.sendemptymessagedelayed (0, 0);        }//super.handlemessage (msg); }};/** * Set Progress, this is a thread-safe control, due to multi-line considerationsThe problem that needs to be synchronized * Refresh interface call Postinvalidate () can be refreshed in non-UI thread * @param progress */public synchronized void setprogress (int progress) {if (pro Gress < 0) {throw new IllegalArgumentException ("Progress not less than 0");} if (Progress > Max) {progress = max;} if (Progress <= max) {this.progress = Progress;postinvalidate ();}} public int Getcriclecolor () {return roundcolor;} public void Setcriclecolor (int criclecolor) {this.roundcolor = Criclecolor;} public int Getcricleprogresscolor () {return roundprogresscolor;} public void Setcricleprogresscolor (int cricleprogresscolor) {this.roundprogresscolor = Cricleprogresscolor;} public int GetTextColor () {return textcolor;} public void SetTextColor (int textcolor) {this.textcolor = TextColor;} public float GetTextSize () {return textSize;} public void Settextsize (float textSize) {this.textsize = textSize;} public float Getroundwidth () {return roundwidth;} public void Setroundwidth (float roundwidth) {this.roundwidth = Roundwidth;}}

The main drawing section in the class

Canvas.drawarc (oval, progress-90, +, false, paint); //Draw arcs based on progress

CANVAS.DRawarc (Oval, -90, progress , false, paint); //Draw arcs based on progress

Take a look at the Canvas.drawarc method

public void DrawArc (RECTF oval, float startangle, float sweepAngle, Boolean usecenter, Paint paint)

    • Oval: Specifies a rectangular area of the outer contour of an arc.
    • StartAngle: The starting angle of the arc, measured in degrees.
    • SweepAngle: Arc sweep Angle, clockwise direction, unit is degrees.
    • Usecenter: True to include the center of the circle when the arc is drawn, which is often used to draw a pie.
    • Paint: Draws an arc's artboard properties, such as color, whether filled, etc.
You can refer to the following website for this introduction

Http://blog.sina.com.cn/s/blog_783ede0301012im3.html



Finally give the project source code:

Https://github.com/cayden/ListViewDemo


Android drop-down refresh with progress bar effect

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.