当前位置:   article > 正文

Android onDraw画图记录_android draw 画图

android draw 画图

记录1:

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. Paint paint = new Paint();
  5. //去锯齿
  6. paint.setAntiAlias(true);
  7. //设置眼色
  8. paint.setColor(Color.BLUE);
  9. //设置空心
  10. paint.setStyle(Paint.Style.STROKE);
  11. //设置边宽
  12. paint.setStrokeWidth(3);
  13. RectF rect = new RectF(0,0, 200, 200);
  14. canvas.drawRoundRect(rect, 10, 10, paint);
  15. paint.setColor(Color.RED);
  16. Path path = new Path();
  17. // path.arcTo(rect,0, 180,true);
  18. path.quadTo(200, 200, 200, 200);
  19. // path.close();
  20. // path.quadTo(99, 99, 99, 2);
  21. // path.quadTo(99, 99, 2, 99);
  22. //图案1:
  23. // path.moveTo(rect.right/2, rect.top);
  24. // path.quadTo((rect.right-rect.left)/2, (rect.bottom-rect.top)/2, rect.right, (rect.bottom-rect.top)/2);
  25. // path.quadTo((rect.right-rect.left)/2, (rect.bottom-rect.top)/2, (rect.right-rect.left)/2, rect.bottom);
  26. // path.quadTo((rect.right-rect.left)/2, (rect.bottom-rect.top)/2, rect.left, (rect.bottom-rect.top)/2);
  27. // path.quadTo((rect.right-rect.left)/2, (rect.bottom-rect.top)/2, rect.right/2, rect.top);
  28. // path.lineTo(rect.right, (rect.bottom-rect.top)/2);
  29. // path.lineTo((rect.right-rect.left)/2, rect.bottom);
  30. // path.lineTo(rect.left, (rect.bottom-rect.top)/2);
  31. // path.lineTo(rect.right/2, rect.top);
  32. //----------------------------------------------------------
  33. //图案2:波浪线
  34. // path.moveTo(0, rect.bottom/2);
  35. // path.quadTo(rect.right/4, rect.bottom, rect.right/2, rect.bottom/2);
  36. // path.quadTo(rect.right/4*3, rect.top, rect.right, rect.bottom/2);
  37. //图案3: 梯形
  38. // path.moveTo(rect.right/2,rect.top);
  39. // path.lineTo(rect.right/2, rect.bottom);
  40. // path.lineTo(rect.left,rect.bottom/6*4);
  41. // path.lineTo(rect.left,rect.bottom/6*2);
  42. // path.lineTo(rect.right/2,rect.top);
  43. canvas.drawPath(path, paint);
  44. }

记录2:

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. Paint paint = new Paint();
  5. //去锯齿
  6. paint.setAntiAlias(true);
  7. //设置眼色
  8. paint.setColor(Color.BLUE);
  9. //设置空心
  10. paint.setStyle(Paint.Style.STROKE);
  11. //设置边宽
  12. paint.setStrokeWidth(3);
  13. RectF rect = new RectF(0,0, 200, 200);
  14. canvas.drawRoundRect(rect, 10, 10, paint);
  15. paint.setColor(Color.RED);
  16. Path path = new Path();
  17. path.moveTo(rect.width(), rect.height() / 2);
  18. paint.setColor(Color.YELLOW);
  19. canvas.drawPath(path, paint);
  20. path.lineTo((float) (rect.width() * 0.75), (float) (rect.width() / 2 - rect.width() * Math.sqrt(3) / 4));
  21. paint.setColor(Color.BLUE);
  22. canvas.drawPath(path, paint);
  23. path.lineTo((float) (rect.width() * 0.25), (float) (rect.width() / 2 - rect.width() * Math.sqrt(3) / 4));
  24. path.lineTo(0, rect.width() / 2);
  25. paint.setColor(Color.BLACK);
  26. canvas.drawPath(path, paint);
  27. path.lineTo((float) (rect.width() * 0.25), (float) (rect.width() / 2 + rect.width() * Math.sqrt(3) / 4));
  28. path.lineTo((float) (rect.width() * 0.75), (float) (rect.width() / 2 + rect.width() * Math.sqrt(3) / 4));
  29. path.lineTo(rect.width(), rect.height() / 2);
  30. canvas.drawPath(path, paint);
  31. canvas.drawPath(path, paint);
  32. }

记录3:

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. //把整张画布绘制成白色
  5. canvas.drawColor(Color.WHITE);
  6. Paint paint = new Paint();
  7. //去锯齿
  8. paint.setAntiAlias(true);
  9. //设置颜色
  10. paint.setColor(Color.BLUE);
  11. //---------------画空心图形--------------------
  12. //设置空心
  13. paint.setStyle(Paint.Style.STROKE);
  14. //设置边宽
  15. paint.setStrokeWidth(3);
  16. //绘制圆形
  17. canvas.drawCircle(40, 40, 30, paint);
  18. //绘制正方形
  19. canvas.drawRect(10, 80, 70, 140, paint);
  20. //绘制矩形
  21. canvas.drawRect(10, 150, 70, 190, paint);
  22. //绘制圆角矩形
  23. RectF re2 = new RectF(10,200,70,230);
  24. canvas.drawRoundRect(re2, 15, 15, paint);
  25. //绘制椭圆
  26. RectF rel = new RectF(10,240,70,270);
  27. canvas.drawOval(rel, paint);
  28. //定义一个Path对象,封闭一个三角形
  29. Path path1 = new Path();
  30. path1.moveTo(10, 340);
  31. path1.lineTo(70, 340);
  32. path1.lineTo(40, 290);
  33. path1.close();
  34. //根据Path进行绘制,绘制三角形
  35. canvas.drawPath(path1, paint);
  36. //定义一个Path对象,封闭一个五角星
  37. Path path2 = new Path();
  38. path2.moveTo(27, 360);
  39. path2.lineTo(54, 360);
  40. path2.lineTo(70, 392);
  41. path2.lineTo(40, 420);
  42. path2.lineTo(10, 392);
  43. path2.close();
  44. //根据Path进行绘制,绘制五角星
  45. canvas.drawPath(path2, paint);
  46. //--------------画实心图形---------------------------------
  47. //设置填丛风格后进行绘制
  48. //实心
  49. paint.setStyle(Paint.Style.FILL);
  50. paint.setColor(Color.RED);
  51. canvas.drawCircle(120, 40, 30, paint);
  52. //绘制正方形
  53. canvas.drawRect(90, 80, 150, 140, paint);
  54. //绘制矩形
  55. canvas.drawRect(90, 150, 150, 190, paint);
  56. //绘制圆角矩形
  57. RectF re22 = new RectF(90,200,150,230);
  58. canvas.drawRoundRect(re22, 15, 15, paint);
  59. //绘制椭圆
  60. RectF re21 = new RectF(90, 240, 150, 270);
  61. canvas.drawOval(re21, paint);
  62. Path path3 = new Path();
  63. path3.moveTo(90, 340);
  64. path3.lineTo(150, 340);
  65. path3.lineTo(120, 290);
  66. path3.close();
  67. //绘制三角形
  68. canvas.drawPath(path3,paint);
  69. //绘制五角形
  70. Path path4 = new Path();
  71. path4.moveTo(106, 360);
  72. path4.lineTo(134, 360);
  73. path4.lineTo(150, 392);
  74. path4.lineTo(120, 420);
  75. path4.lineTo(90, 392);
  76. path4.close();
  77. canvas.drawPath(path4, paint);
  78. //--------------渐变图形------------------------------
  79. //设置渐变器后绘制
  80. //为Paint设置渐变器
  81. Shader mShasder = new LinearGradient(0, 0, 40, 60, new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);
  82. paint.setShader(mShasder);
  83. //设置阴影
  84. paint.setShadowLayer(45, 10, 10, Color.GRAY);
  85. //绘制圆形
  86. canvas.drawCircle(200, 40, 30, paint);
  87. //绘制正方形
  88. canvas.drawRect(170, 80, 230, 140, paint);
  89. //绘制矩形
  90. canvas.drawRect(170, 150, 230, 190, paint);
  91. //绘制圆角的矩形
  92. RectF re31 = new RectF();
  93. canvas.drawRoundRect(re31, 15, 15, paint);
  94. //绘制椭圆
  95. RectF re32 =new RectF();
  96. canvas.drawOval(re32, paint);
  97. //根据Path,绘制三角形
  98. Path path5 = new Path();
  99. path5.moveTo(170, 340);
  100. path5.lineTo(230, 340);
  101. path5.lineTo(200, 290);
  102. path5.close();
  103. canvas.drawPath(path5, paint);
  104. //根据PAth,进行绘制五角形
  105. Path path6 = new Path();
  106. path6.moveTo(186, 360);
  107. path6.lineTo(214, 360);
  108. path6.lineTo(230, 392);
  109. path6.lineTo(200, 420);
  110. path6.lineTo(170, 392);
  111. path6.close();
  112. canvas.drawPath(path6, paint);
  113. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/765510
推荐阅读
相关标签
  

闽ICP备14008679号