Canvas与图层(三)恢复画布

  • 恢复画布有两个函数:restore()与rest无论哪种* 恢复画布有两个函数:restore()与restongoCount()
  • restore()就是把回退栈中的最上层画布状态出栈,恢复画布状态,具体使用
  • restoreToCount(int count)
public int save();
public int save(int saveFlags)
public int saveLayer(RectF bounds, Paint paint, int saveFlags)
public int saveLayer(float left, float top, float right, float bottom ,Paint paint, int saveFlags)
public int save:ayerAlpha(REctF bounds, int alpha, int saveFlags)
public int saveLayerAlpha(float left, float top ,float right, float bottom,  int alpha, int saveFlags);
  • 在 save()、saveLayer()、saveLayerAlpha()函数保存画布后,都会返回一个ID值,这个ID值表示当前的画布 的栈层索引(从0开始)。比如,保存在第三层,则返回2
//表示一直退栈,直到将指定索引的画布信息退出来,之后的栈的最上层的画布信息将作为最新的画布
public void restoreToCount(int saveCount);

eg:

public class RestoreToCountView extends View {
    private Paint paint;
    public RestoreToCountView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        paint =  new Paint();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int id1 = canvas.save();
        canvas.clipRect(0, 0, 800, 800);
        canvas.drawColor(Color.BLACK);
        System.out.println(id1);
        int id2 = canvas.saveLayer(0, 0, getWidth(), getHeight(), paint, Canvas.ALL_SAVE_FLAG);
        canvas.clipRect(100, 100, 700, 700);
        canvas.drawColor(Color.RED);
        System.out.println(id2);
        int id3 = canvas.saveLayerAlpha(0, 0, getWidth(),getHeight(), 0xf0, Canvas.ALL_SAVE_FLAG);
        canvas.clipRect(200, 200, 600, 600);
        canvas.drawColor(Color.YELLOW);
        System.out.println(id3);
        int id4 = canvas.save();
        canvas.clipRect(300, 300, 500, 500);
        canvas.drawColor(Color.BLUE);
        System.out.println(id4);
        //恢复到生成id3之前的画布状态
        canvas.restoreToCount(id3);
        canvas.drawColor(Color.GREEN);
    }
}
restore()和restoreToCount(int count)的关系
  • 两者针对的是同一个栈,可以完全通用
  • restore()默认将栈顶的内容退出还原画布
  • restoreToCount(int count)函数则一直退栈,直到把指定的索引的画布信息退出来,之后栈顶的画布信息作为最新的画布
  • 无论哪种save()无论哪种flag,都是放到一个栈中
结论:
  • restore()的含义是把回退栈中的最上层画布状态出栈,恢复画布状态
  • restoreToCount(int count)是一直出栈,知道哦把指定画布的信息退出来,讲次至此之前的所有动作都恢复
  • 无论哪种save()函数,哪个flag,保存画布时都是使用一个栈
  • restore()与restoreToCount(int count)针对的都是同一个栈,所以完全可以通用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值