After learning the basic usage of Paint,canvas, we can start to practice, first write a simple picture loading progress bar to see.
By convention, look at the effect chart before deciding whether to look down:
Since see here, should be to understand the picture loading progress bar, we first look at the specific usage, and then see the implementation of the custom view:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:custom= "http:// Schemas.android.com/apk/res-auto "
android:layout_width=" match_parent "
android:layout_height=" Match_ Parent ">
<imageview
android:id=" @+id/img "
android:layout_width=" 200DP "
android:layout_ height= "200DP"
android:scaletype= "Centercrop"
android:layout_centerinparent= "true"/>
< Com.example.circleprogresstest.CircleProgressView
android:id= "@+id/progressview"
android:layout_ Width= "60DP"
android:layout_height= "60DP"
android:layout_centerinparent= "true"
custom: Isshowprogress= "true"/>
</RelativeLayout>
Imageloader.getinstance (). DisplayImage (URL, ImageView, options,
new Simpleimageloadinglistener (),
new Imageloadingprogresslistener () {
@Override public
void Onprogressupdate (String imageuri, view view, int Current, int total) {
if (current==total) {
progressview.setvisibility (view.gone);
} else{
progressview.setsweepangle ((int) (360*current*1.0f/total));
Progressview.postinvalidate ();}}
);
As you can see, the above usage, very simple, add our custom view in XML, and add TextView or button exactly the same, but only our own custom attributes, you can set the color of the circle, as well as text color, size and so on. Then, the method used in Mainactivity is equally simple, as long as the progress of the picture is updated and the progress of our progress bar is updated.
Below we specifically say we implement custom progress bar process, we only need to rewrite the OnDraw () method is enough, it is obvious, our progress bar includes three parts, Inner circle Circle, outer ring arc, the middle of the text, specific look code:
protected void OnDraw (Canvas Canvas) {mwidth=getmeasuredwidth ();
Mheight=getmeasuredheight ();
Radius= (float) (Math.min (mwidth,mheight) *1.0/2)-STROKEWIDTH/2;
Draw Inner Circle Circle Mpaint.setcolor (Initcolor);
Mpaint.setstyle (Paint.Style.STROKE);
Mpaint.setstrokewidth (Strokewidth);
Canvas.drawcircle (Mwidth/2,mheight/2,radius,mpaint);
Draw the covered Arc mpaint.setcolor (Covercolor);
RECTF rectf=new RECTF (Mwidth/2-radius,mheight/2-radius,mwidth/2+radius,mheight/2+radius);
Canvas.drawarc (Rectf,-90,sweepangle,false,mpaint); Draws the middle text if (isshowprogress) {Progresstext=string.format (Getresources (). getString (R.string.progress_text), (int) (
sweepangle*100.0/360));
Mpaint.settextsize (TEXTSIZE);
Mpaint.setcolor (TextColor);
if (mbound==null) {mbound=new Rect ();
} mpaint.gettextbounds (Progresstext,0,progresstext.length (), mbound);
Mpaint.setstyle (Paint.Style.FILL);
Canvas.drawtext (Progresstext,mwidth/2-mbound.width ()/2,mheight/2+mbound.height ()/2,mPaint); }
}
Of course, in order for us to customize the size and color of the progress bar, we also use custom attributes, and in the constructor, we also need to load the attributes in the XML:
<resources>
<declare-styleable name= "Circleprogressview" >
<attr name= "Initcolor" Color "/>
<attr name=" covercolor "format=" color "/> <attr" name= "Strokewidth" format= "Dimension"
>
<attr name= "progresstextsize" format= "Dimension"/> <attr name= "Progresstextcolor"
Color "/>
<attr name=" Isshowprogress "format=" boolean "/>
</declare-styleable>
</ Resources>
private void Initvalues (context context, AttributeSet attrs, int defstyleattr) {TypedArray Typedarray=context.gettheme (
). Obtainstyledattributes (attrs,r.styleable.circleprogressview,defstyleattr,0);
int Num=typedarray.getindexcount ();
for (int i=0;i<num;i++) {int attr=typedarray.getindex (i);
Switch (attr) {case R.styleable.circleprogressview_initcolor:initcolor=typedarray.getcolor (Attr,color.gray);
Break
Case R.styleable.circleprogressview_covercolor:covercolor=typedarray.getcolor (Attr,color.black);
Break
Case R.styleable.circleprogressview_strokewidth:strokewidth=typedarray.getdimensionpixeloffset (attr,5);
Break
Case R.styleable.circleprogressview_progresstextsize:textsize=typedarray.getdimensionpixelsize (attr,30);
Break
Case R.styleable.circleprogressview_progresstextcolor:textcolor=typedarray.getcolor (Attr,Color.BLACK);
Break
Case R.styleable.circleprogressview_isshowprogress:isshowprogress=typedarray.getboolean (Attr,false); BreaK
Default:break;
} typedarray.recycle ();
Mpaint=new Paint ();
Mpaint.setantialias (TRUE);
}
SOURCE Download: http://xiazai.jb51.net/201608/yuanma/circleprogress (jb51.net). rar
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.