Android Graphics Patheffect class

Source: Internet
Author: User

The 1.PathEffect API sample gives instructions on how to apply each effect.
So far, all the effects have affected how the paint fills the image, and Patheffect is used to control how the outlines (lines) are drawn.

Patheffect is especially useful for drawing path basic graphics, but they can also be applied to any paint to affect how lines are drawn.

With Patheffect, you can change the appearance of a corner of a shape and control the appearance of the contour. Android contains several patheffect, including:

Cornerpatheffect can use rounded corners instead of sharp corners to smooth the sharp corners of the base shape.

Dashpatheffect can use Dashpatheffect to create a dashed outline (dash/dot) instead of using solid lines. You can also specify a repeating pattern for any virtual/real segments.

Discretepatheffect is similar to dashpatheffect, but it adds randomness. When drawing it, you need to specify the length of each segment and the degree of deviation from the original path.

Pathdashpatheffect This effect can define a new shape (path) and use it as an outline marker for the original path.

The following effect can be combined with multiple path Effect in a single paint.

Composepatheffect combines the two effects, using the first effect, and then applying the second effect based on the effect.


Sumpatheffect sequentially adds two effects to a path so that each effect can be applied to the original path, and the two results can be combined.


Changes in the patheffect of the object shape affect the area of the shape. This ensures that the fill effect applied to the same shape will be drawn to the new boundary.

Use the Setpatheffect method to apply the Patheffect to the Paint object.

2. The following example will use the above drawing effect

1  PackageCom.example.myandroidtest2;2 3 ImportAndroid.annotation.SuppressLint;4 Importandroid.app.Activity;5 ImportAndroid.content.Context;6 ImportAndroid.graphics.Canvas;7 ImportAndroid.graphics.Color;8 ImportAndroid.graphics.ComposePathEffect;9 ImportAndroid.graphics.CornerPathEffect;Ten ImportAndroid.graphics.DashPathEffect; One ImportAndroid.graphics.DiscretePathEffect; A ImportAndroid.graphics.Paint; - ImportAndroid.graphics.Path; - ImportAndroid.graphics.PathDashPathEffect; the ImportAndroid.graphics.PathEffect; - ImportAndroid.graphics.SumPathEffect; - ImportAndroid.os.Bundle; - ImportAndroid.view.View; +  -  Public classPathtestextendsActivity { +     /**Called when the activity is first created.*/ A @Override at      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); -Setcontentview (NewMyView ( This)); -     } -  -     classMyViewextendsView { in         floatphase; -patheffect[] Effects =NewPatheffect[7]; to         int[] colors; +         Privatepaint paint; - path Path; the  *          PublicMyView (Context context) { $             Super(context);Panax NotoginsengPaint =NewPaint (); - Paint.setstyle (Paint.Style.STROKE); thePaint.setstrokewidth (4); +             //Create, and initialize path APath =NewPath (); thePath.moveto (0, 0); +              for(inti = 1; I <= 15; i++) { -                 //generates 15 points, randomly generates their coordinates, and connects them to a path $Path.lineto (I * 20, (float) Math.random () * 60); $             } -             //Initialize seven colors -colors =New int[] {color.black, Color.Blue, Color.cyan, Color.green, Color.magenta, Color.Red, the Color.yellow}; -         }Wuyi  the@SuppressLint ("Drawallocation") -         protected voidOnDraw (canvas canvas) { Wu             //fill the background with white - Canvas.drawcolor (color.white); About             //-------Start initializing the effect of the path in 7 $             //using the path effect -Effects[0] =NULL; -             //using the Cornerpatheffect path effect -EFFECTS[1] =NewCornerpatheffect (10); A             //Initialize Discretepatheffect +EFFECTS[2] =NewDiscretepatheffect (3.0f, 5.0f); the             //Initialize Dashpatheffect -EFFECTS[3] =NewDashpatheffect (New float[] {20, 10, 5, 10}, phase); $             //Initialize Pathdashpatheffect thePath p =NewPath (); theP.addrect (0, 0, 8, 8, Path.Direction.CCW); theEFFECTS[4] =NewPathdashpatheffect (P, 12, phase, PathDashPathEffect.Style.ROTATE); the             //Initialize Pathdashpatheffect -EFFECTS[5] =NewComposepatheffect (effects[2], effects[4]); inEFFECTS[6] =NewSumpatheffect (Effects[4], effects[3]); the             //move the canvas to 8, 8 to start drawing theCanvas.translate (8, 8); About             //use different path effects in 7, 7 different colors to draw the path the              for(inti = 0; i < effects.length; i++) { the Paint.setpatheffect (Effects[i]); the Paint.setcolor (Colors[i]); + Canvas.drawpath (path, paint); -Canvas.translate (0, 60); the             }Bayi             //change the phase value to create an animated effect thePhase + = 1; the invalidate (); -         } -     } the}

Android Graphics Patheffect class

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.