/** * 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);

做了一个简单实验:

@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubLog.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了一次.


更多相关文章

  1. android禁止状态栏下拉
  2. Android电源信息
  3. android api25 点击隐藏BottomSheet需求的Behavior设置技巧
  4. android 实时监听网络连接状态
  5. Android(安卓)网络状态操作
  6. ViewModel + SavedState
  7. Android--Notification
  8. 屏幕亮屏、熄屏监听代码
  9. Android开关控件ToggleButton

随机推荐

  1. Android(安卓)开发 短信app
  2. Android(安卓)Annotations+Retrofit+Rxja
  3. Android(安卓)Toast用法
  4. android 睡眠和唤醒过程
  5. android SQLite3常用命令&语法
  6. [置顶] Android实现RecyclerView的下拉刷
  7. android CheckBox控件的定义及事件监听
  8. android中回调函数
  9. android启动之SystemServer启动
  10. Android通知(Notification)使用详解