Android custom View allows you to display the upload progress of images (like QQ) and Android Images

Source: Internet
Author: User

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

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.