SurfaceView不能直接使用,需要使用时需要基于他派生出我们自己的类,并导出SurfaceHolder.Callback接口并实现。SurfaceView继承于视图类(View),能够实现线程绘图主要是因为其内部包含一个专门用于绘制的Surface。人们通过getHolder()获得Surface的句柄,然后通过SurfaceHolder接口的callback来使用他。SurfaceHolder的使用周期与surfaceview有关,surfaceview可见时,surface被创建;surfaceview不可见时,surface被销毁,且在surfaceview不可见之前。这样设计大概是基于节省资源考虑。surfaceview的核心提供了两个线程:UI线程和渲染线程。所有SurfaceView和SurfaceHolder.Callback的方法都应该在UI线程里调用,一般来说就是应用程序主线程。渲染线程所要访问的各种变量应该作同步处理。由于surface可能被销毁,它只在SurfaceHolder.Callback.surfaceCreated()和 SurfaceHolder.Callback.surfaceDestroyed()之间有效,所以要确保渲染线程访问的是合法有效的surface。而我们的一些资源变量初始化和释放也尽量在这两个线方法之中。
SurfaceHolder在android说明文档中定义如下:
// 编译自SurfaceHolder.java (版本 1.5:49.0,无超级位)
public abstract static interface android.view.SurfaceHolder$Callback {
// 方法描述符 #4(Landroid/view/SurfaceHolder;)V
public abstract voidsurfaceCreated(android.view.SurfaceHolder arg0);
// 方法描述符 #6(Landroid/view/SurfaceHolder;III)V
public abstract voidsurfaceChanged(android.view.SurfaceHolder arg0, int arg1, int arg2, int arg3);
// 方法描述符 #4(Landroid/view/SurfaceHolder;)V
public abstract voidsurfaceDestroyed(android.view.SurfaceHolder arg0);
内部类:
[内部类信息: #1android/view/SurfaceHolder$Callback, 外部类信息: #10android/view/SurfaceHolder
内部名: #12 Callback, 访问标志:1545 public abstract static]
}
调用SurfaceHolder需要重载这三个方法,然后通过lockCanvas方法获得画布绘图,绘结束后使用unlockCanvasAndPost提交给前台窗口刷新。流程是先获得SurfaceHolder,然后添加回调,实现三个接口,最后在lockCanvas和unlockCanvasAndPost之间实现代码:
一个简单的调用例子如下:
import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; public class Test extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } //内部类 class MyView extends SurfaceView implements SurfaceHolder.Callback{ SurfaceHolder holder; public MyView(Context context) { super(context); holder = this.getHolder();//获取holder holder.addCallback(this); //setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(new MyThread()).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } //内部类的内部类 class MyThread implements Runnable{ @Override public void run() { Canvas canvas = holder.lockCanvas(null);//获取画布 Paint mPaint = new Paint(); mPaint.setColor(Color.BLUE); canvas.drawRect(new RectF(40,60,80,80), mPaint); holder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像 } } } }
上面例子取自摘自http://www.iteye.com/topic/420410,只是一个简单的调用,但用来学习却是最容易理解的。下面是我写的一个例子,使用SurfaceView作一些简单的动画。动画是在窗口显示5*8个不断放大缩小的实心圆,图形如下:
代码如下:
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.drawable.BitmapDrawable; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder.Callback; public class MySurfaceView extends SurfaceView implements Runnable, Callback { private SurfaceHolder mHolder; // 用于控制SurfaceView private Thread t; // 声明一条线程 private volatile boolean flag; // 线程运行的标识,用于控制线程 private Canvas mCanvas; // 声明一张画布 private Paint p; // 声明一支画笔 float m_circle_r = 10; public MySurfaceView(Context context) { super(context); mHolder = getHolder(); // 获得SurfaceHolder对象 mHolder.addCallback(this); // 为SurfaceView添加状态监听 p = new Paint(); // 创建一个画笔对象 p.setColor(Color.WHITE); // 设置画笔的颜色为白色 setFocusable(true); // 设置焦点 } /** * 当SurfaceView创建的时候,调用此函数 */ @Override public void surfaceCreated(SurfaceHolder holder) { t = new Thread(this); // 创建一个线程对象 flag = true; // 把线程运行的标识设置成true t.start(); // 启动线程 } /** * 当SurfaceView的视图发生改变的时候,调用此函数 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * 当SurfaceView销毁的时候,调用此函数 */ @Override public void surfaceDestroyed(SurfaceHolder holder) { flag = false; // 把线程运行的标识设置成false mHolder.removeCallback(this); } /** * 当屏幕被触摸时调用 */ @Override public boolean onTouchEvent(MotionEvent event) { return true; } /** * 当用户按键时调用 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { surfaceDestroyed(mHolder); return super.onKeyDown(keyCode, event); } @Override public void run() { while (flag) { try { synchronized (mHolder) { Thread.sleep(100); // 让线程休息1000毫秒 Draw(); // 调用自定义画画方法 } } catch (InterruptedException e) { e.printStackTrace(); } finally { if (mCanvas != null) { // mHolder.unlockCanvasAndPost(mCanvas);//结束锁定画图,并提交改变。 } } } } /** * 自定义一个方法,在画布上画一个圆 */ protected void Draw() { mCanvas = mHolder.lockCanvas(); // 获得画布对象,开始对画布画画 if (mCanvas != null) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Style.FILL); if (m_circle_r >= (getWidth() / 10)) { m_circle_r = 0; } else { m_circle_r++; } Bitmap pic = ((BitmapDrawable) getResources().getDrawable( R.drawable.qq)).getBitmap(); mCanvas.drawBitmap(pic, 0, 0, paint); for (int i = 0; i < 5; i++) for (int j = 0; j < 8; j++) mCanvas.drawCircle( (getWidth() / 5) * i + (getWidth() / 10), (getHeight() / 8) * j + (getHeight() / 16), m_circle_r, paint); mHolder.unlockCanvasAndPost(mCanvas); // 完成画画,把画布显示在屏幕上 } } }
// setContentView(R.layout.main); // 隐藏状态栏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 把Activity的标题去掉 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置布局 this.setContentView(new MySurfaceView(this));
import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; public class TestDoubleActivity extends Activity { MyView mv; float m_circle_r = 10; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); mv = new MyView(this); setContentView(mv); Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTask(), 1, 100); } public class MyView extends View { MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Style.FILL); if (m_circle_r >= (getWidth()/10)) { m_circle_r = 0; } else { m_circle_r++; } Bitmap pic = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap(); canvas.drawBitmap(pic, 0, 0, paint); for (int i = 0; i < 5; i++) for (int j = 0; j < 8; j++) canvas.drawCircle((getWidth()/5)*i+(getWidth()/10), (getHeight()/8)*j+(getHeight()/16), m_circle_r, paint); } } private class MyTask extends TimerTask{ @Override public void run() { mv.postInvalidate(); } } }
package com.test; import android.app.Activity; import android.content.Context; import android.os.Bundle; import java.lang.Thread; import java.util.Random; import android.view.View; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; public class TestThreadActivity extends Activity { int c = Color.BLUE; MyThread myThread; volatile boolean bThreadRun = false; MyView mv; // 首先定义一个paint Paint paint = new Paint(); Path m_path = new Path(); volatile int ox = 0, oy = 0, nx = 100, ny = 200; final Random rand = new Random(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); mv = new MyView(this); setContentView(mv); } @Override protected void onRestoreInstanceState(android.os.Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } @Override protected void onSaveInstanceState(android.os.Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onStart() { super.onStart(); //myThread = new MyThread(); //myThread.start(); new Thread (new MyThread()).start(); bThreadRun = true; } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); bThreadRun = false; // myThread.stop(); } @Override protected void onStop() { super.onStop(); onPause(); } @Override protected void onDestroy() { super.onDestroy(); // myThread.destroy(); } public class MyView extends View { MyView(Context context) { super(context); m_path.moveTo(ox, oy); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // 绘制矩形区域-实心矩形 // 设置颜色 paint.setColor(c); paint.setStrokeWidth(10); // 设置样式-填充 paint.setStyle(Style.STROKE); // 绘制一个矩形 //canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint); nx = (int) (rand.nextFloat()*400); ny = (int) (rand.nextFloat()*800); m_path.lineTo(nx, ny); canvas.drawPath(m_path, paint); } } public class MyThread implements Runnable{ // 线程的主要工作方法 @Override public void run() { while (bThreadRun) { try { Thread.sleep(500); mv.postInvalidate(); } catch(InterruptedException e){ } } } } }
使用TIMER,THREAD和surfaceview都能实现相同的效果,有什么区别呢,两者实现起来尽管那么像,但却一点都不一样,TIMER,THREAD都是在主UI线程里绘图的,遇到复杂情况,容易因为阻塞延迟超过5S而被系统干掉,surfaceview在自己的线程中处理,可以避免这个问题。最后一个例子中虽然使用了多线程,但并不能把绘图操作放入线程中处理,多线程也仅仅起到了与TIMER一样的作用,所以我们在许多UI设计中,肯定是离不开surfaceview的
参考资料:
http://www.cnblogs.com/xuling/archive/2011/06/06/android.html
http://www.iteye.com/topic/420410