Android SurfaceView实现静态于动态画图效果

本文是基于Android的SurfaceView的动态画图效果,实现静态和动态下的正弦波画图,可作为自己做图的简单参考,废话不多说,先上图,

静态效果:

Android SurfaceView实现静态于动态画图效果

动态效果:

Android SurfaceView实现静态于动态画图效果

比较简单,代码注释的也比较详细,易懂,我就直接上代码了吧:

1.main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <SurfaceView
  7. android:layout_width="fill_parent"
  8. android:layout_height="600px"
  9. android:id="@+id/surfaceview_draw"/>
  10. <RelativeLayout android:layout_width="fill_parent"
  11. android:layout_height="wrap_content">
  12. <Button android:layout_below="@id/surfaceview_draw"
  13. android:layout_marginLeft="7dip"
  14. android:id="@+id/simpledraw_btn"
  15. android:text="简单绘图"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textSize="20dip"/>
  19. <Button
  20. android:id="@+id/timerdraw_btn"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_toRightOf="@id/simpledraw_btn"
  24. android:text="定时绘图"
  25. android:textSize="20dip"/>
  26. <Button
  27. android:id="@+id/cleardraw_btn"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_toRightOf="@id/timerdraw_btn"
  31. android:text="清除图像"
  32. android:textSize="20dip"/>
  33. </RelativeLayout>
  34. </LinearLayout>

2.SurfaceViewDrawActivity.java

  1. package com.huangcheng.draw;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.app.Activity;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Rect;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.SurfaceHolder;
  12. import android.view.SurfaceView;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. public class SurfaceViewDrawActivity extends Activity {
  17. /** Called when the activity is first created. */
  18. private SurfaceView surface_draw;
  19. private SurfaceHolder surfaceholder;
  20. private Button simpledraw_btn;
  21. private Button timerdraw_btn;
  22. private Button cleardraw_btn;
  23. private Timer timer;
  24. private TimerTask timertask;
  25. private int[] Y;// 保存图像Y轴上的点
  26. private int centerY, oldX, oldY, currentX;// 保存水平中心线位置,上一个(x,y)点,当先绘制到的x
  27. ;
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. surface_draw = (SurfaceView) findViewById(R.id.surfaceview_draw);
  33. surfaceholder = surface_draw.getHolder();
  34. simpledraw_btn = (Button) findViewById(R.id.simpledraw_btn);
  35. timerdraw_btn = (Button) findViewById(R.id.timerdraw_btn);
  36. cleardraw_btn = (Button) findViewById(R.id.cleardraw_btn);
  37. cleardraw_btn.setEnabled(false);
  38. centerY = ;
  39. Y = new int[getWindowManager().getDefaultDisplay().getWidth()];
  40. ; i < Y.length + ; i++) {
  41. Y[i - ] = centerY + ( * Math.sin(i *  * Math.PI / ));
  42. // System.out.println("sin:"+100*Math.sin(i*2*Math.PI/180));
  43. }
  44. simpledraw_btn.setOnClickListener(new OnClickListener() {
  45. @Override
  46. public void onClick(View arg0) {
  47. // TODO Auto-generated method stub
  48. simpledraw();// 静态绘制正弦图
  49. signal=;
  50. simpledraw_btn.setEnabled(false);
  51. timerdraw_btn.setEnabled(false);
  52. cleardraw_btn.setEnabled(true);
  53. }
  54. });
  55. timerdraw_btn.setOnClickListener(new OnClickListener() {
  56. @Override
  57. public void onClick(View arg0) {
  58. // TODO Auto-generated method stub
  59. oldX=;
  60. oldY=centerY;
  61. currentX=;
  62. timer=new Timer();
  63. timertask=new TimerTask() {
  64. @Override
  65. public void run() {
  66. // TODO Auto-generated method stub
  67. currentX++;
  68. complexdraw(currentX);
  69. ){
  70. ClearDraw();
  71. currentX = ;
  72. oldX=;
  73. oldY=centerY;
  74. }
  75. }
  76. };
  77. timer.schedule(timertask, , );// 动态绘制正弦图
  78. signal=;
  79. simpledraw_btn.setEnabled(false);
  80. timerdraw_btn.setEnabled(false);
  81. cleardraw_btn.setEnabled(true);
  82. }
  83. });
  84. cleardraw_btn.setOnClickListener(new OnClickListener() {
  85. @Override
  86. public void onClick(View arg0) {
  87. // TODO Auto-generated method stub
  88. ){
  89. ClearDraw();
  90. simpledraw_btn.setEnabled(true);
  91. timerdraw_btn.setEnabled(true);
  92. }
  93. ){
  94. timertask.cancel();
  95. timer.cancel();
  96. ClearDraw();
  97. simpledraw_btn.setEnabled(true);
  98. timerdraw_btn.setEnabled(true);
  99. }
  100. }
  101. });
  102. }
  103. // 简单画图
  104. void simpledraw() {
  105. oldX = ;
  106. oldY = centerY;
  107. Canvas canvas = surfaceholder
  108. .lockCanvas(, oldX + Y.length,
  109. getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
  110. Log.i("Canvas:X:",
  111. String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
  112. Paint mPaint = new Paint();
  113. mPaint.setColor(Color.GREEN);// 画笔为绿色
  114. mPaint.setStrokeWidth();// 设置画笔粗细
  115. int y;
  116. ; i < Y.length; i++) {// 绘画正弦波
  117. y = Y[i - ];
  118. canvas.drawLine(oldX, oldY, i, y, mPaint);
  119. // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
  120. oldX = i;
  121. oldY = y;
  122. }
  123. surfaceholder.unlockCanvasAndPost(canvas);
  124. }
  125. // 消除画得图
  126. void ClearDraw() {
  127. Canvas canvas = surfaceholder.lockCanvas(null);
  128. canvas.drawColor(Color.BLACK);// 清除画布
  129. surfaceholder.unlockCanvasAndPost(canvas);
  130. }
  131. //动态画图
  132. void complexdraw(int current){
  133. Canvas canvas = surfaceholder
  134. .lockCanvas(, current,
  135. getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
  136. Log.i("Canvas:X:",
  137. String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
  138. Paint mPaint = new Paint();
  139. mPaint.setColor(Color.GREEN);// 画笔为绿色
  140. mPaint.setStrokeWidth();// 设置画笔粗细
  141. ];
  142. canvas.drawLine(oldX, oldY, current, y, mPaint);
  143. // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
  144. oldX = current;
  145. oldY = y;
  146. surfaceholder.unlockCanvasAndPost(canvas);
  147. }
  148. }
上一篇:JMS开源比较


下一篇:【规范】javascript 变量命名规则