我正在尝试使用Android的画布和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);
}
发布于 2015-04-27 07:25:08
尝试将rlineto(0,0)添加到分段路径,然后使用paint.Documentation绘制路径建议使用4.3以下的安卓版本。此解决方案仅适用于目标sdk小于或等于4.3的情况。
https://stackoverflow.com/questions/19164135
复制