Android SurfaceView 詳解(三)

SurfaceView 示波器的例子:直接上代碼瞭就

該程序會根據單擊的按鈕在屏幕上自動繪制正弦波和餘弦波形。程序每次繪制時隻需要繪制(更新)當前點的波形,前面已經繪制的波形無須更新,利用瞭SurfaceHolder的lockCanvas(Rect r)方法

1.佈局文件Layout/show_wave.xml:

 

[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation ="vertical" 
  
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
   
  <Button 
  android:id="@+id/sin" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="正弦曲線" 
  /> 
  <Button 
  android:id="@+id/cos" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="餘弦曲線" 
  /> 
  <SurfaceView 
  android:id="@+id/show" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  />    
</LinearLayout> 

2.主界面Activity:ShowWave

[java]
package com.infy.configuration; 
 
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.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.SurfaceHolder.Callback; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class ShowWave extends Activity{ 
 
    private SurfaceHolder holder; 
    private Paint paint; 
    final int HEIGHT=320; 
    final int WIDTH=320; 
    final int X_OFFSET = 5; 
    private int cx = X_OFFSET; 
    //實際的Y軸的位置 
    int centerY = HEIGHT /2; 
    Timer timer = new Timer(); 
    TimerTask task = null; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
         
        setContentView(R.layout.show_wave); 
         
        final SurfaceView surface = (SurfaceView)findViewById(R.id.show); 
        //初始化SurfaceHolder對象 www.aiwalls.com  
        holder = surface.getHolder(); 
        paint = new Paint(); 
        paint.setColor(Color.GREEN); 
        paint.setStrokeWidth(3); 
         
        Button sin =(Button)findViewById(R.id.sin); 
        Button cos =(Button)findViewById(R.id.cos); 
        OnClickListener listener = (new OnClickListener() { 
             
            @Override 
            public void onClick(final View source) { 
                // TODO Auto-generated method stub 
            drawBack(holder); 
            cx = X_OFFSET; 
            if(task != null){ 
                task.cancel(); 
            } 
             
            task = new TimerTask() { 
                 
                @Override 
                public void run() { 
                    int cy = source.getId() == R.id.sin ? centerY -(int)(100 * Math.sin((cx -5) *2 * Math.PI/150)): 
                                   centerY -(int)(100 * Math.cos((cx-5)*2*Math.PI/150)); 
                    Canvas canvas = holder.lockCanvas(new Rect(cx,cy-2,cx+2,cy+2)); 
                    canvas.drawPoint(cx, cy, paint); 
                    cx++; 
                     
                    if(cx >WIDTH){ 
                         
                        task.cancel(); 
                        task = null; 
                         
                    } 
                    holder.unlockCanvasAndPost(canvas); 
                } 
            }; 
            timer.schedule(task, 0,30); 
                 
            } 
        }); 
         
        sin.setOnClickListener(listener); 
        cos.setOnClickListener(listener); 
        holder.addCallback(new Callback() { 
            public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){ 
                drawBack(holder); 
            } 
 
            @Override 
            public void surfaceCreated(SurfaceHolder holder) { 
                // TODO Auto-generated method stub 
            } 
 
            @Override 
            public void surfaceDestroyed(SurfaceHolder holder) { 
                // TODO Auto-generated method stub 
            timer.cancel();  
            } 
             
        }); 
     
     
    } 
     
     
    private void drawBack(SurfaceHolder holder){ 
        Canvas canvas = holder.lockCanvas(); 
        //繪制白色背景 
        canvas.drawColor(Color.WHITE); 
        Paint p = new Paint(); 
        p.setColor(Color.BLACK); 
        p.setStrokeWidth(2); 
         
        //繪制坐標軸 
        canvas.drawLine(X_OFFSET, centerY, WIDTH, centerY, p); 
        canvas.drawLine(X_OFFSET, 40, X_OFFSET, HEIGHT, p); 
        holder.unlockCanvasAndPost(canvas); 
        holder.lockCanvas(new Rect(0,0,0,0)); 
        holder.unlockCanvasAndPost(canvas); 
         
    } 
     
     

3.最後顯示結果:

 a.正弦曲線

 
b.餘弦曲線:

 

摘自 Damon_tong的專欄

發佈留言