Android Custom View implementation picture upload progress display (imitation QQ)

Source: Internet
Author: User

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)

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.