First look at the effect we want to achieve such as (QQ chat in the effect of sending pictures):
And look at the effect we've achieved:
The implementation of the principle is very simple, first we upload a picture need a progress value progress, whether it is written by the method of uploading or using a third-party open Source Library, second, you need to customize a view and override the OnDraw method, in this case the progress is to open a thread, and then imitate the progress increment, The progress value is then called through a custom view to a custom view and is redrawn based on progress.
The drawing is divided into three parts:
1. Draw Rectangle (picture area) Upper half shadow area;
2. Draw Rectangle (picture area) The lower part of the non-shaded area;
3. Draw the Intermediate Progress value (text);
OnDraw Code:
@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Mpaint.setantialias (TRUE); Anti -aliasing Mpaint.setstyle (Paint.Style.FILL); Mpaint.setcolor (Color.parsecolor ("#70000000"));//Translucent canvas.drawrect (0, 0, getwidth (), GetHeight ()-getheight ( ) *progress/100, mpaint); Mpaint.setcolor (Color.parsecolor ("#00000000"));//Fully transparent canvas.drawrect (0, GetHeight ()-getheight () *progress/ GetWidth (), getheight (), mpaint); Mpaint.settextsize (+); Mpaint.setcolor (Color.parsecolor ("#FFFFFF")); Mpaint.setstrokewidth (2); Rect rect=new rect (); Mpaint.gettextbounds ("100%", 0, "100%". Length (), rect);//determines the width of the text canvas.drawtext (progress+ "%", GetWidth ()/2-rect.width ()/2,getheight ()/2, mpaint); }
Ways to pass in progress values:
public void setprogress (int progress) {this.progress=progress;postinvalidate ();};
Main interface Call Method:
customview= (CUSTOMVIEW6) Findviewbyid (R.id.customview);//Simulation image upload Progress new Thread (new Runnable () {@Overridepublic void run () {while (true) { if (progress==100) {//Picture upload completed handler.sendemptymessage (SUCCESS); return;} progress++; Customview.setprogress (progress); try{ Thread.Sleep ($); Pause 0.2 seconds } catch (Interruptedexception e) { e.printstacktrace ();}}} ). Start ();
Demo: http://download.csdn.net/detail/baiyuliang2013/8690773
Android Custom View implementation picture upload progress display (imitation QQ)