一个可交互的Android绘制曲线的demo:

Java代码
  1. packagecom.ray.demo;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.graphics.Paint.Style;
  8. importandroid.graphics.Path;
  9. importandroid.graphics.Point;
  10. importandroid.graphics.RectF;
  11. importandroid.os.Bundle;
  12. importandroid.view.MotionEvent;
  13. importandroid.view.View;
  14. publicclassLineFunActivityextendsActivity{
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(newSampleView(this));
  19. }
  20. privateclassSampleViewextendsView{
  21. publicstaticfinalintRECT_SIZE=8;
  22. privatePointmSelectedPoint=null;
  23. publicstaticfinalintPOINT_ARRAY_SIZE=7;
  24. publicstaticfinalintC_START=0;
  25. publicstaticfinalintC_END=1;
  26. publicstaticfinalintC_CONTROL_1=2;
  27. publicstaticfinalintC_CONTROL_2=3;
  28. publicstaticfinalintQ_START=4;
  29. publicstaticfinalintQ_END=5;
  30. publicstaticfinalintQ_CONTROL=6;
  31. privatePoint[]mPoints=newPoint[POINT_ARRAY_SIZE];
  32. publicSampleView(Contextcontext){
  33. super(context);
  34. mPoints[C_START]=newPoint(100,100);
  35. mPoints[C_END]=newPoint(200,200);
  36. mPoints[C_CONTROL_1]=newPoint(150,100);
  37. mPoints[C_CONTROL_2]=newPoint(150,200);
  38. mPoints[Q_START]=newPoint(100,300);
  39. mPoints[Q_END]=newPoint(150,400);
  40. mPoints[Q_CONTROL]=newPoint(200,350);
  41. }
  42. @Override
  43. protectedvoidonDraw(Canvascanvas){
  44. super.onDraw(canvas);
  45. canvas.drawColor(Color.WHITE);
  46. //setuppaint
  47. Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);
  48. paint.setColor(Color.BLACK);
  49. //drawthecubicline
  50. Pathpath=newPath();
  51. path.moveTo(mPoints[C_START].x,mPoints[C_START].y);
  52. path.cubicTo(mPoints[C_CONTROL_1].x,mPoints[C_CONTROL_1].y,
  53. mPoints[C_CONTROL_2].x,mPoints[C_CONTROL_2].y,
  54. mPoints[C_END].x,mPoints[C_END].y);
  55. paint.setStrokeWidth(2);
  56. paint.setStyle(Style.STROKE);
  57. canvas.drawPath(path,paint);
  58. canvas.drawLine(mPoints[C_START].x,mPoints[C_START].y,
  59. mPoints[C_CONTROL_1].x,mPoints[C_CONTROL_1].y,paint);
  60. canvas.drawLine(mPoints[C_END].x,mPoints[C_END].y,
  61. mPoints[C_CONTROL_2].x,mPoints[C_CONTROL_2].y,paint);
  62. //drawthequadline
  63. paint.setColor(Color.BLACK);
  64. paint.setStyle(Style.FILL);
  65. paint.setStrokeWidth(2);
  66. path.reset();
  67. path.moveTo(mPoints[Q_START].x,mPoints[Q_START].y);
  68. path.quadTo(mPoints[Q_CONTROL].x,mPoints[Q_CONTROL].y,
  69. mPoints[Q_END].x,mPoints[Q_END].y);
  70. canvas.drawPath(path,paint);
  71. canvas.drawLine(mPoints[Q_START].x,mPoints[Q_START].y,
  72. mPoints[Q_CONTROL].x,mPoints[Q_CONTROL].y,paint);
  73. canvas.drawLine(mPoints[Q_END].x,mPoints[Q_END].y,
  74. mPoints[Q_CONTROL].x,mPoints[Q_CONTROL].y,paint);
  75. //drawcontrolpoints
  76. paint.setColor(Color.RED);
  77. paint.setStyle(Style.FILL);
  78. for(inti=0;i<POINT_ARRAY_SIZE;i++){
  79. canvas.drawRect(pointToRect(mPoints[i]),paint);
  80. }
  81. }
  82. @Override
  83. publicbooleanonTouchEvent(MotionEventevent){
  84. switch(event.getAction()){
  85. caseMotionEvent.ACTION_DOWN:
  86. for(inti=0;i<POINT_ARRAY_SIZE;i++){
  87. if(pointToRect(mPoints[i]).contains(event.getX(),event.getY())){
  88. mSelectedPoint=mPoints[i];
  89. }
  90. }
  91. break;
  92. caseMotionEvent.ACTION_MOVE:
  93. if(null!=mSelectedPoint){
  94. mSelectedPoint.x=(int)event.getX();
  95. mSelectedPoint.y=(int)event.getY();
  96. invalidate();
  97. }
  98. break;
  99. caseMotionEvent.ACTION_UP:
  100. mSelectedPoint=null;
  101. break;
  102. default:
  103. break;
  104. }
  105. returntrue;
  106. }
  107. privateRectFpointToRect(Pointp){
  108. returnnewRectF(p.x-RECT_SIZE/2,p.y-RECT_SIZE/2,
  109. p.x+RECT_SIZE/2,p.y+RECT_SIZE/2);
  110. }
  111. }
  112. }

更多相关文章

  1. Android(安卓)删除手机联系人,添加手机联系人,更新手机联系人信
  2. Android(安卓)代码改变图片颜色android:tint="@color/main_color
  3. eclipse中关联android源码
  4. android 代码proguard
  5. Android编程: 调试方法
  6. Android——全屏显示的两种方式
  7. Android(安卓)OpenGL ES 开发教程小结
  8. android随笔
  9. Android(安卓)JNI简单实例(android 调用C/C++代码)

随机推荐

  1. Android学习—LinearLayout布局中实现左
  2. Android中自定义shape
  3. Android 动态加载(二) - 基础篇(二)
  4. android常用的一些属性说明
  5. android笔记(一)
  6. Android SDK离线快速安装教程 Android SD
  7. Android(安卓)Studio 错误: 非法字符: '
  8. Android 动态加载(三) - 类的加载流程源码
  9. Android Layout属性笔记
  10. 《Android 从初学者入门到成为高手 视频