One of the implementation of the wheel progressbar -- draw an arc

Source: Internet
Author: User
1. Implementation principle:

Wheel progressbar (rolling progress bar). In the native Android progress bar, there is only an indeterminate scroll progress bar. Now we can implement a determinate progress bar to display the rolling progress bar of a specific progress, shows the effect:



We will explain in three steps: 1. How to draw an arc accurately; 2. How to accurately draw the progress text; 3. How to dynamically simulate the progress process. The three steps have basically explained the implementation principle of the final rolling progress bar: dynamically draw an Arc Based on the progress and display the progress text. 2. Draw arc principle:To draw an arc in Android, you must first provide a rectangular area, then, you can draw an arc according to the set brush parameters, starting point degrees, and arc spans in the limited rectangle area. The specific interface is Android. Graphics. Canvas. drawarc (rectf oval, float startangle, float sweepangle, Boolean usecenter, paint)
(Http://developer.android.com/reference/android/graphics/Canvas.html#drawArc (Android. Graphics. rectf, float, float, Boolean, Android. Graphics. Paint) here focuses on how to accurately draw an arc within a limited area. For ease of description, we take an arc of 360 degrees (that is, a circle) as an example. Let's first look at one:

If we want to draw an arc with the width of W1, we know that we must first set the area r_xx of a square, which has two areas: r_out and r_in, the interval between r_out and r_in is W2 = W1/2. I believe many people will choose r_out from the very beginning. In fact, when draw an arc in Android, r_in is the right choice. That is, we need to draw an arc with a diameter of D and a width of W, size of the square area of the painting that must be set
The relationship of size is: size = D-W, which has the following relationship: r_in_size = r_out_size-W13. Specific implementation:1. Customize a class customarc and inherit from the view; 2. Override the onattachedtowindow () method and set the circular arc square area (rectf marcbounds;) and the brush parameter (paint marcpaint;): @ override
Public void onattachedtowindow (){
Super. onattachedtowindow ();
Marcbounds = new rectf ((Marcwidth + 1)/2 ,(Marcwidth + 1)/2, getlayoutparams (). Width -(Marcwidth + 1)/2, getlayoutparams (). Height -(Marcwidth + 1)/2); marcpaint. setcolor (marccolor); marcpaint. setantialias (true); marcpaint. setstyle (style. Stroke); marcpaint. setstrokewidth (marcwidth); invalidate ();}
Pay special attention to the bold part of the Code: marcwidth + 1. Because the pixel value is an integer, when the width of the paint brush (that is, the width of the circular arc marcwidth) is an odd number, there is a pixel difference between the set square area and the actual ideal area's wide and high values. In this case, we should take a small area to ensure that the drawn arc will not overflow a pixel around it. 3. Override the ondraw () method and draw an arc: @ override
Protected void ondraw (canvas ){
Super. ondraw (canvas );
Canvas. drawarc (marcbounds,-90,360, false, marcpaint);} 4. Complete Project download: customarc4. Reference:Progresswheel: https://github.com/Todd-Davies/ProgressWheel



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.