Canvas 状态保存机制 中 , 存在两个栈结构 , 分别是 状态栈 和 图层栈 ;
其中 图层栈 又称为 Layer 栈 ;
调用 Canvas#restoreToCount(int saveCount) 方法 , 可以 指定出栈到某层 ;
如下图所示 , 已经调用了 5 次 Canvas#save() 方法 ,
Canvas#restoreToCount(int saveCount) 方法函数原型如下 :
/**
* 在save count达到saveCount后,弹出对save()的任何调用的有效方法。
* saveCount小于1是一个错误。
*
* Example:
* int count = canvas.save();
* ... // more calls potentially to save()
* canvas.restoreToCount(count);
* // now the canvas is back in the same state it was before the initial
* // call to save().
*
* @param saveCount The save level to restore to.
*/
public void restoreToCount(int saveCount) {
if (saveCount < 1) {
if (!sCompatibilityRestore || !isHardwareAccelerated()) {
// do nothing and throw without restoring
throw new IllegalArgumentException(
"Underflow in restoreToCount - more restores than saves");
}
// compat behavior - restore as far as possible
saveCount = 1;
}
nRestoreToCount(mNativeCanvasWrapper, saveCount);
}