Android之SurfaceView
来源:互联网 发布:上海炫踪网络上市 编辑:程序博客网 时间:2024/05/15 05:49
SurfaceView也用过很多了,今天对Android中SurfaceView以及相关知识进行整理总结。
首先,来看一下Android API对SurfaceView的介绍:
Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen
The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.
The transparent region that makes the surface visible is based on the layout positions in the view hierarchy. If the post-layout transform properties are used to draw a sibling view on top of the SurfaceView, the view may not be properly composited with the surface.
Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by callinggetHolder()
.
The Surface will be created for you while the SurfaceView's window is visible; you should implementsurfaceCreated(SurfaceHolder)
andsurfaceDestroyed(SurfaceHolder)
to discover when the Surface is created and destroyed as the window is shown and hidden.
One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:
- All SurfaceView and
SurfaceHolder.Callback
methods will be called from the thread running the SurfaceView's window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread. - You must ensure that the drawing thread only touches the underlying Surface while it is valid -- between
SurfaceHolder.Callback.surfaceCreated()
andSurfaceHolder.Callback.surfaceDestroyed()
.
接下来,说一下自己的理解:
SurfaceView继承View,它的特性是:能够在子线程中刷新UI线程。
如何实现呢?
继承SurfaceView类,并且实现SurfaceHolder.Callback接口
需要重写的方法:
SurfaceView创建时回调
public void surfaceCreated(SurfaceHolder holder){}
Surface的大小发生改变时回调
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){}
销毁时回调
public void surfaceDestroyed(SurfaceHolder holder) {}
整个处理过程如下:
继承SurfaceView并实现SurfaceHolder.Callback接口--->SurfaceView.getHolder()获得SurfaceHolder对象--->
SurfaceHolder.addCallback(callback)添加回调函数--->SurfaceHolder.lockCanvas()获得Canvas对象并锁定画布
--->Canvas绘画--->SurfaceHolder.unlockCanvasAndPost(Canvas canvas)
这里涉及到一个类SurfaceHolder的,关于SurfaceHolder,
SurfaceHolder 是对 SurfaceView 的 Surface 的包装,不但在 SurfaceHolder.Callback 接口中负责 Surface 创建、大小改变和销毁的回调,而且还对 Surface 的关键方法 LockCanvas()、unLockCanvasAndPost() 方法进行了线程安全的包装,所以 SurfaceHolder 是 Surface 对象的持有者,负责 Surface 的生命周期中对 Surface 操作方法的调用。
SurfaceView、SurfaceHolder、Surface三者的关系如下图:
SurfaceView视图内嵌了一个专门用于绘制的Surface,其实每一个Surface都在每一个窗口的后面,我们可以通过SurfaceView类来控制哪些Surface的内容可以显示出来,其实它的作用就是可以直接从内存或者是DMA硬件里获取图像数据,将这些图像数据迅速地显示出来,通过开启一个渲染线程可以迅速地完成画面的更新。
另外,SurfaceView使用双缓冲机制,使SurfaceView同时对两张图片进行渲染操作,其实目的也是为了迅速更新图片的显示,第一个缓冲对这一帧进行解析,第二个会对下一帧进行解析,这样就可以避免在上一帧的图片显示完成后,下一帧的图片还没有进行显示的情况发生。
surface是纵深排序(Z-ordered)的,可以通过setZOrderOnTop(true)或者setZOrderMediaOverlay(true)来设置。
但是,必须在surface进行渲染前调用,在渲染的过程中调用无效。
我们再来看一下GLSurfaceView、
- android 之surfaceView 学习
- Android开发之SurfaceView
- Android布局之SurfaceView
- Android开发之SurfaceView
- Android开发之SurfaceView
- Android开发之SurfaceView
- Android开发之SurfaceView
- Android开发之SurfaceView
- Android开发之SurfaceView
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView
- Android 开发之SurfaceView
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView学习
- Compile caffe while another caffe exists already
- 机房收费系统——上下机
- weblogic下 java.lang.NoSuchMethodError异常
- Roman 字符
- TextView跑马灯效果
- Android之SurfaceView
- 关于腾讯应用管理中心,认领应用
- myBatis主键自增长sql
- 网站数据分析:如何追踪访客初始来源
- device wakeup 功能 & wake_lock电源锁
- Android-java.lang.RuntimeException: Package manager has died问题
- pybindgen-0.17.0.886.tar包共享
- 实现一个函数,将字符串中的空格替换成"%20"
- Spring 3.x 企业开发实战Chapter2 Part1