Activity Code
1 PublicclassMainactivity extends Appcompatactivity {2Processimageview Processimageview =NULL; 3intprogress=0; 4 5 @Override6protectedvoidonCreate (Bundle savedinstancestate) {7 super.oncreate (savedinstancestate); 8 Setcontentview (R.layout.activity_main); 9 Tenprocessimageview=(Processimageview) Findviewbyid (r.id.image); One//simulate picture upload progress ANewThread (NewRunnable () { - @Override - Publicvoidrun () { the while(true){ -if(progress== -){//Image upload Complete -return; - } +progress++; - processimageview.setprogress (progress); +Try{ AThread.Sleep ( $);//pause for 0.2 seconds at}Catch(interruptedexception e) { - E.printstacktrace (); - } - } - } - }). Start (); in } -}
Custom View
1 PublicclassProcessimageview extends imageview{2Privatecontext Context; 3 PrivatePaint paint; 4PrivateLogutil log=logutil.getinstance (); 5intProgress =0; 6PrivateBoolean flag; 7 8 PublicProcessimageview (Context context) {9 super (context); Ten } One A PublicProcessimageview (Context context, AttributeSet attrs) { - This(Context, Attrs,0); - } the - PublicProcessimageview (context context, AttributeSet attrs,intdefstyleattr) { - Super (context, attrs, defstyleattr); - This. context=context; +Paint=NewPaint (); - } + A @Override atprotectedvoidOnDraw (canvas canvas) { - Super.ondraw (canvas); -Paint.setantialias (true);//Anti- aliasing -Paint.setstyle (Paint.Style.FILL);//set Paint to solid, Paint.Style.STROKE to hollow -Paint.setcolor (Color.parsecolor ("#70000000"));//set to Translucent -Canvas.drawrect (0,0, GetWidth (), GetHeight ()-getheight () *progress/ -, paint);//here GetWidth () Gets the image object width height XML value in -Paint.setcolor (Color.parsecolor ("#00000000"));//fully Transparent toCanvas.drawrect (0, GetHeight ()-getheight () * Progress/ -, + getwidth (), getheight (), paint); - theif(!flag) { *Paint.settextsize ( -); $Paint.setcolor (Color.parsecolor ("#FFFFFF")); Panax NotoginsengPaint.setstrokewidth (2); -Rect rect =NewRect (); thePaint.gettextbounds ("100%",0,"100%". Length (), rect);//determine the width of text +Canvas.drawtext (Progress +"%", getwidth ()/2-Rect.width ()/2, AGetHeight ()/2, paint); the } + } - $ PublicvoidSetprogress (intprogress) { $ This. Progress =progress; -if(progress== -){ -flag=true; the } - postinvalidate (); Wuyi } the}
Look at the custom view class, in the main OnDraw () method.
The drawing is divided into three parts,
The first part is the upper semi-transparent area
Paint.setantialias (TRUE); Anti-aliasing Paint.setstyle (Paint.Style.FILL); Set Paint to solid, Paint.Style.STROKE as Hollow Paint.setcolor (Color.parsecolor ("#70000000")); Set to Translucent Canvas.drawrect (0,0,getwidth (), GetHeight ()-getheight () *progress/100,paint);
Part II is divided into the next fully transparent area
Paint.setcolor (Color.parsecolor ("#00000000"));//Fully transparent Canvas.drawrect (0, GetHeight ()-getheight () * progress/100, GetWidth (), getheight (), paint);
The third part is the progress value change in the middle.
if (!flag) {paint.settextsize (30); Paint.setcolor (Color.parsecolor ("#FFFFFF")); Paint.setstrokewidth (2); Rect rect = new rect (); Paint.gettextbounds ("100%", 0, "100%". Length (), rect);//determines the width of the text Canvas.drawtext (progress + "%", getwidth ()/2-r Ect.width ()/2, GetHeight ()/2, paint); }
Android Custom ImageView copy image upload