Custom View achieve a variety of types, were inherited from the View is fully customizable, inherited from the prior control (e.g., the ImageView) to achieve a particular effect, achieved ViewGroup layout class inherits from this which involves measuring and layout View, View, drawing, touch event handling, animation.
The realization of custom circular progress bar, I am using inheritance LinearLayout way to achieve the final effect is as follows:
my thoughts
To achieve this simple effect, my idea is that the circle_progress_view as a background, the progress achieved with Paint draw an arc, as a sub-assembly. As long as the current schedule changes, redraw it.
Drawing View content itself, call View.onDraw method; draw your own children, it is achieved by dispatchDraw.
Drawing View component of the first to call the draw method to draw the background, after the unfinished background, draw method calls onDraw, then call dispatchDraw method, draw the distribution sub-assemblies.
Mentioned above, as the background circle_progress_view, arcuate progress bar as a subcomponent, so long as the drawing of the arc added in the progress bar on the line dispatchDraw method, very simple!
Copyright * (C) 1996-2016 YONGF Inc.All Rights Reserved. * Scott Wang blog.54yongf.com | blog.csdn.net/yongf2014 * File Name: CircleProgressView * Description: * Change History: * The version number of the dates overview related operations * 1.0 Scott Wang 16-4-21 new: the Create * /
/ ** * Set the progress of the background image * * @param resId * / public void the setIcon ( int resId) { mIcon.setImageResource (resId); }
/ ** * Set the progress bar custom tip text * * @param desc * / public void setDesc (String desc) { mDesc.setText (desc); }
/ ** * Set the progress bar display turned off by default * * @param progressEnable * / public void setProgressEnable ( Boolean progressEnable) { mProgressEnable = progressEnable; }
/ ** * Set the progress of the maximum value, the default is 100 * * @param max * / public void setMax ( Long max) { Mmax = max; }
/ ** * Set the current progress * * @param Progress * / public void the setProgress ( Long Progress) { mProgress = Progress;
invalidate(); }
@Override protected void dispatchDraw (the Canvas Canvas) { Super .dispatchDraw (Canvas); // draw the background
IF (mProgressEnable) { RectF Oval = new new RectF (mIcon.getLeft (), mIcon.getTop (), mIcon.getRight (), mIcon.getBottom ()); // background image to draw a rectangle based on a float startAngle = - 90 ; // - 90 is the equivalent of "True north"
a float sweepAngle mProgress * = 360 .F / Mmax; // angle of rotation
Boolean useCenter = to false ; // whether to keep both sides of the radius
Paint = the Paint new new the Paint (); paint.setColor (Color.BLUE); // blue brush paint.setStyle (Paint.Style.STROKE); paint.setStrokeWidth ( . 5 ); // set the thickness of the brush, i.e. the progress bar thickness paint.setAntiAlias ( to true ); // anti-aliasing // begin drawing an arc to indicate the progress of canvas.drawArc (oval, startAngle, sweepAngle, useCenter, paint);
Thank you for visiting my personal blog friends, if you feel your site has a search of help for the problem, and feel for the site also satisfied, the top of it, and hope you will share the site to your friends! In this heartfelt thanks to you! :)