使用SurfaceView實現正弦餘弦曲線的繪製動畫!java
主要代碼以下:android
1 package com.example.surfaceviewtest; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import android.app.Activity; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.graphics.Rect; 11 import android.os.Bundle; 12 import android.util.Log; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.view.SurfaceHolder; 16 import android.view.SurfaceHolder.Callback; 17 import android.view.SurfaceView; 18 19 public class ShowWave extends Activity { 20 21 SurfaceView surfaceView; 22 SurfaceHolder holder; 23 Paint paint; 24 final int HEIGHT = 320; 25 final int WIDTH = 320; 26 final int X_OFFSET = 5; 27 private int cx = X_OFFSET; 28 int CenterY = HEIGHT / 2; 29 Timer timer; 30 TimerTask task = null; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 surfaceView = (SurfaceView) findViewById(R.id.show); 38 39 holder = surfaceView.getHolder(); 40 holder.addCallback(new Callback() { 41 42 @Override 43 public void surfaceDestroyed(SurfaceHolder holder) { 44 Log.i("csx", "surfaceDestroyed"); 45 timer.cancel(); 46 } 47 48 @Override 49 public void surfaceCreated(SurfaceHolder holder) { 50 Log.i("csx", "surfaceCreated"); 51 timer = new Timer(); 52 drawBack(holder); 53 54 } 55 56 @Override 57 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 58 Log.i("csx", "surfaceChanged"); 59 60 } 61 }); 62 63 paint = new Paint(); 64 paint.setColor(Color.GREEN); 65 paint.setStrokeWidth(3); 66 67 } 68 69 @Override 70 public boolean onCreateOptionsMenu(Menu menu) { 71 72 getMenuInflater().inflate(R.menu.main, menu); 73 return true; 74 } 75 76 @Override 77 public boolean onOptionsItemSelected(MenuItem item) { 78 79 int id = item.getItemId(); 80 drawWave(id); 81 return super.onOptionsItemSelected(item); 82 } 83 84 public void drawWave(final int item_id) { 85 if (task != null) { 86 task.cancel(); 87 } 88 drawBack(holder); 89 cx = X_OFFSET; 90 91 task = new TimerTask() { 92 93 @Override 94 public void run() { 95 int cy = item_id == R.id.sin ? CenterY 96 - (int) (100 * Math.sin((cx - 5) * 2 * Math.PI / 150)) : CenterY 97 - (int) (100 * Math.cos((cx - 5) * 2 * Math.PI / 150)); 98 99 try { 100 Canvas canvas = holder.lockCanvas(new Rect(cx, cy - 2, cx + 2, cy + 2)); 101 canvas.drawPoint(cx, cy, paint); 102 cx++; 103 if (cx > WIDTH) { 104 task.cancel(); 105 task = null; 106 } 107 holder.unlockCanvasAndPost(canvas); 108 } catch (NullPointerException e) { 109 return; 110 } 111 112 } 113 }; 114 115 timer.schedule(task, 0, 30); 116 117 } 118 119 private void drawBack(SurfaceHolder holder) { 120 Canvas canvas = holder.lockCanvas(); 121 canvas.drawColor(Color.WHITE); 122 Paint p = new Paint(); 123 p.setColor(Color.BLACK); 124 p.setStrokeWidth(3); 125 canvas.drawLine(X_OFFSET, CenterY, WIDTH, CenterY, p); 126 canvas.drawLine(X_OFFSET, 40, X_OFFSET, HEIGHT, p); 127 holder.unlockCanvasAndPost(canvas); 128 holder.lockCanvas(new Rect(0, 0, 0, 0)); 129 holder.unlockCanvasAndPost(canvas); 130 131 } 132 133 }