Android to achieve gradient-changing arc dashed effect _android

Source: Internet
Author: User

First look at the effect chart:


1,sweepgradient (Gradient rendering)

Public sweepgradient (float CX, float CY, int[] colors, float[] positions)

Scanning rendering, that is, a point in the center of the rotation of a week to form the effect! The parameters are, in turn:

CX: Center x coordinates of the scan

CY: Center y-coordinate of the scan

Colors: color array of gradient gradients

Positions: Specifies the relative position of the color array

public static final int[] Sweep_gradient_colors = new Int[]{color.green, Color.green, Color.Blue, Color.Red, color.red};< C6/>mcolorshader = new Sweepgradient (radius, radius,sweep_gradient_colors,null);

Effect Chart:


Sweepgradient

2,dashpatheffect (dotted line of path segments)

Dashpatheffect (float[] intervals, float phase)

Intervals: An array of the on and off dashes that require >= 2 for the number of elements in the array

Phase: Offset for drawing

The length of the computed path
pathmeasure pathmeasure = new Pathmeasure (MPath, false);
Float length = Pathmeasure.getlength ();
float step = length/60;
Dashpatheffect = new Dashpatheffect (new FLOAT[]{STEP/3, step * 2/3}, 0);

Effect Chart:


Dashpatheffect

3, here are all the code:

Package Com.example.yyw.xfermodedemo;
Import Android.animation.ValueAnimator;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.DashPathEffect;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import Android.graphics.PathMeasure;
Import Android.graphics.RectF;
Import android.graphics.SweepGradient;
Import Android.util.AttributeSet;

Import Android.view.View;
 /** * Created by Yyw on 2016/10/11. * * public class Oiltableline extends View {public static final int[] Sweep_gradient_colors = new Int[]{color.green, Col Or.
 GREEN, Color.Blue, color.red, color.red};
 private int tablewidth = 50;
 Private Paint Mpaint;
 Private Path MPath;
 Private RECTF MTABLERECTF;
 Private Dashpatheffect dashpatheffect which divides the path into virtual segment;
 Color the path private sweepgradient mcolorshader;
 The path of the pointer private path Mpointerpath;

 Private float mcurrentdegree = 60; Public Oiltableline (context, AttributeSet attrs) {Super (context, ATTRS);
  Mpaint = new Paint ();
  Mpaint.setantialias (TRUE);
  Mpaint.setdither (TRUE);
  Mpaint.setcolor (Color.Black);
  MPath = new Path ();
  Mpointerpath = new Path ();

 Startanimator ();
  @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH);
  Float size = Math.min (W, h)-tablewidth * 2;
  The position of the oil gauge box MTABLERECTF = new RECTF (0, 0, size, size);
  Mpath.reset ();
  Add an oil meter path from the starting Radian Mpath.addarc (MTABLERECTF, 60, 240);
  The length of the computed path pathmeasure pathmeasure = new Pathmeasure (MPath, false);
  Float length = pathmeasure.getlength ();
  float step = length/60;

  Dashpatheffect = new Dashpatheffect (new FLOAT[]{STEP/3, step * 2/3}, 0);
  float radius = SIZE/2;
  Mcolorshader = new Sweepgradient (radius, radius,sweep_gradient_colors,null);
  Sets the path position of the pointer Mpointerpath.reset ();
  Mpointerpath.moveto (RADIUS, radius-20);
  Mpointerpath.lineto (RADIUS, radius + 20); Mpointerpath.lineto (RADIUS * 2-tablewidth, RADIUS);
 Mpointerpath.close ();
  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
  float dx = (getwidth ()-mtablerectf.width ())/2;
  float dy = (getheight ()-mtablerectf.height ())/2;
  Pan the square of the oil meter to the positive middle canvas.translate (dx, dy);
  Canvas.save ();
  Rotate Canvas canvas.rotate (mtablerectf.width ()/2, Mtablerectf.height ()/2);
  Mpaint.setstyle (Paint.Style.STROKE);
  Mpaint.setstrokewidth (Tablewidth);
  Mpaint.setpatheffect (Dashpatheffect);
  Mpaint.setshader (Mcolorshader);
  Canvas.drawpath (MPath, mpaint);
  Canvas.restore ();
  Restore brush mpaint.setpatheffect (null);
  Mpaint.setshader (NULL);
  Mpaint.setstyle (Paint.Style.FILL);
  Mpaint.setstrokewidth (TABLEWIDTH/10);
  Canvas.save ();
  Canvas.rotate (+ Mcurrentdegree, mtablerectf.width ()/2, Mtablerectf.height ()/2);
  Canvas.drawpath (Mpointerpath, mpaint);
 Canvas.restore ();
  public void Startanimator () {Valueanimator animator = valueanimator.offloat (0, 240);
  Animator.setduration (40000); AnImator.setrepeatcount (Valueanimator.infinite);
  Animator.setrepeatmode (Valueanimator.restart); Animator.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void onanimationupdate (Valu
    Eanimator animation) {mcurrentdegree = (int) (0 + (Float) animation.getanimatedvalue ());
   Invalidate ();
  }
  });
 Animator.start (); }
}

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.