的path数据复杂度也不一样,但格式也算是非常的清楚,即采用一定的指令把数据点进行拼接;
现在有了这些数据点,我们需要做的则是对数据进行解析,封装成我们要的Path;
解析的过程也无非是 遇到指令则采用android Path 里的对应方法进行置换,解析方式如下:
public Path parsePath(String s) throws ParseException {
? ? ? ? mCurrentPoint.set(Float.NaN, Float.NaN);
? ? ? ? mPathString = s;
? ? ? ? mIndex = 0;
? ? ? ? mLength = mPathString.length();
? ? ? ? PointF tempPoint1 = new PointF();
? ? ? ? PointF tempPoint2 = new PointF();
? ? ? ? PointF tempPoint3 = new PointF();
? ? ? ? Path p = new Path();
? ? ? ? p.setFillType(Path.FillType.WINDING);
? ? ? ? boolean firstMove = true;
? ? ? ? while (mIndex < mLength) {
? ? ? ? ? ? char command = consumeCommand();
? ? ? ? ? ? boolean relative = (mCurrentToken == TOKEN_RELATIVE_COMMAND);
? ? ? ? ? ? switch (command) {
? ? ? ? ? ? ? ? case 'M':
? ? ? ? ? ? ? ? case 'm': {
? ? ? ? ? ? ? ? ? ? // m指令,相当于android 里的 moveTo()
? ? ? ? ? ? ? ? ? ? boolean firstPoint = true;
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? relative && mCurrentPoint.x != Float.NaN);
? ? ? ? ? ? ? ? ? ? ? ? if (firstPoint) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? p.moveTo(tempPoint1.x, tempPoint1.y);
? ? ? ? ? ? ? ? ? ? ? ? ? ? firstPoint = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (firstMove) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? firstMove = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? p.lineTo(tempPoint1.x, tempPoint1.y);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 'C':
? ? ? ? ? ? ? ? case 'c': {
? ? ? ? ? ? ? ? ? ? // c指令,相当于android 里的 cubicTo()
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint1, relative);
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint2, relative);
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint3, relative);
? ? ? ? ? ? ? ? ? ? ? ? p.cubicTo(tempPoint1.x, tempPoint1.y, tempPoint2.x, tempPoint2.y,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tempPoint3.x, tempPoint3.y);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint3);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 'L':
? ? ? ? ? ? ? ? case 'l': {
? ? ? ? ? ? ? ? ? ? // 相当于lineTo()进行画直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint1, relative);
? ? ? ? ? ? ? ? ? ? ? ? p.lineTo(tempPoint1.x, tempPoint1.y);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 'H':
? ? ? ? ? ? ? ? case 'h': {
? ? ? ? ? ? ? ? ? ? // 画水平直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? float x = transformX(consumeva lue());
? ? ? ? ? ? ? ? ? ? ? ? if (relative) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x += mCurrentPoint.x;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? p.lineTo(x, mCurrentPoint.y);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 'V':
? ? ? ? ? ? ? ? case 'v': {
? ? ? ? ? ? ? ? ? ? // 画竖直直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ?