平移、缩放、旋转等操作等于是,我在一个正的画布绘制好图,然后再把画布做旋转、平移、缩放等等的效果。

也就是说,我使用的X、Y坐标还是正常的坐标(没旋转、平移、缩放等之前的坐标)。

save()和restore()是用来规定操作的范围的。

如果有save()和restore(),那么平移、缩放、旋转等操作只对save()和restore()作用域之间的代码有效。

在此我参考了http://www.javaeye.com/topic/440623

然后我做了代码测试(在onDraw()中画图),如下:

protected void onDraw(Canvas canvas){
  //首先定义中心点和半径
  int px=getMeasuredWidth()/2;
  int py=getMeasuredHeight()/2;
  
  int radius=Math.min(px, py);
  
  canvas.drawCircle(px, py, radius, circlePaint);
  canvas.save();//注释save①
  canvas.rotate(-bearing, px, py);
  //canvas.save();
  
  int textWidth=(int)textPaint.measureText("W");
  int cardinalX=px-textWidth/2;
  int cardinalY=py-radius+textHeight;
  
  //开始绘制刻度和文字
  //每15度一个刻度,每45度一个数字,每90度一个方向
  for(int i=0; i<24; i++){
   canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
   
   canvas.save();//注释save②
   canvas.translate(0, textHeight);
   
   
   if(i%6==0){
    String dirString="";
    switch(i){
    case(0):{
     dirString=northString;
     int arrowY=2*textHeight;
     canvas.drawLine(px, arrowY, px-5, 3*textHeight, markerPaint);
     canvas.drawLine(px, arrowY, px+5, 3*textHeight, markerPaint);
     break;
    }
    case(6):dirString=eastString;break;
    case(12):dirString=westString;break;
    case(18):dirString=southString;break;
    }
    canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
   }
   else if(i%3==0){
    String angle=String.valueOf(i*15);
    float angleTextWidth=textPaint.measureText(angle);
    
    int angleX=(int)(px-angleTextWidth/2);
    int angleY=py-radius+textHeight;
    canvas.drawText(angle, angleX, angleY, textPaint);
   }
   canvas.restore();//注释restore②   
   canvas.rotate(15, px, py);
   
  }
  //测试save()和restore()的作用域
  canvas.drawText("Hello world 在restore之前!", 100, 100, textPaint);
  canvas.restore();//注释restore①
  canvas.drawText("Hello world 在restore之后!", 100, 100, textPaint);
 }

//结论:在save②到restore②之间所画的图顶点下移textHeight个像素,restore②之后的代码不受影响

//在save①到restore①之间所画的内容都选择45°,restore①之后的代码不会旋转

//注意save②到restore②也是在save①到restore①作用之内的,所以save②到restore②之间的内容不但顶点下移textHeight个像素,并且旋转

//45度。

 

最新文章

  1. Sublime Text 3插件安装
  2. Oracle数据库之三
  3. [转载]关于AutoCAD.NET的辅助方法
  4. Asp.Net Unix时间戳和DateTime类型转换
  5. delphi 数据导出到word
  6. BTrace: DTrace for Java2
  7. SQL中 WHERE与HAVING的区别
  8. C语言复杂的函数指针声明
  9. adnroid仿miui的dialog
  10. CentOS 7 systemd service开机启动设定
  11. Node.js:模块
  12. UML系列图
  13. knowledge, Experience &amp; Creativity
  14. vue使用过滤器利用moment实现日期的格式化
  15. java新知识系列 五
  16. Cordova开箱注意事项
  17. Spring整合MyBatis(简单登录Demo)
  18. 在Ubuntu中搭建KMS服务器
  19. Fortran编译器之一GUN Fortran安装(Windows XP)
  20. php--------ThinkPHP3.2验证码使用

热门文章

  1. 基于百度OCR的图片文字识别
  2. python补漏----isinstance 和 issubclass
  3. 添加字段的SQL语句的写法:
  4. fedora安装rails缺少js runtime和cannot load such file -- sqlite3/sqlite3_native解决办法
  5. Model View Controller(MVC) in PHP
  6. 九度oj 题目1452:搬寝室
  7. math &amp; 三元一次方程组的解法
  8. Mysql 数值类型
  9. HDU——1058Humble Numbers(找规律)
  10. ——CentOS 7 安装SQL Server2019