android - Canvas 加载标识或保存以进行多次还原

标签 android canvas matrix

以下作品:

        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/

相关文章:

JavaScript Canvas 在错误的坐标处绘制

javascript - canvas.toDataURLWithMultiplier 不是一个函数

c - C 中矩阵的迭代器和指针

C:从文件中读取某种格式的二维复数数组

python - 如何制作 2D 和 3D 矩阵的点积(分别对每个维度进行计算)

android - 如何使用 Android Studio + Gradle + NDK 构建外部 C++ 库?

android - 使用 firebase bundle 通过 GTM v5 增强电子商务标签

java - 刷新 slider 中的 fragment

java - 如何用 <pre> 解析内容?

canvas - 有人知道 HTML5 Canvas 中游戏的 "good"框架吗? (见要求)