本文是基于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
- ;
- @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 = ;
- Y = new int[getWindowManager().getDefaultDisplay().getWidth()];
- ; i < Y.length + ; i++) {
- Y[i - ] = centerY + ( * Math.sin(i * * Math.PI / ));
- // 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=;
- 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=;
- oldY=centerY;
- currentX=;
- timer=new Timer();
- timertask=new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- currentX++;
- complexdraw(currentX);
- ){
- ClearDraw();
- currentX = ;
- oldX=;
- oldY=centerY;
- }
- }
- };
- timer.schedule(timertask, , );// 动态绘制正弦图
- signal=;
- 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
- ){
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- ){
- timertask.cancel();
- timer.cancel();
- ClearDraw();
- simpledraw_btn.setEnabled(true);
- timerdraw_btn.setEnabled(true);
- }
- }
- });
- }
- // 简单画图
- void simpledraw() {
- oldX = ;
- oldY = centerY;
- Canvas canvas = surfaceholder
- .lockCanvas(, 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();// 设置画笔粗细
- int y;
- ; i < Y.length; i++) {// 绘画正弦波
- y = Y[i - ];
- 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(, 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();// 设置画笔粗细
- ];
- 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);
- }
- }