【问题标题】:Animating a Path in Android?在Android中动画路径?
【发布时间】:2015-04-27 15:25:08
【问题描述】:

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

}

【问题讨论】:

  • 只是为了好玩,我们可以看到logcat的输出吗?
  • 我之前没有使用过PathPathMeasure,但我的直觉是整个二次方被认为是一个“段”,所以如果你的距离没有包含整个事情它没有不会被退回。 getSegment 调用的返回值是多少?
  • logcat 输出如您所愿。 'd' 根据段的长度递增,如果您取消注释边界代码,您将获得路径的正确边界。 getSegment 的返回值为 true。事实上,只要开始和结束距离参数不颠倒 (0

标签: java android android-canvas


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2010-12-25
    • 2013-09-08
    • 2011-06-29
    相关资源
    最近更新 更多