Android Basics Getting Started tutorial--8.3.12 Paint API--patheffect (path effect)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
This section continues to learn the Api--patheffect (path effect) of paint, and we set the sytle of the brush to a stroke, which can be
Draw a graph of lines that occasionally look monotonous, like you want to change these into dashed lines and
Or if you want the corners of the path to be sleek and so on, then you can consider using this patheffect to achieve it!
Official API Document: Patheffect
In the document, you can find this patheffect and the Maskfilter (mask) and colorfilter (color
Filter), there are few available methods:
We generally use his six subcategories:
Composepatheffect,cornerpatheffect,dashpatheffect
Discretepatheffect,pathdashpatheffect,sumpatheffect
Let's analyze their function and how to construct it in turn!
1. Sub-class function and construction method parameter analysis: 1) cornerpatheffect
Cornerpatheffect (float radius)
The angle between each connected segment of path is connected in a smoother way, similar to the effect of arcs and tangents.
Radius is the radius of the specified arc!
2) Dashpatheffect
Dashpatheffect (float[] intervals, float phase)
The segment of path is dashed, intervals is a dashed array of on and off, and the number of elements in the array needs to be >= 2;
The phase is the offset at the time of drawing!
3) Discretepatheffect
Discretepatheffect (float segmentlength, float deviation)
Break the path of the line, so that the original path based on the break-through effect.
The Segmentlength specifies the largest segment length, and the deviation is the amount of deviation when plotted.
4) Pathdashpatheffect
Pathdashpatheffect (Path shape, float advance, float phase, Pathdashpatheffect.style Style)
The function is to use the path graph to fill the current path, shape refers to the fill shape, advance is the interval between each graph,
The style is the free enumeration value of this class, in three cases:ROTATE,MORPH , and TRANSLATE
Rotate case: The graphic transformation at the segment connection is rotated to connect at an angle that is consistent with the next moving direction
Morph case: The drawing is connected to the next segment in the case of deformation such as stretching or compression
Translate: The drawing is connected to the next segment in a position translation
5) Composepatheffect
Composepatheffect (Patheffect Outerpe, Patheffect Innerpe)
The function is: the combination effect , will first turn Innerpe out, then on the basis of Innerpe to increase the Outerpe effect!
6) Sumpatheffect
Sumpatheffect (Patheffect First, patheffect second)
The effect is: the superposition effect, and composepatheffect different, in the performance will be two parameters of the effect of independent expression,
Then the two effects are simply overlapped and displayed together!
2. Write code to verify the respective effects
More useless, write code the most practical, we write down the code to try these sub-categories of their respective effects!
Run :
Implementation Code :
We write a view ourselves, inside the effect of the line movement is phase increase, each time + 2,
And then invalidate it again, so don't be surprised! Patheffectview.java:
/** * Created by Jay on 2015/10/30 0030. * * Public class patheffectview extends View { PrivatePaint Mpaint;PrivatePath MPath;Private floatPhase =0;Privatepatheffect[] Effects =Newpatheffect[7];Private int[] colors; Public Patheffectview(Context context) { This(Context,NULL); } Public Patheffectview(context context, AttributeSet attrs) {Super(context, attrs); Init (); } Public Patheffectview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }//Initialize brush Private void Init() {Mpaint =NewPaint (Paint.anti_alias_flag);//anti-aliasingMpaint.setstyle (Paint.Style.STROKE);//Painting style: HollowMpaint.setstrokewidth (5);//Stroke thicknessMPath =NewPath (); Mpath.moveto (0,0); for(inti =1; I <= the; i++) {//Generate 15 points, randomly generate their coordinates and link them to a pathMpath.lineto (i * +, (float) Math.random () * -); }//Initialize 7 colorscolors =New int[] {color.red, Color.Blue, Color.green, Color.yellow, Color.Black, Color.magenta, Color.cyan}; }@Override protected void OnDraw(Canvas canvas) {Canvas.drawcolor (color.white);//Initialize one of the path effects:effects[0] =NULL;//No effecteffects[1] =NewCornerpatheffect (Ten);//cornerpatheffecteffects[2] =NewDiscretepatheffect (3.0F5.0f);//discretepatheffecteffects[3] =NewDashpatheffect (New float[] { -,Ten,5,Ten},phase);//dashpatheffectPath p =NewPath (); P.addrect (0,0,8,8, Path.Direction.CCW); effects[4] =NewPathdashpatheffect (P, A, phase, PathDashPathEffect.Style.ROTATE);//pathdashpatheffecteffects[5] =NewComposepatheffect (effects[2], effects[4]);//composepatheffecteffects[6] =NewSumpatheffect (effects[2], effects[4]);//sumpatheffect //Move the canvas to start drawing at (10,10)Canvas.translate (Ten,Ten);//Use different path effects in 7, different colors in 7 to draw the path for(inti =0; i < effects.length; i++) {mpaint.setpatheffect (effects[i]); Mpaint.setcolor (Colors[i]); Canvas.drawpath (MPath, mpaint); Canvas.translate (0, -); }//Change the phase value to form an animated effectPhase + =2; Invalidate (); }}
OK, the code comments are very clear, and here is not nagging ~
3. The sample code for this section is downloaded:
Patheffectdemo.zip
Summary of this section
This section has nothing to do with the role of the six subclasses of Patheffect, just call Setpatheffect
Method applied to the Paint object, very simple ~ well, say so much, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--8.3.12 Paint API--patheffect (path effect)