Java Code Examples for android.graphics.PathMeasure#setPath()

The following examples show how to use android.graphics.PathMeasure#setPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScaleView.java    From ScaleView with Apache License 2.0 6 votes vote down vote up
/**
 * 画指示器
 *
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {
    PathMeasure mPathMeasure = new PathMeasure();
    mPathMeasure.setPath(mArcPath, false);
    float[] tan = new float[2];
    float[] pos = new float[2];
    mPathMeasure.getPosTan(mPathMeasure.getLength() * (0.5f), pos, tan);
    canvas.save();
    double angle = calcArcAngle(Math.atan2(tan[1], tan[0])) + 90;
    canvas.rotate((float) angle, pos[0], pos[1]);
    //画直线
    canvas.drawLine(pos[0], pos[1], pos[0] + 80, pos[1], mIndicatorPaint);
    Path linePath = new Path();
    //画箭头
    linePath.moveTo(pos[0] + 80, pos[1] - 20);
    linePath.lineTo(pos[0] + 80 + 20, pos[1]);
    linePath.lineTo(pos[0] + 80, pos[1] + 20);
    canvas.drawPath(linePath, mIndicatorPaint);
    canvas.restore();
}
 
Example 2
Source File: BrokenAnimator.java    From BrokenView with MIT License 6 votes vote down vote up
/**
 *  Make sure it can be seen in "FILL" mode
 */
private void warpStraightLines() {
    PathMeasure pmTemp = new PathMeasure();
    for (int i = 0; i < mConfig.complexity; i++) {
        if(lineRifts[i].isStraight())
        {
            pmTemp.setPath(lineRifts[i], false);
            lineRifts[i].setStartLength(pmTemp.getLength() / 2);
            float[] pos = new float[2];
            pmTemp.getPosTan(pmTemp.getLength() / 2, pos, null);
            int xRandom = (int) (pos[0] + Utils.nextInt(-Utils.dp2px(1), Utils.dp2px(1)));
            int yRandom = (int) (pos[1] + Utils.nextInt(-Utils.dp2px(1), Utils.dp2px(1)));
            lineRifts[i].reset();
            lineRifts[i].moveTo(0,0);
            lineRifts[i].lineTo(xRandom,yRandom);
            lineRifts[i].lineToEnd();
        }
    }
}
 
Example 3
Source File: GestureDescription.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @param path The path to follow. Must have exactly one contour. The bounds of the path
 * must not be negative. The path must not be empty. If the path has zero length
 * (for example, a single {@code moveTo()}), the stroke is a touch that doesn't move.
 * @param startTime The time, in milliseconds, from the time the gesture starts to the
 * time the stroke should start. Must not be negative.
 * @param duration The duration, in milliseconds, the stroke takes to traverse the path.
 * Must be positive.
 * @param willContinue {@code true} if this stroke will be continued by one in the
 * next gesture {@code false} otherwise. Continued strokes keep their pointers down when
 * the gesture completes.
 */
public StrokeDescription(@NonNull Path path,
        @IntRange(from = 0) long startTime,
        @IntRange(from = 0) long duration,
        boolean willContinue) {
    mContinued = willContinue;
    Preconditions.checkArgument(duration > 0, "Duration must be positive");
    Preconditions.checkArgument(startTime >= 0, "Start time must not be negative");
    Preconditions.checkArgument(!path.isEmpty(), "Path is empty");
    RectF bounds = new RectF();
    path.computeBounds(bounds, false /* unused */);
    Preconditions.checkArgument((bounds.bottom >= 0) && (bounds.top >= 0)
            && (bounds.right >= 0) && (bounds.left >= 0),
            "Path bounds must not be negative");
    mPath = new Path(path);
    mPathMeasure = new PathMeasure(path, false);
    if (mPathMeasure.getLength() == 0) {
        // Treat zero-length paths as taps
        Path tempPath = new Path(path);
        tempPath.lineTo(-1, -1);
        mTapLocation = new float[2];
        PathMeasure pathMeasure = new PathMeasure(tempPath, false);
        pathMeasure.getPosTan(0, mTapLocation, null);
    }
    if (mPathMeasure.nextContour()) {
        throw new IllegalArgumentException("Path has more than one contour");
    }
    /*
     * Calling nextContour has moved mPathMeasure off the first contour, which is the only
     * one we care about. Set the path again to go back to the first contour.
     */
    mPathMeasure.setPath(mPath, false);
    mStartTime = startTime;
    mEndTime = startTime + duration;
    mTimeToLengthConversion = getLength() / duration;
    mId = sIdCounter++;
}
 
Example 4
Source File: SmileView.java    From ToDoList with Apache License 2.0 4 votes vote down vote up
private void initPaint(Context context) {
    drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
            | Paint.FILTER_BITMAP_FLAG);
    lineWidth = dip2px(context, lineWidth);
    radius = dip2px(context, radius);
    path = new Path();
    pathCircle = new Path();
    pathCircle2 = new Path();

    //在path中添加一个顺时针的圆,这时候路径的起点和终点在最后边
    //在画前半部分的脸和运动中的脸,起点在最右边比较方便的计算,但在最后那部分,运动的终点
    //是在圆形的底部,这样把路径圆进行转换到底部,方便计算
    pathCircle.addCircle(0, 0, radius, Path.Direction.CW);
    pathCircle2.addCircle(0, 0, radius, Path.Direction.CW);
    //利用Matrix,让pathCircle中的圆旋转90度,这样它的路径的起点和终点都在底部了
    Matrix m = new Matrix();
    m.postRotate(90);
    pathCircle.transform(m);

    //画脸的笔
    paint = new Paint();
    //画眼睛的笔
    eyePaint = new Paint();

    paint.setColor(blue);
    eyePaint.setColor(blue);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(lineWidth);
    eyePaint.setStrokeWidth(lineWidth);

    //设置画脸的笔的端点为圆角(即起点和终点都是圆角)
    paint.setStrokeCap(Paint.Cap.ROUND);

    //使用PathMeasure计算路径的信息
    pm = new PathMeasure();
    pm.setPath(pathCircle, false);
    pm2 = new PathMeasure();
    pm2.setPath(pathCircle2, false);

    //路径的长度,两个路径都是圆形,因此只计算其中一个即可
    length = pm.getLength();
    eyeRadius = (float) (lineWidth / 2.0 + lineWidth / 5.0);
}
 
Example 5
Source File: ScaleView.java    From ScaleView with Apache License 2.0 4 votes vote down vote up
/**
 * 画刻度线和刻度
 *
 * @param canvas
 */
private void drawScale(Canvas canvas) {
    PathMeasure mPathMeasure = new PathMeasure();
    mPathMeasure.setPath(mArcPath, false);
    float[] pos = new float[2];
    float[] tan = new float[2];
    for (int i = 1; i <= mScaleNumber; i++) {
        float percentage = i / (float) mScaleNumber;//所占百分比
        mPathMeasure.getPosTan(mPathMeasure.getLength() * percentage, pos, tan);
        double atan2 = Math.atan2(tan[1], tan[0]);
        double angle = calcArcAngle(atan2) + 90;//刻度线垂直切线,所以旋转90°

        int scale = Math.round(currentAngle + mScaleMin + i * mScaleSpace);

        if (scale >= mScaleMin && scale % mDrawLineSpace == 0) {
            float startX = pos[0];
            float startY = pos[1];
            float endX = 0;
            float endY = pos[1];
            if (scale % mDrawTextSpace == 0) {
                endX = pos[0] + 80;
                mScaleLinePaint.setStrokeWidth(15);
                mScaleLinePaint.setColor(mScaleLineColor);
                if (currentAngle >= (-(mScaleNumber / 2) * mScaleSpace)) {
                    canvas.save();
                    canvas.rotate((float) (angle + 90), pos[0], pos[1]);
                    String mScaleText = scale + mScaleUnit;
                    float scaleTextLength = mScaleTextPaint.measureText(mScaleText, 0, mScaleText.length());
                    canvas.drawText(mScaleText, pos[0] - scaleTextLength / 2, pos[1] - 130, mScaleTextPaint);
                    canvas.restore();
                }
            } else if (scale % mDrawLineSpace == 0) {
                mScaleLinePaint.setColor(mScaleTextColor);
                mScaleLinePaint.setStrokeWidth(10);
                endX = pos[0] + 50;
            }

            canvas.save();
            canvas.rotate((float) angle, pos[0], pos[1]);
            canvas.drawLine(startX, startY, endX, endY, mScaleLinePaint);
            canvas.restore();

        }
    }
}
 
Example 6
Source File: CheckView.java    From CheckView with Apache License 2.0 4 votes vote down vote up
/**
 * Perform measurements and pre-calculations. This should be called any time
 * the view measurements or visuals are changed, such as with a call to {@link #setPadding(int, int, int, int)}
 * or an operating system callback like {@link #onLayout(boolean, int, int, int, int)}.
 */
private void measurePaths() {
    int maxSize;
    float middle;

    maxSize = Math.min(getWidth(), getHeight());
    padding = Math.max(
          Math.max(getPaddingBottom(), getPaddingTop()),
          Math.max(getPaddingRight(), getPaddingLeft()));
    maxSize -= padding * 2;
    middle = maxSize / 2f;

    pathMeasure = new PathMeasure();

    PointF p1a = new PointF(middle, 0);
    PointF p1b = getCheckRightPoint(maxSize);

    firstPath = new Path();
    firstPath.moveTo(p1a.x, p1a.y);
    firstPath.lineTo(p1b.x, p1b.y);
    pathMeasure.setPath(firstPath, false);
    firstPathLength = pathMeasure.getLength();

    PointF p2a = new PointF(middle, maxSize);
    PointF p2b = getCheckMiddlePoint(maxSize);

    secondPath = new Path();
    secondPath.moveTo(p2a.x, p2a.y);
    secondPath.lineTo(p2b.x, p2b.y);
    pathMeasure.setPath(secondPath, false);
    secondPathLength = pathMeasure.getLength();

    PointF p3a = new PointF(0, middle);
    PointF p3b = getCheckLeftPoint(maxSize);

    thirdPath = new Path();
    thirdPath.moveTo(p3a.x, p3a.y);
    thirdPath.lineTo(p3b.x, p3b.y);
    pathMeasure.setPath(thirdPath, false);
    thirdPathLength = pathMeasure.getLength();

    PointF p4a = new PointF(maxSize, middle);

    fourPath = new Path();
    fourPath.moveTo(p4a.x, p4a.y);
    fourPath.lineTo(p2b.x, p2b.y);
    pathMeasure.setPath(fourPath, false);
    fourPathLength = pathMeasure.getLength();

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.SQUARE);
    paint.setStrokeWidth(strokeWidth);

    fromXY = new float[]{0f, 0f};
    toXY = new float[]{0f, 0f};
}
 
Example 7
Source File: BrokenAnimator.java    From BrokenView with MIT License 4 votes vote down vote up
/**
 * Build warped-lines according to the baselines, like the DiscretePathEffect.
 */
private void buildBrokenLines(Rect r) {
    LinePath[] baseLines = new LinePath[mConfig.complexity];
    buildBaselines(baseLines, r);
    PathMeasure pmTemp = new PathMeasure();
    for (int i = 0; i < mConfig.complexity; i++) {
        lineRifts[i] = new LinePath();
        lineRifts[i].moveTo(0, 0);
        lineRifts[i].setEndPoint(baseLines[i].getEndPoint());

        pmTemp.setPath(baseLines[i], false);
        float length = pmTemp.getLength();
        final int THRESHOLD = SEGMENT + SEGMENT / 2;

        if (length > Utils.dp2px(THRESHOLD)) {
            lineRifts[i].setStraight(false);
            // First, line to the point at SEGMENT of baseline;
            // Second, line to the random-point at (SEGMENT+SEGMENT/2) of baseline;
            // So when we set the start-draw-length to SEGMENT and the paint style is "FILL",
            // we can make the line become visible faster(exactly, the triangle)
            float[] pos = new float[2];
            pmTemp.getPosTan(Utils.dp2px(SEGMENT), pos, null);
            lineRifts[i].lineTo(pos[0], pos[1]);

            lineRifts[i].points.add(new Point((int)pos[0], (int)pos[1]));

            int xRandom, yRandom;
            int step = Utils.dp2px(THRESHOLD);
            do{
                pmTemp.getPosTan(step, pos, null);
                // !!!
                // Here determine the stroke width of lineRifts
                xRandom = (int) (pos[0] + Utils.nextInt(-Utils.dp2px(3),Utils.dp2px(2)));
                yRandom = (int) (pos[1] + Utils.nextInt(-Utils.dp2px(2),Utils.dp2px(3)));
                lineRifts[i].lineTo(xRandom, yRandom);
                lineRifts[i].points.add(new Point(xRandom, yRandom));
                step += Utils.dp2px(SEGMENT);
            } while (step < length);
            lineRifts[i].lineToEnd();
        } else {
            // Too short, it's still a beeline, so we must warp it later {@warpStraightLines()},
            // to make sure it is visible in "FILL" mode.
            lineRifts[i] = baseLines[i];
            lineRifts[i].setStraight(true);
        }
        lineRifts[i].points.add(lineRifts[i].getEndPoint());
    }
}
 
Example 8
Source File: CircleBroodLoadingRenderer.java    From DevUtils with Apache License 2.0 3 votes vote down vote up
private float getRestLength(Path path, float startD) {
    Path tempPath = new Path();
    PathMeasure pathMeasure = new PathMeasure(path, false);

    pathMeasure.getSegment(startD, pathMeasure.getLength(), tempPath, true);

    pathMeasure.setPath(tempPath, false);

    return pathMeasure.getLength();
}