在Android中设置动画路径?
问题描述:
我正在尝试使用Android的Canvas和PathMeasure设置动画路径。在每一帧上,路径应从源路径中绘制路径段,直到整个段完成。由此产生的效果应该类似于用钢笔/铅笔等书写。但是,当我使用PathMeasure getSegment时,目标路径似乎没有绘制任何东西。在Android中设置动画路径?
以下代码应以灰色绘制源路径,以红色绘制当前分段的终点,最后以黑色绘制路径子分段,但仅绘制源路径和终点(不是段) 。
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);
}
答
尝试增加rlineto(0,0)的分割路径,然后使用paint.Documentation建议绘制的路径这与Android的版本相比较小,如果你的目标SDK是4.3.This解决方案将只适用于使用小于或等于4.3。
只是为了好玩,我们可以看到logcat输出吗? – Dave
我之前没有使用'Path'或'PathMeasure',但我的直觉是整个二次曲线被认为是一个“段”,所以如果你的距离没有封装整个事物,它不会被返回。 getSegment调用的返回值是什么? – Dave
logcat输出如您所料。 'd'根据段的长度递增,如果取消注释边界代码,则会得到正确路径的边界。 getSegment的返回值为true。事实上,只要起始和结束距离参数不反转(0 Rhomel