码迷,mamicode.com
首页 > 移动开发 > 详细

安卓突击:绘制图形和文本

时间:2015-07-13 00:34:20      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

drawPoint
drawLine
drawCircle
drawArc
drawText

创建一个类继承于View
首先创建一个Paint对象,给这个Paint对象设置线的颜色,大小等风格。然后在onDraw方法里,通过Canvas对象来调用划线函数,最后一个参数是paint对象。
invalidate()方法是:让屏幕刷新一次。即是将所有的图形都抹掉,重新画。
在画弧线的时候,可以设置是不是选择经过圆心。

在Activity的onCreate的方法里,setContentView()函数里的参数是new 一个视图的对象。
下面这个例子是,点击一次屏幕,重新绘制一遍屏幕上的线或者字。其中有一个是画扇形的时候不经过圆心,有一个是经过的。

  1. package cn.eoe.draw;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.RectF;
  8. import android.graphics.Paint.Style;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. public class Main extends Activity
  13. {
  14. class MyView extends View
  15. {
  16. private Paint paint1 = new Paint();
  17. private Paint paint2 = new Paint();
  18. private Paint paint3 = new Paint();
  19. private boolean useCenter = true;
  20. private float[] textSizeArray = new float[]
  21. { 15, 18, 21, 24, 27 };
  22. @Override
  23. public boolean onTouchEvent(MotionEvent event)
  24. {
  25. if (useCenter)
  26. {
  27. useCenter = false;
  28. paint1.setColor(Color.RED);
  29. paint2.setColor(Color.BLACK);
  30. paint3.setColor(Color.GREEN);
  31. paint1.setStrokeWidth(6);
  32. paint2.setStrokeWidth(4);
  33. paint3.setStrokeWidth(2);
  34. }
  35. else
  36. {
  37. useCenter = true;
  38. paint1.setColor(Color.BLACK);
  39. paint2.setColor(Color.RED);
  40. paint3.setColor(Color.BLUE);
  41. paint1.setStrokeWidth(2);
  42. paint2.setStrokeWidth(4);
  43. paint3.setStrokeWidth(6);
  44. }
  45. for (int i = 0; i < textSizeArray.length / 2; i++)
  46. {
  47. float textSize = textSizeArray[i];
  48. textSizeArray[i] = textSizeArray[textSizeArray.length - i - 1];
  49. textSizeArray[textSizeArray.length - i - 1] = textSize;
  50. }
  51. invalidate();
  52. return super.onTouchEvent(event);
  53. }
  54. public MyView(Context context)
  55. {
  56. super(context);
  57. setBackgroundColor(Color.WHITE);
  58. paint1.setColor(Color.BLACK);
  59. paint1.setStrokeWidth(2);
  60. paint2.setColor(Color.RED);
  61. paint2.setStrokeWidth(4);
  62. paint3.setColor(Color.BLUE);
  63. paint3.setStrokeWidth(6);
  64. }
  65. private void drawLinesExt(Canvas canvas, float[] pts, Paint paint)
  66. {
  67. float[] points = new float[pts.length * 2 - 4];
  68. for (int i = 0, j = 0; i < pts.length; i = i + 2)
  69. {
  70. points[j++] = pts[i];
  71. points[j++] = pts[i + 1];
  72. if (i > 1 && i < pts.length - 2)
  73. {
  74. points[j++] = pts[i];
  75. points[j++] = pts[i + 1];
  76. }
  77. }
  78. canvas.drawLines(points, paint);
  79. }
  80. @Override
  81. protected void onDraw(Canvas canvas)
  82. {
  83. canvas.drawPoint(60, 120, paint3);
  84. canvas.drawPoint(70, 130, paint3);
  85. canvas.drawPoints(new float[]
  86. { 70, 140, 75, 145, 75, 160 }, paint2);
  87. //canvas.drawPoints(new float[]
  88. // { 70, 140, 75, 145, 75, 160 }, 1,4,paint2);
  89. canvas.drawLine(10, 10, 300, 10, paint1);
  90. canvas.drawLine(10, 30, 300, 30, paint2);
  91. canvas.drawLine(10, 50, 300, 50, paint3);
  92. drawLinesExt(canvas, new float[]
  93. { 10, 70, 120, 70, 120, 170, 10, 170, 10, 70 }, paint2);
  94. drawLinesExt(canvas, new float[]
  95. { 25, 85, 105, 85, 105, 155, 25, 155, 25, 85 }, paint3);
  96. drawLinesExt(canvas, new float[]
  97. { 160, 70, 230, 150, 170, 155, 160, 70 }, paint2);
  98. paint2.setStyle(Style.STROKE);
  99. canvas.drawCircle(260, 110, 40, paint2);
  100. paint2.setStyle(Style.FILL);
  101. canvas.drawCircle(260, 110, 30, paint2);
  102. RectF rectF = new RectF();
  103. rectF.left = 30;
  104. rectF.top = 190;
  105. rectF.right = 120;
  106. rectF.bottom = 280;
  107. canvas.drawArc(rectF, 0, 200, useCenter, paint2);
  108. rectF.left = 140;
  109. rectF.top = 190;
  110. rectF.right = 280;
  111. rectF.bottom = 290;
  112. paint2.setStyle(Style.STROKE);
  113. canvas.drawArc(rectF, 0, 360, useCenter, paint2);
  114. rectF.left = 160;
  115. rectF.top = 190;
  116. rectF.right = 260;
  117. rectF.bottom = 290;
  118. paint3.setStyle(Style.STROKE);
  119. canvas.drawArc(rectF, 0, 360, useCenter, paint3);
  120. float y = 0;
  121. for (int i = 0; i < textSizeArray.length; i++)
  122. {
  123. paint1.setTextSize(textSizeArray[i]);
  124. paint1.setColor(Color.BLUE);
  125. canvas.drawText("Android(宽度:" + paint1.measureText("Android")
  126. + ")", 20, 315 + y, paint1);
  127. y += paint1.getTextSize() + 5;
  128. }
  129. paint1.setTextSize(22);
  130. }
  131. }
  132. @Override
  133. public void onCreate(Bundle savedInstanceState)
  134. {
  135. super.onCreate(savedInstanceState);
  136. setContentView(new MyView(this));
  137. }
  138. }





安卓突击:绘制图形和文本

标签:

原文地址:http://www.cnblogs.com/zhuzhenfeng/p/4641762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!