本文是基於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);
- }
- }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11