多亏了@VitorHugoSchwaab的评论,我终于找到了解决方案。小提醒:我在这个问题中发布的所有代码都应该在
public class DrawCanvas extends View
班话虽如此:
所以,我把“触发器方法”留作如下:
private int s, p;
private boolean replay_mode;
public void replayStrokes() {
s = 0;
p = 0;
replay_mode = true;
invalidate();
}
我修改了
onDraw
这样的方法(我将其转换为
Megamoth
,但我稍后可以修复:P):
@Override
protected void onDraw(Canvas canvas) {
if (replay_mode) {
if (s == 0 && p == 0) {
Log.i("SANDBOX_INFO", "Replay Mode START");
canvas.drawRGB(255, 255, 255);
p++;
replayPath.reset();
replayPath.moveTo(strokes.get(0).points.get(0).x,
strokes.get(0).points.get(0).y);
invalidate();
} else {
Log.i("SANDBOX_INFO", "Replaying --> S:" + s + " P:" + p);
if (p == 0) {
replayPath.moveTo(strokes.get(s).points.get(p).x,
strokes.get(s).points.get(p).y);
} else {
replayPath.lineTo(strokes.get(s).points.get(p).x,
strokes.get(s).points.get(p).y);
canvas.drawPath(replayPath, mPaint);
}
int points_size = strokes.get(s).points.size();
if (s == strokes.size() - 1 && p == points_size - 1) {
Log.i("SANDBOX_INFO", "Replay Mode END");
replay_mode = false;
}
else if (p == points_size - 1) {
s++;
p = 0;
} else {
p++;
}
}
invalidate();
} else {
canvas.drawPath(mPath, mPaint);
}
super.onDraw(canvas);
}
这是有效的。然而,在回放笔划时会出现“闪烁”的小故障效果。我认为这可能是因为它实际上是在“重新绘制”每一帧的整个“回放路径”。基于这个猜测,当我有更多的时间进行编码时,我会尝试解决这个问题。