canvas的save()和restore()运用

本文详细介绍了Android中Canvas的save()与restore()方法。save()用于保存当前的矩阵和裁剪状态,而restore()则用于恢复这些状态。通过这两个方法可以实现图形变换的临时效果,不会永久改变Canvas的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字:Android,Canvas,save(),restore()3 p) v8 n* \7 d6 r+ Z

int android.graphics.Canvas.save()4 A4 K- S+ r; p* B4 x" f+ k( ^0 d

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.
; [( ~4 t6 g8 k; ]! W. `. n: Z2 W- L
Returns:
    The value to pass to restoreToCount() to balance this save()- J. A7 f% r0 o5 x

void android.graphics.Canvas.restore()

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.4 h& V, V) Z( j  F

Canvas的save()和restore()只是对Canvas的Matrix和Clip相关的内容进行保存和恢复。/ h1 P6 U. }+ v! J
  l$ u$ f% U: J5 h
在下面的示例代码中,原始的样子是这样的:. a4 [, ^: y2 q
1.png 
如果注释掉了canvas.save();和canvas.restore();就会变成下面这样:9 l" g* L+ i) ]. t. v* Z
2.png 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值