前端技术
联系方式
您现在的位置:主页 > 前端技术 > 内容

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

发布时间:2019-01-26 编辑:AG88

前面有关自定义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开发的示波器:

<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" >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




运行结果:

系统更新画面时也只要更新这个范围即可。

http://www.bkjia.com/Androidjc/767177.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/767177.htmlTechArticle前面有关自定义View中进行了绘图,但View的绘图机制存在如下缺陷: 1、View缺乏双缓冲机制。 2、当程序需要更新View上的图像时,程序必须...

本文源自: 凯发国际

上一篇:Android4.2.2 SurfaceFlinger之图形渲染queueBuffer实现和VSYNC的存在感

下一篇:没有了

Copyright © 2005-2016 http://www.erlygoods.com 凯发国际_凯发国际开户礼金_凯发注册送_搜狐财经版权所有 凯发国际_凯发国际开户礼金_凯发注册送_搜狐财经