自定义View画布save()和restore()
来源:互联网 发布:淘宝儿童皮鞋 编辑:程序博客网 时间:2024/05/17 04:34
遇到这两个防法正好记录一下,有一个简单的例子,画一个钟表的刻度盘,首先画圆,然后通过旋转画布画刻度,下面主要代码:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 画圆 */ canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, mPaint); /** * 画12点位置 */ canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 50, mPaint); /** * 画中心点 */ canvas.drawPoint(mWidth / 2, mHeight / 2, mPaint); /** * 旋转画布画画其他位置的刻度 */ for (int i = 0; i < 11; i++) { canvas.rotate(30, mWidth / 2, mHeight / 2); canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 50, mPaint); } }
效果图:
那么现在修改代码添加两个放法save,和restore如下代码所示:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 画圆 */ canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, mPaint); /** * 画12点位置 */ canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 50, mPaint); /** * 画中心点 */ canvas.drawPoint(mWidth / 2, mHeight / 2, mPaint); /** * 旋转画布画画其他位置的刻度 */ for (int i = 0; i < 11; i++) { canvas.save(); canvas.rotate(30, mWidth / 2, mHeight / 2); canvas.restore(); canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 50, mPaint); } }效果图如下:
总结:好了我们看到了所有的刻度都画在12点了,为什么,在一般情况下save放法和restore一般是成对调用的,save放法用于保存当前画布状态,而restore放法则是回复之前保存的画布的状态,在本例中,我再旋转画布之前进行保存画布状态,(此时保存的画布状态是没有经过旋转的),我在旋转之后调用restore恢复之前保存的状态,所以目前的画布状态仍然是没有旋转过的,所以所有的刻度都画在12点位置,不知道明白了没有,在save和restore一般进行画布的旋转缩放平移等操作,最后再用箭头表示一下画布的状态。
阅读全文
0 0
- 自定义View画布save()和restore()
- 画布的save 和restore
- android画布中canvas.save()和canvas.restore() 作用
- kvm save和restore
- Canvas:save()和restore()
- 自定义View之路——Canvas(save,restore,translate,rotate)
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- Canvas的save和restore
- 理解linux time命令的输出
- Caused by: java.lang.NoClassDefFoundError: HttpSessionBindingEvent
- windows 下nodejs 升级无法通过n模块升级
- 人生不要设限
- 字符串编程题题目解析(From leetcode)——1
- 自定义View画布save()和restore()
- 诊断篇(5.4) 01. 日志和监控 ❀ 飞塔 (Fortinet) 防火墙
- 第7周项目6
- 值类型和引用类型的区别
- recycleView布局切换
- 01html学习笔记
- windows 下解决端口占用
- 解决outlook创建规则中运行脚本选项丢失的问题
- Deep Learning:正则化(十一)