Android菜鸟的成长笔记(27)——SurfaceView的使用

本文探讨了SurfaceView及其SurfaceHolder在游戏开发中的关键作用,详细介绍了如何利用它们进行高效绘图,避免了传统View绘图机制的缺陷。通过示例展示了如何在SurfaceView上绘制动态图像,实现局部更新,以及与SurfaceHolder结合使用以提高性能。此外,通过实例演示了基于SurfaceView开发示波器的过程,展示了SurfaceView在不同场景下的应用与优化。
摘要由CSDN通过智能技术生成

前面有关自定义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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<SurfaceView  
	android:id="@+id/show"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" 
	/>
</LinearLayout>
运行效果:



可以看出来,第一次绘制的图形会被第二次的区域遮挡,第三次绘制的图形可能遮挡第二次绘制的区域,但不会遮挡第一次的区域。如果第二次绘制的区域被第三次的区域所遮挡,第一次所绘制的图形可能显露出来。

基于SurfaceView开发的示波器:

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<LinearLayout android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:gravity="center"
	>
<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="余旋曲线"
	/>	
</LinearLayout>
<SurfaceView android:id="@+id/show"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center"
	/>
</LinearLayout>
运行结果:

当程序每次绘制正旋波、余旋波上的当前点时,程序无须重绘整个画面,SurfaceHolder只要锁定当前绘制点的小范围即可,系统更新画面时也只要更新这个范围即可。

转载于:https://www.cnblogs.com/lanzhi/p/6468992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值