【SurfaceHolder.Callback】

SurfaceView 继承视图(View)类,每一个SurfaceView都内嵌了一个专门用于绘制的Surface,并控制Surface的位置。

Handleonto a raw buffer that is being managed by the screen compositor,原生缓冲器(raw buffer)用于保存当前窗口的像素数据

SurfaceHolder是显示一个surface的抽象接口。通常SurfaceView来实现此接口。通过调用SurfaceHolder可以调用SurfaceView,控制图形的尺寸和大小。而SurfaceHolder 是通过getholder()来取得。

SurfaceHolder.Callback接口实现后可接受接收surface变化的消息。创立SurfaceHolder 对象后,用SurfaceHolder.Callback()来回调SurfaceHolder,对SurfaceView进行控制。只在SurfaceHolder.Callback.surfaceCreated()SurfaceHolder.Callback.surfaceDestroyed()之间有效。

例如,应该实现surfaceCreated(SurfaceHolder)和 surfaceDestroyed(SurfaceHolder)方法来知道在这个Surface在窗口的显示和隐藏过程中是什么时候创建和销毁的。
class BBatt extends SurfaceView implements SurfaceHolder.Callback {
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){//看其名知其义,在surface的大小发生改变时激发
drawThread = new DrawThread(holder);
drawThread.start();}
public void surfaceCreated(SurfaceHolder holder){
//同上,在创建时激发,初始化绘制界面相关工作,一般在这里调用画图的线程。

new Thread(new MyThread()).start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
//同上,销毁时激发,一般在这里将画图的线程停止、释放。

if( drawThread != null ) {
drawThread.doStop();
while (true) try {
drawThread.join();
break ;
} catch(Exception ex) {}

}
}

E.G., 通过findViewById()方法取得一个cameraPreview的SurfaceView,然后再通过getHolder(),取得这个SurfaceView的SurfaceHolder。

SurfaceHolder holder = ((SurfaceView) findViewById(R.id.cameraPreview)).getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//SURFACE_TYPE_PUSH_BUFFERS说明该Surface不包涵原生数据,Surface用到的数据由其他对象准备
holder.addCallback(this); //用addcallback()来回调SurfaceView。因为该Acivity继承了SurfaceHolder.callback接口,重写了callback的三个函数,所以要用addcallback将这三个函数与Surface关联起来

  • SurfaceView可以在多线程中被访问,即可在多线程中画图。
  • 一个SurfaceView只在SurfaceHolder.Callback.surfaceCreated() 和 SurfaceHolder.Callback.surfaceDestroyed()调用之间是可用的,其他时间是得不到它的Canvas对象的 (null)
  • 在SurfaceHolder.Callback.surfaceCreated()调用过后,holder.lockCanvas()对象就可以得到SurfaceView对象对应的Canvas对象canvas了。用canvas对象画图,结束后调用holder.unlockCanvasAndPost()就把图画在窗口中了。
  • 得到这个SurfaceView的SurfaceHolder对象holder之后,holder.addCallback(callback),即实现SurfaceHolder.Callback接口的类对象
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: