动画Android中的路径?

时间:2013-10-03 16:16:36

标签: java android android-canvas

我正在尝试使用Android的Canvas和PathMeasure为路径设置动画。在每个帧上,路径应从源路径绘制路径段,直到整个段完成。产生的效果应类似于用钢笔/铅笔等书写。但是,当我使用PathMeasure getSegment时,目标路径似乎没有绘制任何内容。

以下代码应绘制灰色的源路径,当前段的终止点为红色,最后路径子段为黑色,但仅绘制源路径和终止点(段不是)。

public void initialize() {

    // Path to animate
    source = new Path();
    source.moveTo(0f, 10f);
    source.quadTo(100, 10, 100, 100);

    // temp path to store drawing segments
    segment = new Path();

    pm = new PathMeasure(source, false);

    frames = 10;
    increment = pm.getLength() / (float)frames;
    Log.d(TAG, "increment " + increment);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(5f);

    black = new Paint(paint);
    black.setColor(Color.BLACK);

    gray = new Paint(paint);
    gray.setColor(Color.GRAY);

    red = new Paint(paint);
    red.setColor(Color.RED);
}

@Override
public void onDraw(Canvas c) {
    super.onDraw(c);

    // draw the source path
    c.drawPath(source, gray);

    // draw the segment
    segment.reset();
    pm.getSegment(0, d, segment, true);
    c.drawPath(segment, black);

    //RectF bounds = new RectF();
    //segment.computeBounds(bounds, true);
    //Log.d(TAG, "bounds: " + bounds.toString());

    // draw the termination point on the segment
    float[] pos = new float[2];
    float[] tan = new float[2];
    pm.getPosTan(d, pos, tan);
    c.drawPoints(pos, red);

    // update the frame index
    frameIndex = (frameIndex + 1) % frames;
    d = (float)frameIndex * increment;
    Log.d(TAG, "d = " + d);

}

1 个答案:

答案 0 :(得分:0)

尝试将rlineto(0,0)添加到分段路径,然后使用paint.Documentation绘制路径,建议将其用于小于4.3的android版本。此解决方案仅在目标sdk小于或等于时才适用到4.3。