本文是基于Android的SurfaceView的动态画图效果,实现静态和动态下的正弦波画图,可作为自己做图的简单参考,废话不多说,先上图,
静态效果:
动态效果:
比较简单,代码注释的也比较详细,易懂,我就直接上代码了吧:
1.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <SurfaceView
- android:layout_width="fill_parent"
- android:layout_height="600px"
- android:id="@+id/surfaceview_draw"/>
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:layout_below="@id/surfaceview_draw"
- android:layout_marginLeft="7dip"
- android:id="@+id/simpledraw_btn"
- android:text="简单绘图"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dip"/>
- <Button
- android:id="@+id/timerdraw_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/simpledraw_btn"
- android:text="定时绘图"
- android:textSize="20dip"/>
- <Button
- android:id="@+id/cleardraw_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/timerdraw_btn"
- android:text="清除图像"
- android:textSize="20dip"/>
- </RelativeLayout>
- </LinearLayout>
2.SurfaceViewDrawActivity.java
- package com.huangcheng.draw;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class SurfaceViewDrawActivity extends Activity {
- /** Called when the activity is first created. */
- private SurfaceView surface_draw;
- private SurfaceHolder surfaceholder;
- private Button simpledraw_btn;
- private Button timerdraw_btn;
- private Button cleardraw_btn;
- private Timer timer;
- private TimerTask timertask;
- private int[] Y;// 保存图像Y轴上的点
- private int centerY, oldX, oldY, currentX;// 保存水平中心线位置,上一个(x,y)点,当先绘制到的x
- private volatile int signal=0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- surface_draw = (SurfaceView) findViewById(R.id.surfaceview_draw);
- surfaceholder = surface_draw.getHolder();
- simpledraw_btn = (Button) findViewById(R.id.simpledraw_btn);
- timerdraw_btn = (Button) findViewById(R.id.timerdraw_btn);
- cleardraw_btn = (Button) findViewById(R.id.cleardraw_btn);
- cleardraw_btn.setEnabled(false);
- centerY = 300;
- Y = new int[getWindowManager().getDefaultDisplay().getWidth()];
- for (int i = 1; i < Y.length + 1; i++) {
- Y[i - 1] = centerY + (int) (100 * Math.sin(i * 2 * Math.PI / 180));
- // System.out.println("sin:"+100*Math.sin(i*2*Math.PI/180));
- }
- simpledraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- simpledraw();// 静态绘制正弦图
- signal=1;
- simpledraw_btn.setEnabled(false);
- timerdraw_btn.setEnabled(false);
- cleardraw_btn.setEnabled(true);
- }
- });
- timerdraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- oldX=0;
- oldY=centerY;
- currentX=0;
- timer=new Timer();
- timertask=new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- currentX++;
- complexdraw(currentX);
- if(currentX==Y.length-1){
- ClearDraw();
- currentX = 0;
- oldX=0;
- oldY=centerY;
- }
- }
- };
- timer.schedule(timertask, 0, 5);// 动态绘制正弦图
- signal=2;
- simpledraw_btn.setEnabled(false);
- timerdraw_btn.setEnabled(false);
- cleardraw_btn.setEnabled(true);
- }
- });
- cleardraw_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if(signal==1){
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- if(signal==2){
- timertask.cancel();
- timer.cancel();
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- }
- });
- }
- // 简单画图
- void simpledraw() {
- oldX = 0;
- oldY = centerY;
- Canvas canvas = surfaceholder
- .lockCanvas(new Rect(oldX, 0, oldX + Y.length,
- getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
- Log.i("Canvas:X:",
- String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
- Paint mPaint = new Paint();
- mPaint.setColor(Color.GREEN);// 画笔为绿色
- mPaint.setStrokeWidth(2);// 设置画笔粗细
- int y;
- for (int i = oldX + 1; i < Y.length; i++) {// 绘画正弦波
- y = Y[i - 1];
- canvas.drawLine(oldX, oldY, i, y, mPaint);
- // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
- oldX = i;
- oldY = y;
- }
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- // 消除画得图
- void ClearDraw() {
- Canvas canvas = surfaceholder.lockCanvas(null);
- canvas.drawColor(Color.BLACK);// 清除画布
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- //动态画图
- void complexdraw(int current){
- Canvas canvas = surfaceholder
- .lockCanvas(new Rect(oldX, 0, current,
- getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
- Log.i("Canvas:X:",
- String.valueOf(oldX) + "," + String.valueOf(oldX + Y.length));
- Paint mPaint = new Paint();
- mPaint.setColor(Color.GREEN);// 画笔为绿色
- mPaint.setStrokeWidth(2);// 设置画笔粗细
- int y=Y[current-1];
- canvas.drawLine(oldX, oldY, current, y, mPaint);
- // System.out.println("oldX:"+oldX+" oldY:"+oldY+",x:"+i+" y:"+y);
- oldX = current;
- oldY = y;
- surfaceholder.unlockCanvasAndPost(canvas);
- }
- }