Android implements animation through PathMeasure and Path

  Recently, I have been learning the android framework, and I have not blogged much. A few days ago, I saw a blog posted on WeChat, and I thought the animation was cool, so I studied it. Here I just implement it briefly, and talk about the implementation principle and the pits encountered. If you are interested, you can refer to: http://blog.csdn.net/zxt0601/article/details/53040506
This is a detailed introduction. There are some differences in implementation.
 
Rendering:



  Here I implement PathMeasure and Path, briefly introduce PathMeasure
  PathMeasure is used to operate Path, initialize
mPathMeasure=new PathMeasure();
        //forceClosed is whether the Path needs to be closed in the end. If it is True, it will be closed regardless of whether the associated Path is closed or not. The calculation of PathMeasure will include the last closed path.
        mPathMeasure.setPath(mPath,false);



To get the length of the path, it can be understood that no matter how complicated the actual Path is, PathMeasure is equivalent to doing one thing, that is, "straightening" the Path, and then giving us an interface (getLength) to tell us the total length of the path,
mPathMeasure.getLength()

Note that the length obtained here is the length of the line segment pointed to by the current mPathMeasure, not the total length of the path. If you want to get the total length, you need to traverse mPathMeasure through nextContour, get the length of each segment and add it up to



get a point in the Path or a segment
getPosTan(float distance, float[] pos, float[] tan)
getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo) It

should be noted here that the obtained line segment is added to dst, not reassigned to dst. To regenerate a line segment, you can reset dst path.reset().
startWithMoveTo
to true: intercept again, when the starting point is 0, it is still the starting point of the original path.
If false: intercept again, when the starting point is 0, it is the end point of the last interception.
startD stopD is the start and starting position of the line segment pointed to by the current PathMeasure.


Path can be composed of multiple curves, but whether it is getLength, getgetSegment or other methods, it will only run on the first line segment, and this nextContour is used for Jump to the next curve to method, return true if the jump is successful, and return false if the jump fails.

It should be noted here that calling mPathMeasure.nextContour() directly at the beginning will jump to the first line segment, but if you operate mPathMeasure, such as getLength, it will automatically jump to the first line segment.


The implementation principle is very simple. Generate a path for the pattern you want to display, draw it in onDraw, and then continuously get the path you want to highlight through mPathMeasure, and then draw a

demo in onDraw: https://github.com/592713711/HeartAnimDemo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326463655&siteId=291194637