canvas save() restoreToCount() Test

134 篇文章 0 订阅
25 篇文章 0 订阅
这篇博客介绍了在Android中使用Canvas的save()和restoreToCount()方法进行状态保存和恢复的实验过程。作者通过代码示例展示了如何使用这两个方法,并指出在使用restoreToCount()时需要注意保存状态的顺序,因为较早保存的状态在较晚恢复时会被清除。实验结果显示,canvas在onDraw开始前已自动进行了一次save操作。
摘要由CSDN通过智能技术生成
   /**
     * 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了一次.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值