首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图

[转]android 几何图形的作图

2012-07-25 来源:读书人网 【读书人网(Reader8.cn):综合教育门户网站】
[转]android 几何图形的绘制转自:http://byandby.iteye.com/blog/826418??以前的文章里边画一般都是一些矩

[转]android 几何图形的绘制

转自:http://byandby.iteye.com/blog/826418

?

?以前的文章里边画一般都是一些矩形,今天就看看怎么在android手机屏幕上绘制一些几何图形,如三角形、多边形、椭圆、圆形、正方形 等等。并且设置 空心、实心。下面我们先来看看?
在android中可以绘制出那些几何图形?
?????
???? 方法????????????????????????????????????????????????????????????????????? 说明?
??? drawRect????????????????????????????????????????????????????????????? 绘制矩形?
??? drawCircle??????????????????????????????????????????????????????????? 绘制圆形?
??? drawOval????????????????????????????????????????????????????????????? 绘制椭圆?
??? drawPath???????????????????????????????????????????????????????????? 绘制任意多边形?
??? drawLine????????????????????????????????????????????????????????????? 绘制直线?
??? drawPoin????????????????????????????????????????????????????????????? 绘制点?

???我们先看看运行效果---------------------。。。。。?
[转]android 几何图形的作图?

? 下面我们看例子吧?
? 布局文件?
<?xml version="1.0" encoding="utf-8"?>?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"?
??? android:orientation="vertical"?
??? android:layout_width="fill_parent"?
??? android:layout_height="fill_parent"?
??? >?
<TextView??
??? android:layout_width="fill_parent"?
??? android:layout_height="wrap_content"?
??? android:text="@string/hello"?
??? android:textColor="#00FF00"?
??? />?
<xiaohang.zhimeng.GameView?
android:layout_width="wrap_content"?
android:layout_height="wrap_content"?
/>?
</LinearLayout>?
?? 红色的部分 是我们自己实现的 GameView此类继承了View 我们把它作为布局文件的一部分加载了进来。?
? 下边是代码?
Activity01?? 比较简单 但是为了保持完整我还是贴上来吧。?
Java代码??[转]android 几何图形的作图
  1. package?xiaohang.zhimeng;??
  2. ??
  3. import?android.app.Activity;??
  4. import?android.os.Bundle;??
  5. ??
  6. public?class?Activity01?extends?Activity?{??
  7. ????@Override??
  8. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  9. ????????super.onCreate(savedInstanceState);??
  10. ????????setContentView(R.layout.main);??
  11. ????}??
  12. }??

???
? GameView?
Java代码??[转]android 几何图形的作图
  1. package?xiaohang.zhimeng;??
  2. ??
  3. import?android.content.Context;??
  4. import?android.graphics.Canvas;??
  5. import?android.graphics.Color;??
  6. import?android.graphics.Paint;??
  7. import?android.graphics.Path;??
  8. import?android.graphics.Rect;??
  9. import?android.graphics.RectF;??
  10. import?android.util.AttributeSet;??
  11. import?android.view.View;??
  12. ??
  13. public?class?GameView?extends?View?implements?Runnable?{??
  14. ????//?声明Paint对象??
  15. ????private?Paint?mPaint?=?null;??
  16. ??????
  17. ????private?GameView2?mGameView2?=?null;??
  18. ??????
  19. ????public?GameView(Context?context,?AttributeSet?attr){??
  20. ????????super(context,attr);??
  21. ????????System.out.println(1);??
  22. ????????//?构建画笔对象??
  23. ????????mPaint?=?new?Paint();??
  24. ??
  25. ????????mGameView2?=?new?GameView2(context);??
  26. ????????//?开启线程??
  27. ????????new?Thread(this).start();??
  28. ????}??
  29. ??
  30. ????@Override??
  31. ????protected?void?onDraw(Canvas?canvas)?{??
  32. ????????super.onDraw(canvas);??
  33. ??
  34. ????????//?设置画布为黑色背景??
  35. ????????canvas.drawColor(Color.BLACK);??
  36. ??
  37. ????????//?取消锯齿??
  38. ????????mPaint.setAntiAlias(true);??
  39. ??
  40. ????????//?设置画笔风格为空心??
  41. ????????mPaint.setStyle(Paint.Style.STROKE);??
  42. ??
  43. ????????{??
  44. ????????????//?定义矩形对象??
  45. ????????????Rect?rect1?=?new?Rect();??
  46. ????????????//?设置矩形大小??
  47. ????????????rect1.left?=?5;??
  48. ????????????rect1.top?=?5;??
  49. ????????????rect1.bottom?=?25;??
  50. ????????????rect1.right?=?45;??
  51. ??
  52. ????????????mPaint.setColor(Color.BLUE);??
  53. ????????????//?绘制矩形??
  54. ????????????canvas.drawRect(rect1,?mPaint);??
  55. ??
  56. ????????????mPaint.setColor(Color.RED);??
  57. ????????????//?绘制矩形??
  58. ????????????canvas.drawRect(50,?5,?90,?25,?mPaint);??
  59. ??
  60. ????????????mPaint.setColor(Color.YELLOW);??
  61. ????????????//?绘制圆形??
  62. ????????????//?40?70?分别是圆心的X?和?Y坐标?30为半径?mPaint为画笔对象??
  63. ????????????canvas.drawCircle(40,?70,?30,?mPaint);??
  64. ??
  65. ????????????//?定义椭圆??
  66. ????????????RectF?rectf1?=?new?RectF();??
  67. ????????????rectf1.left?=?80;??
  68. ????????????rectf1.top?=?30;??
  69. ????????????rectf1.right?=?120;??
  70. ????????????rectf1.bottom?=?70;??
  71. ??
  72. ????????????mPaint.setColor(Color.LTGRAY);??
  73. ????????????//?绘制椭圆??
  74. ????????????canvas.drawOval(rectf1,?mPaint);??
  75. ??
  76. ????????????//?绘制多边形??
  77. ????????????Path?path1?=?new?Path();??
  78. ??
  79. ????????????/**?
  80. ?????????????*?这个多变形我也没试验它到底是怎么画?应该就是从起点?找点?一个点?一个点的连线?
  81. ?????????????*/??
  82. ????????????path1.moveTo(150?+?5,?80?-?50);?//?此点为多边形的起点??
  83. ????????????path1.lineTo(150?+?45,?80?-?50);??
  84. ????????????path1.lineTo(150?+?30,?120?-?50);??
  85. ????????????path1.lineTo(150?+?20,?120?-?50);??
  86. ????????????//?使这些点构成封闭的多边形??
  87. ????????????path1.close();??
  88. ??
  89. ????????????mPaint.setColor(Color.GRAY);??
  90. ????????????//?绘制这个多边形??
  91. ????????????canvas.drawPath(path1,?mPaint);??
  92. ??
  93. ????????????mPaint.setColor(Color.RED);??
  94. ????????????//?设置画笔空心边框的宽度??
  95. ????????????mPaint.setStrokeWidth(3);??
  96. ????????????//?绘制直线??
  97. ????????????//?这个绘制直线的方法?前2个参数是前点坐标?后?2个参数是?终点坐标我们可看出两个点的Y坐标都一样的??
  98. ????????????canvas.drawLine(5,?110,?315,?110,?mPaint);??
  99. ????????}??
  100. ??
  101. ????????//?绘制实心几何体??
  102. ????????//?将画笔设置为实心??
  103. ????????mPaint.setStyle(Paint.Style.FILL);??
  104. ????????{??
  105. ????????????//?定义矩形??
  106. ????????????Rect?rect1?=?new?Rect();??
  107. ????????????rect1.left?=?5;??
  108. ????????????rect1.top?=?130?+?5;??
  109. ????????????rect1.bottom?=?130?+?25;??
  110. ????????????rect1.right?=?45;??
  111. ??
  112. ????????????mPaint.setColor(Color.BLUE);??
  113. ????????????//?绘制矩形??
  114. ????????????canvas.drawRect(rect1,?mPaint);??
  115. ??
  116. ????????????mPaint.setColor(Color.RED);??
  117. ????????????//?绘制矩形??
  118. ????????????canvas.drawRect(50,?130?+?5,?90,?130?+?25,?mPaint);??
  119. ??
  120. ????????????mPaint.setColor(Color.YELLOW);??
  121. ????????????//?绘制圆形?这里参数就不说明了?上边已经说了??
  122. ????????????canvas.drawCircle(40,?130?+?70,?30,?mPaint);??
  123. ??
  124. ????????????//?定义椭圆对象??
  125. ????????????RectF?rectf1?=?new?RectF();??
  126. ????????????//?设置椭圆大小??
  127. ????????????rectf1.left?=?80;??
  128. ????????????rectf1.top?=?130+30;??
  129. ????????????rectf1.right?=?120;??
  130. ????????????rectf1.bottom?=?130?+?70;??
  131. ??
  132. ????????????mPaint.setColor(Color.LTGRAY);??
  133. ????????????//?绘制椭圆??
  134. ????????????canvas.drawOval(rectf1,?mPaint);??
  135. ??
  136. ????????????//?绘制多边形??
  137. ????????????Path?path1?=?new?Path();??
  138. ??
  139. ????????????//?设置多边形的点??
  140. ????????????path1.moveTo(150+5,?130+80-50);??
  141. ????????????path1.lineTo(150+45,?130+80-50);??
  142. ????????????path1.lineTo(150+30,?130+120-50);??
  143. ????????????path1.lineTo(150+20,?130+120-50);??
  144. ????????????//?使这些点构成封闭的多边形??
  145. ????????????path1.close();??
  146. ????????????mPaint.setColor(Color.GRAY);??
  147. ????????????//?绘制这个多边形??
  148. ????????????canvas.drawPath(path1,?mPaint);??
  149. ??
  150. ????????????mPaint.setColor(Color.RED);??
  151. ????????????mPaint.setStrokeWidth(3);??
  152. ????????????//?绘制直线??
  153. ????????????canvas.drawLine(5,?130?+?110,?315,?130?+?110,?mPaint);??
  154. ????????}??
  155. ????????//?通过ShapDrawable来绘制几何图形??
  156. ????????mGameView2.DrawShape(canvas);??
  157. ??
  158. ????}??
  159. ??
  160. ????@Override??
  161. ????public?void?run()?{??
  162. ????????while?(!Thread.currentThread().isInterrupted())?{??
  163. ????????????try?{??
  164. ????????????????Thread.sleep(100);??
  165. ????????????}?catch?(InterruptedException?e)?{??
  166. ????????????????Thread.currentThread().interrupt();??
  167. ????????????}??
  168. ????????????//?使用postInvalidate可以直接在线程中更新界面??
  169. ????????????postInvalidate();??
  170. ????????}??
  171. ??
  172. ????}??
  173. ??
  174. }??


?? 在android中还可以通过ShapDrawable来绘制图像,ShapDrawable可以设置画笔的形状。通过 getPaint 方法可以得到Paint对象,可以像前面一样设置这个画笔的颜色、尺寸等属性。然而,在ShapDrawable中提供了 setBounds 方法来设置图形显示的区域,最后通过ShapeDrawable 和 Draw方法将图形显示到屏幕上。 请见下边的代码?
? GameView2?
Java代码??[转]android 几何图形的作图
  1. package?xiaohang.zhimeng;??
  2. ??
  3. import?android.content.Context;??
  4. import?android.graphics.Canvas;??
  5. import?android.graphics.Color;??
  6. import?android.graphics.LinearGradient;??
  7. import?android.graphics.Paint;??
  8. import?android.graphics.Path;??
  9. import?android.graphics.Rect;??
  10. import?android.graphics.Shader;??
  11. import?android.graphics.drawable.ShapeDrawable;??
  12. import?android.graphics.drawable.shapes.OvalShape;??
  13. import?android.graphics.drawable.shapes.PathShape;??
  14. import?android.graphics.drawable.shapes.RectShape;??
  15. import?android.view.View;??
  16. ??
  17. public?class?GameView2?extends?View?{??
  18. ??
  19. ????//?声明ShapDrawable对象??
  20. ????ShapeDrawable?mShapeDrawable?=?null;??
  21. ??
  22. ????public?GameView2(Context?context)?{??
  23. ????????super(context);??
  24. ????}??
  25. ??
  26. ????public?void?DrawShape(Canvas?canvas)?{??
  27. ????????//?实例化ShapeDrawable对象并说明是绘制一个矩形??
  28. ????????mShapeDrawable?=?new?ShapeDrawable(new?RectShape());??
  29. ??
  30. ????????//?得到画笔paint对象并设置其颜色??
  31. ????????mShapeDrawable.getPaint().setColor(Color.RED);??
  32. ??
  33. ????????Rect?bounds?=?new?Rect(5,?250,?55,?280);??
  34. ??
  35. ????????//?设置图像显示的区域??
  36. ????????mShapeDrawable.setBounds(bounds);??
  37. ??
  38. ????????//?绘制图像??
  39. ????????mShapeDrawable.draw(canvas);??
  40. ????????/*?===============================?*/??
  41. ????????/*?实例化ShapeDrawable对象并说明是绘制一个椭圆?*/??
  42. ????????mShapeDrawable?=?new?ShapeDrawable(new?OvalShape());??
  43. ??
  44. ????????//?得到画笔paint对象并设置其颜色??
  45. ????????mShapeDrawable.getPaint().setColor(Color.GREEN);??
  46. ??
  47. ????????//?设置图像显示的区域??
  48. ????????mShapeDrawable.setBounds(70,?250,?150,?280);??
  49. ??
  50. ????????//?绘制图像??
  51. ????????mShapeDrawable.draw(canvas);??
  52. ??
  53. ????????Path?path1?=?new?Path();??
  54. ????????//?设置多边形??
  55. ????????path1.moveTo(150?+?5,?80?+?80?-?50);??
  56. ????????path1.lineTo(150?+?45,?80?+?80?-?50);??
  57. ????????path1.lineTo(150?+?30,?80?+?120?-?50);??
  58. ????????path1.lineTo(150?+?20,?80?+?120?-?50);??
  59. ????????//?使这些点封闭成多边形??
  60. ????????path1.close();??
  61. ??
  62. ????????//?PathShape后面两个参数分别是高度和宽度??
  63. ????????mShapeDrawable?=?new?ShapeDrawable(new?PathShape(path1,?150,?150));??
  64. ??
  65. ????????//?得到画笔paint对象并设置其颜色??
  66. ????????mShapeDrawable.getPaint().setColor(Color.BLUE);??
  67. ??
  68. ????????//?设置图像显示的区域??
  69. ????????mShapeDrawable.setBounds(100,?170,?200,?280);??
  70. ??
  71. ????????//?绘制图像??
  72. ????????mShapeDrawable.draw(canvas);??
  73. ??????????
  74. ????????//绘制正方形??
  75. ????????mShapeDrawable?=?new?ShapeDrawable(new?RectShape());??
  76. ????????//得到画笔并设置颜色??
  77. ????????Paint?xh_Paint?=?mShapeDrawable.getPaint();??
  78. ??????????
  79. ??????????/*设置渐变色?这个正方形的颜色是改变的*/??
  80. ????????Shader?mShader=new?LinearGradient(0,0,100,100,??
  81. ????????????????new?int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.LTGRAY},??
  82. ????????????????null,Shader.TileMode.REPEAT);??
  83. ????????xh_Paint.setShader(mShader);??
  84. ??????????
  85. ????????mShapeDrawable.setBounds(250,?250,?280,?280);??
  86. ????????mShapeDrawable.draw(canvas);??
  87. ????}??
  88. }??


?? 大家感兴趣可以 下载源码 运行看看?