以下作品:
canvas.save();
canvas.translate(s.startLine[2], s.startLine[3]);
canvas.rotate(-90);
canvas.drawText(s.startTimeString, 0, 0, darkPaint);
canvas.restore();
canvas.save(); \\ redundant?
canvas.rotate(-90);
canvas.translate(s.endLine[2],s.endLine[3]);
canvas.drawText(s.endTimeString,0,0,darkPaint);
canvas.restore();
这是做我想做的最有效的方法吗?我的意思是这第二个 save() 对我来说似乎是多余的......我认为删除它会使 restore 将矩阵状态恢复到最新保存的状态但不幸的是我遇到了这个异常:
java.lang.IllegalStateException: Underflow in restore - more restores than saves
有更好的方法吗?请注意,除了这部分之外, Canvas 矩阵在我的绘图中永远不会改变,所以也许加载单位矩阵也是一个选项,但我不确定它是否更好......
最佳答案
根据 the documentation :
void restore ()
This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call. It is an error to call restore() more times than save() was called.
所以您得到的错误与文档一致。
您可能想看一下 restoreToCount
方法 (documentation):
void restoreToCount (int saveCount)
Efficient way to pop any calls to save() that happened after the save count reached saveCount.
...
您建议加载单位矩阵作为撤消的一种方式。我不会这样做,不是因为性能,而是因为您正在尝试撤消(恢复)一些更改,向堆栈添加更多更改。
关于android - Canvas 加载标识或保存以进行多次还原,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877482/