Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android菜鳥的成長筆記(27)——SurfaceView的使用

Android菜鳥的成長筆記(27)——SurfaceView的使用

編輯:關於Android編程

前面有關自定義View中進行了繪圖,但View的繪圖機制存在如下缺陷:

1、View缺乏雙緩沖機制。

2、當程序需要更新View上的圖像時,程序必須重繪View上顯示的整張圖片。

3、新線程無法直接更新View組件。

由於View存在上面缺陷,所以在游戲開發中一般使用SurfaceView來進行繪制,SurfaceView一般會與SurfaceHolder結合使用,SurfaceHolder用於向與之關聯的SurfaceView上繪圖,調用SurfaceView的getHolder()方法即可獲取SurfaceView關聯的SurfaceHolder.

SurfaceHolder提供了如下方法來獲取Canvas對象:

1、Canvas lockCanvas():鎖定整個SurfaceView對象,獲取該Surface上的Canvas.

2、Canvas lockCanvas(Rect dirty):鎖定SurfaceView上Rect劃分的區域,獲取該Surface上的Canvas.

兩個方法返回的是同一個Canvas,但是第二個方法只對圈出來的區域進行刷新,Canvas繪圖完成後通過unlockCanvasAndPost(canvas)方法來釋放畫布,提交修改。當調用SurfaceHolder的unlockCanvasAndPost方法之後,該方法之前所繪制的圖形還處於緩沖之下,下一次lockCanvas()方法鎖定的區域可能會“遮擋”它。

package com.example.erweimatest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class SurfaceViewTest extends Activity {
	private SurfaceHolder holder;
	private Paint paint;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		paint = new Paint();
		SurfaceView surface = (SurfaceView) findViewById(R.id.show);
		//初始化SurfaceHolder對象
		holder = surface.getHolder();
		holder.addCallback(new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
			
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				//鎖定整個SurfaceView
				Canvas canvas = holder.lockCanvas();
				//繪制背景
				Bitmap back = BitmapFactory.decodeResource(SurfaceViewTest.this.getResources(), R.drawable.bg);
				//繪制背景
				canvas.drawBitmap(back, 0, 0, null);
				//繪制完成,釋放畫布,提交修改
				holder.unlockCanvasAndPost(canvas);
				//重新鎖一次,“持久化”上次所繪制內容
				//本次lockCanvas會遮擋上次lockCanvas
				holder.lockCanvas(new Rect(0, 0, 0, 0));
				holder.unlockCanvasAndPost(canvas);
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				// TODO Auto-generated method stub
				
			}
		});
		
		surface.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					int cx = (int) event.getX();
					int cy = (int) event.getY();
					//鎖定SurfaceView的布局區域,只更新局部內容
					Canvas canvas = holder.lockCanvas(new Rect(cx - 50, cy - 50, cx + 50, cy + 50));
					//保存canvas當前狀態
					canvas.save();
					//旋轉畫布
					canvas.rotate(30, cx, cy);
					paint.setColor(Color.RED);
					//繪制紅色方塊
					canvas.drawRect(cx - 40,  cy - 40, cx, cy, paint);
					//恢復canvas之前的保存狀態
					canvas.restore();
					paint.setColor(Color.GREEN);
					//繪制綠色方塊
					canvas.drawRect(cx, cy, cx + 40, cy + 40, paint);
					//繪制完成,釋放畫布,提交修改
					holder.unlockCanvasAndPost(canvas);
				}
				return false;
			}
		});
	}
}
main.xml





運行效果:

\

可以看出來,第一次繪制的圖形會被第二次的區域遮擋,第三次繪制的圖形可能遮擋第二次繪制的區域,但不會遮擋第一次的區域。如果第二次繪制的區域被第三次的區域所遮擋,第一次所繪制的圖形可能顯露出來。

基於SurfaceView開發的示波器:

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD48cHJlIGNsYXNzPQ=="brush:java;">package com.example.erweimatest; 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.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ShowVawe 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) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SurfaceView surface = (SurfaceView) findViewById(R.id.show); //初始化SurfaceHolder對象 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) { 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() { @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } }); } 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); } } activity_main.xml




運行結果:

鷌J赀i菄^ky系統更新畫面時也只要更新這個范圍即可。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved