/**
* Saves the current matrix and clip onto a private stack. Subsequent
* calls to translate,scale,rotate,skew,concat or clipRect,clipPath
* will all operate as usual, but when the balancing call to restore()
* is made, those calls will be forgotten, and the settings that existed
* before the save() will be reinstated.
*
* @return The value to pass to restoreToCount() to balance this save()
*/
public native int save();
/**
* 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.
*/
public native void restore();
/**
* Returns the number of matrix/clip states on the Canvas' private stack.
* This will equal # save() calls - # restore() calls.
*/
public native int getSaveCount();
/**
* Efficient way to pop any calls to save() that happened after the save
* count reached saveCount. It is an error for saveCount to be less than 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 native void restoreToCount(int saveCount);
做了一个简单实验:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Log.e("FYF", "begin onDraw canvas SaveCount " + canvas.getSaveCount());
super.onDraw(canvas);
canvas.save();
Drawable d = getResources().getDrawable(R.drawable.bender03pb);
int savecount1 = canvas.save();
canvas.translate(100, 100);
canvas.scale(1.5f, 1.5f);
int savecount2 = canvas.save();
canvas.rotate(90);
canvas.skew(0.5f, 0.5f);
Log.e("FYF", savecount1 + " " + savecount2
+ " canvas save count: " + canvas.getSaveCount());
d.setBounds(0,0,200,200);
d.draw(canvas);
canvas.restoreToCount(savecount2);
Log.e("FYF", "after restore savecount2, canvas save count: " + canvas.getSaveCount());
d.draw(canvas);
canvas.restoreToCount(savecount1);
d.draw(canvas);
Log.e("FYF", "after restore savecount1, canvas save count: " + canvas.getSaveCount());
canvas.restore();
Log.e("FYF", "end onDraw canvas SaveCount " + canvas.getSaveCount());
}
}
结果:
E/FYF (31669): begin onDraw canvas SaveCount 1
E/FYF (31669): 2 3 canvas save count: 4
E/FYF (31669): after restore savecount2, canvas save count: 3
E/FYF (31669): after restore savecount1, canvas save count: 2
E/FYF (31669): end onDraw canvas SaveCount 1
后来试着先canvas.restoreToCount(savecount1), 再 canvas.restoreToCount(savecount2), 没有出错,不过canvas.restoreToCount(savecount2)没有生效,
因为savecount2代表的状态在savecount1restore的时候就已经被清除了.
E/FYF (32303): HELLO WORLD!
E/FYF (32303): find tag B2 true
E/FYF (32303): begin onDraw canvas SaveCount 1
E/FYF (32303): 2 3 canvas save count: 4
E/FYF (32303): after restore savecount2, canvas save count: 2
E/FYF (32303): after restore savecount1, canvas save count: 2
E/FYF (32303): end onDraw canvas SaveCount 1
以前都没有注意过canvas 的save()还有返回值,偶然看到restoreToCount()才知道还可以这么用。方便使用。
看样子canvas 在进入onDraw前还被save了一次.