Android Custom View Cool digital ring _android

Source: Internet
Author: User

Let's look at the final effect.

First, begin to realize
Create a new Doughnutview inheritance view

  public class Doughnutview extends View {

  }

Rewrite the Onmeasure method first.

 /**
   * When layout is wrap_content set default width
   *
   * @param widthmeasurespec * *
   @param heightmeasurespec *
  Override
  protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
    setmeasureddimension ( Measure (Widthmeasurespec), Measure (Heightmeasurespec));

  private int measure (int origin) {
    int result = Default_min_width;
    int specmode = Measurespec.getmode (origin);
    int specsize = Measurespec.getsize (origin);
    if (Specmode = = measurespec.exactly) {result
      = specsize;
    } else {
      if (Specmode = = measurespec.at_most) {
   result = Math.min (result, specsize);
      }
    }
    return result;
  }

The following are the most important rewrite OnDraw methods, which are generally as follows
1, draw the white ring (background), remember to change the activity background color or white ring can not see out.

 Draw background White ring
 initpaint ();
 float Doughnutwidth = math.min (width, height)/2 * 0.15f;
 Paint.setstrokewidth (doughnutwidth);
 Paint.setstyle (Paint.Style.STROKE);
 Paint.setcolor (color.white);
 Paint.setantialias (true);
 RECTF RECTF = new RECTF (width > height?) Math.Abs (width-height)/2:0) + DOUGHNUTWIDTH/2, (height > width?) Math.Abs (height-width)/2:0) + DOUGHNUTWIDTH/2, Width-(Width > height) Math.Abs (width-height)/2:0)-DOUGHNUTWIDTH/2, Height-(height > width?) Math.Abs (height-width)/2:0)-DOUGHNUTWIDTH/2);
 Canvas.drawarc (RECTF, 0, 360, false, paint);

2. Draw the color ring

Using sweepgradient to achieve the effect of the ring gradient, there is a judge when the set of color array only one color, direct ' setcolor ', more than one color to use sweepgradient to achieve gradient. This will support both gradient and monochrome.

It's also important to note that the Sweepgradient default is to start the gradient from 3 o'clock, so that the canvas rotates -90° to allow it to begin the gradient from 12 o'clock.

 Draw Color Ring
 initpaint ();
 Canvas.rotate ( -90, WIDTH/2, HEIGHT/2);
 Paint.setstrokewidth (doughnutwidth);
 Paint.setstyle (Paint.Style.STROKE);
 if (Doughnutcolors.length > 1) {
   Paint.setshader (new Sweepgradient (WIDTH/2, HEIGHT/2, doughnutcolors, null)); 
   } else {
   paint.setcolor (doughnutcolors[0]);
 }
 Canvas.drawarc (RECTF, 0, CurrentValue, false, paint);

3. Draw the white background of the median value (just to make the numerical display more obvious)

 Draw an intermediate value of the background
 int fontsize = m;
 Initpaint ();
 Paint.setstyle (Paint.Style.FILL);
 Paint.setcolor (color.white);
 Canvas.drawcircle (WIDTH/2, HEIGHT/2, FontSize * 2, paint);

4. Draw the middle value

 Draw Intermediate numerical
 canvas.rotate (WIDTH/2, HEIGHT/2);
 Initpaint ();
 Paint.setcolor (Colorutils.getcurrentcolor (currentvalue/360f, doughnutcolors));
 Paint.settextsize (fontsize);
 Paint.settextalign (Paint.Align.CENTER);
 float BaseLine = HEIGHT/2-(Paint.getfontmetrics (). Descent + Paint.getfontmetrics (). Ascent)/2;
 Canvas.drawtext ((int) (CURRENTVALUE/360F *) + "%", WIDTH/2, BaseLine, paint);

Here are two points of comparison pits:

1, the color of the value

The effect is to have the color of the value be the same as the color of the end of the color ring. Searching for a long time and not finding a way to get sweepgradient rendering to an angle color =_=

Finally spent almost half a day to write a color gradient algorithm, the code is as follows:

/** * Color Gradient algorithm * Get gradient color value under a percentage * * @param percent * @param colors * @return * * p
   ublic static int Getcurrentcolor (float percent, int[] colors) {float[][] f = new float[colors.length][3];
     for (int i = 0; i < colors.length i++) {f[i][0] = (Colors[i] & 0xff0000) >> 16;
     F[I][1] = (Colors[i] & 0x00ff00) >> 8;
   F[I][2] = (Colors[i] & 0X0000FF);
   } float[] result = new FLOAT[3]; for (int i = 0; i < 3; i++) {for (int j = 0; J < F.length; J +) {if (f.length = 1 | | | percent = j/(
       f.length-1f)) {result = F[j]; else {if (Percent > J/(F.LENGTH-1F) && percent < (j + 1f)/(f.length-1)) {res
         Ult[i] = f[j][i]-(f[j][i]-f[j + 1][i]) * (Percent-j/(F.LENGTH-1F)) * (f.length-1f);
 }}} return Color.rgb (int) result[0], (int) result[1], (int) result[2]); }

2, the Value center alignment problem

The DrawText is positioned according to the baseline. You can look at the following two article analysis: Article one, article two. The number may be slightly different from the center of the letter.

Second, the realization of animation effect
First code:

public void SetValue (float value) {
Valueanimator valueanimator = valueanimator.offloat (currentvalue, value);
Valueanimator.setduration ();
Valueanimator.setinterpolator (New Interpolator () {
  @Override public
  float getinterpolation (float v) {
    return n (1-v) * (1-v) * (1-V);
  }
);
Valueanimator.addupdatelistener (New Valueanimator.animatorupdatelistener () {
  @Override public
  Void Onanimationupdate (Valueanimator valueanimator) {
    CurrentValue = (float) valueanimator.getanimatedvalue ();
    Invalidate ();
  }
);
Valueanimator.start ();
}

Use Valueanimator to achieve animation effects. You can also set different interpolation to achieve different animation effects:

 Valueanimator.setinterpolator (New Accelerateinterpolator ())//Acceleration
  valueanimator.setinterpolator (New Decelerateinterpolator ())//Deceleration
  valueanimator.setinterpolator (New Acceleratedecelerateinterpolator ())/accelerated deceleration
  Valueanimator.setinterpolator (New Linearinterpolator ());//Cloud Speed

The introduction of common interpolation can be seen in this article.

Of course, you can also implement a simple interpolation device:

Valueanimator.setinterpolator (New Interpolator () {
  @Override public
  float getinterpolation (float v) {
    return n (1-v) * (1-v) * (1-V);
  }
);

The above is the entire content of this article, I hope to help you learn.

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.