Android高级UI开发(四十三)Canvas基本知识(1)

举报
yd_57386892 发表于 2020/12/29 01:11:03 2020/12/29
【摘要】  直接贴代码,注释很详细。源码下载:https://download.csdn.net/download/gaoxiaoweiandy/12371948 package com.xiaowei.lsn7_canvas; import android.content.Context;import android.graphics.Bitmap;import android.g...

 直接贴代码,注释很详细。源码下载:https://download.csdn.net/download/gaoxiaoweiandy/12371948


  
  1. package com.xiaowei.lsn7_canvas;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.RectF;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11. import com.xiaowei.lsn7_canvas.R;
  12. /**
  13. * Created by John on 2017/5/14.
  14. */
  15. public class MyView2 extends View {
  16. private Bitmap mBitmap;
  17. public MyView2(Context context) {
  18. this(context,null);
  19. }
  20. public MyView2(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.xyjy2)).getBitmap();
  23. }
  24. @Override
  25. protected void onDraw(Canvas canvas) {
  26. super.onDraw(canvas);
  27. //绘制直线
  28. Paint paint = new Paint();
  29. paint.setColor(Color.RED);
  30. //paint.setStyle(Paint.Style.FILL);//
  31. paint.setAntiAlias(true);
  32. paint.setDither(true);
  33. paint.setStyle(Paint.Style.STROKE);
  34. paint.setStrokeWidth(20);
  35. /*gxw-s for 画线段2点之间
  36. canvas.drawLine(0, 0, 100, 100, paint);
  37. gxw-e*/
  38. /*gxw-s for 画多段线段,4个值为1条线段,4个值分别是起点的x,y与终点的x',y'。
  39. float []pts = {0,0,100,100,200,200,300,300,0,0};
  40. canvas.drawLines(pts, paint);
  41. gxw-e*/
  42. /*gxw-s for 画多段线段,可以跳过几个值(这里为6),跳过之后再去绘制几个值(这里为4,即(300,300,0,0)),最终就只画了这条起点为300,300终点为0,0的线段
  43. float []pts = {0,0,100,100,200,200,300,300,0,0,100,100};
  44. canvas.drawLines(pts, 6, 4, paint);
  45. gxw-e*/
  46. /*gxw-s for 画1个点与画多个点
  47. canvas.drawPoint(500, 500, paint);
  48. float []pts = {0,0,100,100,200,200,300,300,0,0};
  49. canvas.drawPoints(pts, paint);
  50. gxw-e */
  51. /*gxw-s for 画矩形
  52. RectF r = new RectF(100, 100, 500, 500);
  53. canvas.drawRect(r, paint);
  54. canvas.drawRect(100, 100, 400, 500, paint);
  55. gxw-e*/
  56. /*gxw-s for 绘制圆角矩形,30,30为圆角半径,x,y
  57. RectF r = new RectF(100, 100, 400, 500);
  58. canvas.drawRoundRect(r, 30, 30, paint);
  59. gxw-e*/
  60. //画圆canvas.drawCircle(300, 300, 200, paint);
  61. /*gxw-s for 绘制椭圆的2种方法
  62. // canvas.drawOval(100,100,400,300, paint);
  63. RectF r = new RectF(100, 100, 400, 300);
  64. canvas.drawOval(r, paint);
  65. gxw-e*/
  66. /*gxw-s for 画弧形
  67. // paint.setStyle(Paint.Style.FILL);
  68. RectF r = new RectF(100, 100, 400, 500);
  69. canvas.drawArc(r, 0, 90, false, paint);//顺时针旋转90度
  70. gxw-e */
  71. /*gxw-s for 绘制路径,这里为直线路径与贝塞尔曲线*/
  72. /* 绘制直线路径
  73. Path path = new Path();
  74. path.moveTo(100, 100);//画笔落笔的位置
  75. path.lineTo(200, 100);
  76. path.lineTo(200, 200);
  77. path.close();//封闭路径,在这里如果不封闭,绘制的将是一个直角,不能成为一个三角形。
  78. */
  79. /* gxw-s for 绘制贝塞尔曲线路径
  80. Path path = new Path();
  81. path.cubicTo(250, 200, 350, 300, 450, 400);//绘制贝塞尔曲线
  82. canvas.drawPath(path, paint);gxw-e for 绘制贝塞尔曲线路径*/
  83. /*gxw-e */
  84. /*gxw-s for 圆角矩形路径
  85. RectF r = new RectF(100, 100, 600, 800);
  86. Path path = new Path();
  87. float radii[] = {10,10,10,10,10,10,50,60}; //圆角半径,分别为左上,右上,右下,左下。
  88. path.addRoundRect(r, radii, Path.Direction.CCW);
  89. canvas.drawPath(path, paint);
  90. gxw-e */
  91. /*gxw-s for 区域:将椭圆绘制成多个小矩形组成,高等数学中的微积分
  92. RectF r = new RectF(100, 100, 600, 800);
  93. Path path = new Path();
  94. path.addOval(r, Path.Direction.CCW);
  95. //创建一块矩形的区域
  96. Region region = new Region(100, 100, 600, 800);
  97. Region region1 = new Region();
  98. region1.setPath(path, region);//path的椭圆区域和矩形区域进行交集
  99. //结合区域迭代器使用(得到图形里面的所有的矩形区域)
  100. RegionIterator iterator = new RegionIterator(region1);
  101. Rect rect = new Rect();
  102. while (iterator.next(rect)) {
  103. canvas.drawRect(rect, paint);
  104. }
  105. 还有区域的合并,交集等操作。
  106. gxw-e */
  107. /*
  108. 平移START
  109. Canvas绘制坐标轴,canvas的坐标系原点永远是View的左上角
  110. canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
  111. paint.setColor(Color.BLUE);
  112. canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴
  113. //这里实质平移的是Canvas里面的图形(绘图坐标系),然后Canvas并没有平移,它的坐标系原点还是View的左上角。
  114. canvas.translate(100, 50);
  115. canvas.save();
  116. //Canvas平移后再绘制坐标轴,发现“坐标轴图形”确实平移了
  117. paint.setColor(Color.GREEN);
  118. //canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
  119. paint.setColor(Color.BLACK);
  120. canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴
  121. ===================================================================
  122. //旋转
  123. canvas.rotate(45);
  124. canvas.save();
  125. //旋转后再绘制坐标轴,发现“坐标轴图形”确实旋转了
  126. paint.setColor(Color.GREEN);
  127. canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
  128. paint.setColor(Color.BLACK);
  129. canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴
  130. ==================================================================
  131. //缩放Scale
  132. canvas.restoreToCount(1);//还原到第一次save的 状态
  133. canvas.drawColor(Color.BLUE);
  134. RectF r = new RectF(0, 0, 400, 400);
  135. paint.setColor(Color.RED);
  136. //sx,sy:分别对x/y方向的一个缩放系数,画布的缩放会导致里面所有的绘制的东西都会有一个缩放效果
  137. canvas.scale(1.5f, 1.5f);
  138. canvas.drawRect(r, paint);
  139. =====================================================================
  140. //旋转角度
  141. canvas.restoreToCount(1);//还原到第一次save的 状态
  142. canvas.drawColor(Color.BLUE);
  143. paint.setColor(Color.WHITE);
  144. RectF rect = new RectF(300, 600, 400, 800);
  145. canvas.drawRect(rect, paint);
  146. canvas.rotate(40, 300, 600);
  147. paint.setColor(Color.YELLOW);
  148. canvas.drawRect(rect, paint);
  149. =============================================================================
  150. */
  151. //4. 斜拉画布
  152. /* canvas.drawColor(Color.BLUE);
  153. paint.setColor(Color.RED);
  154. RectF rec2t = new RectF(100, 100, 300, 300);
  155. canvas.drawRect(rec2t, paint);
  156. paint.setColor(Color.YELLOW);
  157. //sx,sy倾斜度:X轴方向上倾斜60度,tan60=根号3
  158. canvas.skew(1.73f, 0);
  159. canvas.drawRect(rec2t, paint);
  160. =========================================================================
  161. */
  162. /*gxw-s for 裁剪
  163. RectF r = new RectF(200, 200, 400, 500);
  164. canvas.drawRect(r, paint);
  165. paint.setColor(Color.BLUE);
  166. canvas.clipRect(new Rect(250, 250, 300, 400));
  167. canvas.drawColor(Color.YELLOW);
  168. =====================================================
  169. gxw-e for 裁剪*/
  170. canvas.drawColor(Color.WHITE);
  171. paint.setColor(Color.GREEN);
  172. RectF r = new RectF(200, 200, 400, 500);
  173. canvas.drawRect(r, paint);
  174. canvas.translate(100,100);
  175. paint.setColor(Color.RED);
  176. canvas.drawRect(r, paint);
  177. int layer = canvas.saveLayer(0,0,canvas.getWidth(),canvas.getHeight(),null,Canvas.ALL_SAVE_FLAG);
  178. paint.setColor(Color.YELLOW);
  179. canvas.drawRect(r, paint);
  180. /*gxw -s for 笔记
  181. Canvas的状态保存---状态栈、Layer栈
  182. 状态栈--save、 restore方法来保存和还原变换操作Matrix以及Clip剪裁,也可以通过restoretoCount直接还原到对应栈的保存状态
  183. Layer栈--- saveLayer的时候都会新建一个透明的图层(离屏Bitmap-离屏缓冲),并且会将saveLayer之前的一些Canvas操作延续过来
  184. 后续的绘图操作都在新建的layer上面进行,当我们调用restore 或者 restoreToCount 时 更新到对应的图层和画布上
  185. */
  186. }
  187. }

 

文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/105832787

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。