Detailed setpatheffect of paint (Patheffect effect)

Source: Internet
Author: User

First, Setpatheffect ()

This method is related to path, as the name implies, it is to the path to set the style (effect). Patheffect This path effect class does not have a specific implementation, the effect is implemented by its six subclasses:

These six subclasses can implement different path effects, respectively:

Here, let's use the code to implement it in detail.

Second, the test code

2.1 Code Framework

First initialize the paint and path, then configure the related properties and finally paint.

 PackageCom.kale.cview; Public classCustomViewextendsView {//Instantiate a brush    PrivatePaint Mpaint =NULL; PrivatePath MPath;//Path Object    PrivateContext Mcontext;  PublicCustomView (Context context) {Super(context); }    /*** When you want to add attribute to the view, you need to use this construct *@paramContext *@paramAttrs*/     PublicCustomView (Context context, AttributeSet attrs) {Super(context, attrs); Mcontext=context; //Initializing brushesInitpaint ();    Initpath (); }    Private voidInitpaint () {//instantiate a brush and turn on anti-aliasing//mpaint = new Paint (paint.anti_alias_flag);Mpaint =NewPaint ();          Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokewidth (5);      Mpaint.setcolor (Color.dkgray); }    Private voidInitpath () {//instantiating a pathMPath =NewPath (); //define the starting point of the pathMpath.moveto (10, 50); //define each point of the path         for(inti = 0; I <= 30; i++) {Mpath.lineto (i* 35, (float) (Math.random () * 100)); }    }    /** The method called when drawing the view, there may be multiple calls, so it is not recommended to instantiate the object here, that is, do not appear new * * @param canvas A Canvas object, we can paint on it*/@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); /** Draw Path*/        //Did not do the processing, has not written the code       Canvas.drawpath (MPath, mpaint); }}

2.2 Do not set effect

    @Override    protectedvoid  onDraw (canvas canvas) {        super . OnDraw (canvas);         // Move the canvas down        . Canvas.translate (0, +);           /*          * Draw path          *///           No processing, show blunt        Mpaint.setpatheffect ( NULL );        Canvas.drawpath (MPath, mpaint);    }

2.3 Cornerpatheffect

Cornerpatheffect can turn the corner of the path into a sleek, cornerpatheffect construction method that accepts only one parameter radius, meaning the degree of smoothness at the corner.

    @Override    protectedvoid  onDraw (canvas canvas) {        super . OnDraw (canvas);         // Move the canvas down        . Canvas.translate (0, +);           /*          * Draw Path         *         /mpaint.setpatheffect (new cornerpatheffect);        Canvas.drawpath (MPath, mpaint);    }

2.4 Discretepatheffect

The Discretepatheffect (discrete path effect) is relatively slightly more complex, and it draws a lot of "clutter" on the path to simulate the effect of a similar rusty wire. Its construction method has two parameters:

The first one. Specifies the density of these prominent "clutter points", the smaller the value the more dense the dot;

The second parameter is the "noise" of the prominent size, the greater the value of the greater the distance and vice versa.

    @Override    protectedvoid  onDraw (canvas canvas) {        super . OnDraw (canvas);         // Move the canvas down        . Canvas.translate (0, +);           /*          * Draw Path         *         /mpaint.setpatheffect (New Discretepatheffect (3.0F, 5.0F));        Canvas.drawpath (MPath, mpaint);    }

when we set the dot density to be large, to highlight the distance of the hour, You will find that the lines have softened up.

    @Override    protectedvoid  onDraw (canvas canvas) {        super . OnDraw (canvas);         // Move the canvas down        . Canvas.translate (0, +);           /*          * Draw Path         *         /mpaint.setpatheffect (New Discretepatheffect (10.0F, 2.0F));        Canvas.drawpath (MPath, mpaint);    }

2.5 Dashpatheffect

Its effect is slightly more complex than the above two path effects, although it also contains two parameters:

The first parameter is a floating-point array, what is the meaning of this array? In fact, we define this parameter as long as the number of elements in the floating-point array is greater than or equal to 2, which means that our code can be written like this:

  @Override  protected  void   OnDraw (canvas canvas) { super.ondraw (canvas);  //  move the canvas down.  canvas.translate (          0, 250);  /*   * Draw path  */  Mpaint.setpatheffect ( new  Dashpatheffect (new  float  [] {20, 10}, 1< Span style= "color: #000000;"        >));    Canvas.drawpath (MPath, mpaint); }

Loat[] {20, 10} The even parameter 20 (note that the array subscript is starting from 0 OH) defines the length of our first solid line, and the odd parameter 10 represents the length of the first dash, and if there is no data behind the array then repeats the first number in this reciprocating loop, the entire line becomes [ 20,10,20,10,20,10 ....., ..... ..............] Such a state. Of course if you want to set each solid and dashed line, you can do this:

New Dashpatheffect (newfloat[] {5, ten, Ten, 5}, mphase);  

The second parameter of Dashpatheffect (phase), which I call an offset value, dynamically changes its value to animate the path.

@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //move the canvas down .Canvas.translate (0, 250); /** Draw Path*/Mpaint.setpatheffect (NewDashpatheffect (New float[] {20, 10}, phase));                Canvas.drawpath (MPath, mpaint); //change the offset valuephase++; //Redraw, animateinvalidate (); }

2.6 Pathdashpatheffect

Pathdashpatheffect and Dashpatheffect are similar, the difference is that pathdashpatheffect can let us define the style of the path dashed line, for example, we change it into a small circle consisting of dashed lines:

@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //move the canvas down .Canvas.translate (0, 250); /** Draw Path*/Path Path=NewPath (); Path.addcircle (0, 0, 3, DIRECTION.CCW); Patheffect Patheffect=NewPathdashpatheffect (Path, 12, phase, PathDashPathEffect.Style.ROTATE);        Mpaint.setpatheffect (Patheffect);                Canvas.drawpath (MPath, mpaint); //change the offset valuephase++; //Redraw, animateinvalidate (); }

2.7 Composepatheffect and Sumpatheffect

Both Composepatheffect and sumpatheffect can be used to combine two kinds of path effects, that is, two effects in one. The only difference is the way of grouping:

Composepatheffect (Patheffect Outerpe, Patheffect Innerpe) will first turn the path into Innerpe effect, and then to compound Outerpe path effect, namely: Outerpe (path);

Sumpatheffect (Patheffect First, patheffect second) adds two path effects to the path.

Most of this article is from:

I did a deletion and collation of the original text, the code has knocked on their own. Recorded here, only for the purpose of learning notes.

Detailed setpatheffect of paint (Patheffect effect)

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: 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.