Example usage for android.graphics PathMeasure nextContour

List of usage examples for android.graphics PathMeasure nextContour

Introduction

In this page you can find the example usage for android.graphics PathMeasure nextContour.

Prototype

public boolean nextContour() 

Source Link

Document

Move to the next contour in the path.

Usage

From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java

private static void setupPathMotion(Path path, ObjectAnimator oa, float precision, String propertyXName,
        String propertyYName) {//from   w w w .  j a  va 2 s .c  om
    // Measure the total length the whole path.
    final PathMeasure measureForTotalLength = new PathMeasure(path, false);
    float totalLength = 0;
    // The sum of the previous contour plus the current one. Using the sum here b/c we want to
    // directly substract from it later.
    ArrayList<Float> contourLengths = new ArrayList<>();
    contourLengths.add(0f);
    do {
        final float pathLength = measureForTotalLength.getLength();
        totalLength += pathLength;
        contourLengths.add(totalLength);

    } while (measureForTotalLength.nextContour());

    // Now determine how many sample points we need, and the step for next sample.
    final PathMeasure pathMeasure = new PathMeasure(path, false);

    final int numPoints = min(MAX_NUM_POINTS, (int) (totalLength / precision) + 1);

    float[] mX = new float[numPoints];
    float[] mY = new float[numPoints];
    final float[] position = new float[2];

    int contourIndex = 0;
    float step = totalLength / (numPoints - 1);
    float currentDistance = 0;

    // For each sample point, determine whether we need to move on to next contour.
    // After we find the right contour, then sample it using the current distance value minus
    // the previously sampled contours' total length.
    for (int i = 0; i < numPoints; ++i) {
        pathMeasure.getPosTan(currentDistance, position, null);
        pathMeasure.getPosTan(currentDistance, position, null);

        mX[i] = position[0];
        mY[i] = position[1];
        currentDistance += step;
        if ((contourIndex + 1) < contourLengths.size()
                && currentDistance > contourLengths.get(contourIndex + 1)) {
            currentDistance -= contourLengths.get(contourIndex + 1);
            contourIndex++;
            pathMeasure.nextContour();
        }
    }

    // Given the x and y value of the sample points, setup the ObjectAnimator properly.
    PropertyValuesHolder x = null;
    PropertyValuesHolder y = null;
    if (propertyXName != null) {
        x = PropertyValuesHolder.ofFloat(propertyXName, mX);
    }
    if (propertyYName != null) {
        y = PropertyValuesHolder.ofFloat(propertyYName, mY);
    }
    if (x == null) {
        oa.setValues(y);
    } else if (y == null) {
        oa.setValues(x);
    } else {
        oa.setValues(x, y);
    }
}

From source file:com.github.jorgecastillo.FillableLoader.java

private void buildPathData() {
    SvgPathParser parser = getPathParser();
    pathData = new PathData();
    try {//from w w  w . j ava  2  s  . c  o m
        pathData.path = parser.parsePath(svgPath);
    } catch (ParseException e) {
        pathData.path = new Path();
    }

    PathMeasure pm = new PathMeasure(pathData.path, true);
    while (true) {
        pathData.length = Math.max(pathData.length, pm.getLength());
        if (!pm.nextContour()) {
            break;
        }
    }
}

From source file:cc.kenai.common.AnimatedSvgView.java

private void rebuildGlyphData() {
    SvgPathParser parser = new SvgPathParser() {
        @Override/*w w  w. j av a2  s  . c om*/
        protected float transformX(float x) {
            return x * mWidth / mViewport.x;
        }

        @Override
        protected float transformY(float y) {
            return y * mHeight / mViewport.y;
        }
    };

    Log.i(TAG, "---mWidth = " + mWidth + "---mViewport.x = " + mViewport.x);
    Log.i(TAG, "mGlyphStrings.length = " + mGlyphStrings.length);
    mGlyphData = new GlyphData[mGlyphStrings.length];
    for (int i = 0; i < mGlyphStrings.length; i++) {
        mGlyphData[i] = new GlyphData();
        try {
            mGlyphData[i].path = parser.parsePath(mGlyphStrings[i]);
        } catch (ParseException e) {
            mGlyphData[i].path = new Path();
            Log.e(TAG, "Couldn't parse path", e);
        }
        PathMeasure pm = new PathMeasure(mGlyphData[i].path, true);
        while (true) {
            mGlyphData[i].length = Math.max(mGlyphData[i].length, pm.getLength());
            if (!pm.nextContour()) {
                break;
            }
        }
        mGlyphData[i].paint = new Paint();
        mGlyphData[i].paint.setStyle(Paint.Style.STROKE);
        mGlyphData[i].paint.setAntiAlias(true);
        mGlyphData[i].paint.setColor(Color.WHITE);
        mGlyphData[i].paint.setStrokeWidth(
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }
}

From source file:com.daitu_liang.study.mytest.svg.AnimatedSvgView.java

private void rebuildGlyphData() {
    SvgPathParser parser = new SvgPathParser() {
        @Override//from  www.  ja  v  a 2 s  . co m
        protected float transformX(float x) {
            return x * mWidth / mViewport.x;
        }

        @Override
        protected float transformY(float y) {
            return y * mHeight / mViewport.y;
        }
    };

    Log.i(TAG, "---mWidth = " + mWidth + "---mViewport.x = " + mViewport.x);
    if (mGlyphStrings != null) {
        mGlyphData = new GlyphData[mGlyphStrings.length];
    }
    for (int i = 0; i < mGlyphStrings.length; i++) {
        mGlyphData[i] = new GlyphData();
        try {
            mGlyphData[i].path = parser.parsePath(mGlyphStrings[i]);
        } catch (ParseException e) {
            mGlyphData[i].path = new Path();
            Log.e(TAG, "Couldn't parse path", e);
        }
        PathMeasure pm = new PathMeasure(mGlyphData[i].path, true);
        while (true) {
            mGlyphData[i].length = Math.max(mGlyphData[i].length, pm.getLength());
            if (!pm.nextContour()) {
                break;
            }
        }
        mGlyphData[i].paint = new Paint();
        mGlyphData[i].paint.setStyle(Paint.Style.STROKE);
        mGlyphData[i].paint.setAntiAlias(true);
        mGlyphData[i].paint.setColor(Color.WHITE);
        mGlyphData[i].paint.setStrokeWidth(
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }
}

From source file:com.donsen.svg.ui.common.AnimatedSvgView.java

private void rebuildGlyphData() {
    SvgPathParser parser = new SvgPathParser() {
        @Override/*from   w w w  .j  a va 2 s.c o m*/
        protected float transformX(float x) {
            return x * mWidth / mViewport.x;
        }

        @Override
        protected float transformY(float y) {
            return y * mHeight / mViewport.y;
        }
    };

    mGlyphData = new GlyphData[mGlyphStrings.length];
    for (int i = 0; i < mGlyphStrings.length; i++) {
        mGlyphData[i] = new GlyphData();
        try {
            mGlyphData[i].path = parser.parsePath(mGlyphStrings[i]);
        } catch (ParseException e) {
            mGlyphData[i].path = new Path();
            Log.e(TAG, "Couldn't parse path", e);
        }
        PathMeasure pm = new PathMeasure(mGlyphData[i].path, true);
        while (true) {
            mGlyphData[i].length = Math.max(mGlyphData[i].length, pm.getLength());
            if (!pm.nextContour()) {
                break;
            }
        }
        mGlyphData[i].paint = new Paint();
        mGlyphData[i].paint.setStyle(Paint.Style.STROKE);
        mGlyphData[i].paint.setAntiAlias(true);
        mGlyphData[i].paint.setColor(Color.WHITE);
        mGlyphData[i].paint.setStrokeWidth(
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }
}

From source file:com.jrummyapps.android.widget.AnimatedSvgView.java

/**
 * If you set the SVG data paths more than once using {@link #setGlyphStrings(String...)} you should call this method
 * before playing the animation.//from w  ww . jav  a  2s . co  m
 */
@SuppressWarnings("SuspiciousNameCombination")
public void rebuildGlyphData() {

    float X = mWidth / mViewport.x;
    float Y = mHeight / mViewport.y;

    Matrix scaleMatrix = new Matrix();
    RectF outerRect = new RectF(X, X, Y, Y);
    scaleMatrix.setScale(X, Y, outerRect.centerX(), outerRect.centerY());

    mGlyphData = new GlyphData[mGlyphStrings.length];
    for (int i = 0; i < mGlyphStrings.length; i++) {
        mGlyphData[i] = new GlyphData();
        try {
            mGlyphData[i].path = ExposedPathParser.createPathFromPathData(mGlyphStrings[i]);
            mGlyphData[i].path.transform(scaleMatrix);
        } catch (Exception e) {
            mGlyphData[i].path = new Path();
            Log.e(TAG, "Couldn't parse path", e);
        }
        PathMeasure pm = new PathMeasure(mGlyphData[i].path, true);
        while (true) {
            mGlyphData[i].length = Math.max(mGlyphData[i].length, pm.getLength());
            if (!pm.nextContour()) {
                break;
            }
        }
        mGlyphData[i].paint = new Paint();
        mGlyphData[i].paint.setStyle(Paint.Style.STROKE);
        mGlyphData[i].paint.setAntiAlias(true);
        mGlyphData[i].paint.setColor(Color.WHITE);
        mGlyphData[i].paint.setStrokeWidth(
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }
}

From source file:android.support.graphics.drawable.PathInterpolatorCompat.java

private void initPath(Path path) {
    final PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */);

    final float pathLength = pathMeasure.getLength();
    final int numPoints = min(MAX_NUM_POINTS, (int) (pathLength / PRECISION) + 1);

    if (numPoints <= 0) {
        throw new IllegalArgumentException("The Path has a invalid length " + pathLength);
    }//w ww . j av  a2 s .c  o m

    mX = new float[numPoints];
    mY = new float[numPoints];

    final float[] position = new float[2];
    for (int i = 0; i < numPoints; ++i) {
        final float distance = (i * pathLength) / (numPoints - 1);
        pathMeasure.getPosTan(distance, position, null /* tangent */);

        mX[i] = position[0];
        mY[i] = position[1];
    }

    if (abs(mX[0]) > EPSILON || abs(mY[0]) > EPSILON || abs(mX[numPoints - 1] - 1) > EPSILON
            || abs(mY[numPoints - 1] - 1) > EPSILON) {
        throw new IllegalArgumentException("The Path must start at (0,0) and end at (1,1)" + " start: " + mX[0]
                + "," + mY[0] + " end:" + mX[numPoints - 1] + "," + mY[numPoints - 1]);

    }

    float prevX = 0;
    int componentIndex = 0;
    for (int i = 0; i < numPoints; i++) {
        float x = mX[componentIndex++];
        if (x < prevX) {
            throw new IllegalArgumentException("The Path cannot loop back on itself, x :" + x);
        }
        mX[i] = x;
        prevX = x;
    }

    if (pathMeasure.nextContour()) {
        throw new IllegalArgumentException("The Path should be continuous," + " can't have 2+ contours");
    }
}