码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-05-06 23:42:01      阅读:571      评论:0      收藏:0      [点我收藏+]

标签:android   surfaceview   

前面有关自定义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>
运行效果:

bubuko.com,布布扣

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

基于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>
运行结果:

bubuko.com,布布扣

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

Android菜鸟的成长笔记(27)——SurfaceView的使用,布布扣,bubuko.com

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

标签:android   surfaceview   

原文地址:http://blog.csdn.net/dawanganban/article/details/25127393

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!