我正在尝试使用 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);
}