Android custom View allows you to display the upload progress of images (like QQ) and Android Images
First, let's take a look at the effect we want to achieve (the effect when sending images in QQ chat ):
Let's look at our implementation:
The implementation principle is very simple. First, we need a progress value progress when uploading images. This is a self-written upload method or a third-party open source library. Second, you need to customize a View and override the onDraw method. In this example, the progress is to enable a thread, and then imitate the increasing progress, then, you can call a custom method to upload the progress value to the custom View through the custom View and re-paint it based on the progress.
Plotting is divided into three parts:
1. Draw the upper half shadow area of a rectangle (image area;
2. Draw a non-shadow area in the lower half of a rectangle (image area;
3. Draw the intermediate progress value (text );
OnDraw code:
@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); mPaint. setAntiAlias (true); // eliminate the Sawtooth 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 SS/100, getWidth (), getHeight (), mPaint); mPaint. setTextSize (30); 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 );}Method for passing in the Progress value:
public void setProgress(int progress){this.progress=progress;postInvalidate();};Main Interface call method:
CustomView = (CustomView6) findViewById (R. id. customView); // simulate the Image Upload progress new Thread (new Runnable () {@ Overridepublic void run () {while (true) {if (progress = 100) {// handler is completed after the image is uploaded. sendEmptyMessage (SUCCESS); return;} progress ++; customView. setProgress (progress); try {Thread. sleep (200); // pause for 0.2 seconds} catch (InterruptedException e) {e. printStackTrace ();}}}}). start ();
Demo: http://download.csdn.net/detail/baiyuliang2013/8690773