什么是TextureView
TextureView 是在 4.0(API level 14) 引入的,与 SurfaceView 相比,它不会创建新的窗口来显示内容。它是将内容流直接投放到 View 中,并且可以和其它普通 View 一样进行移动,旋转,缩放,动画等变化。TextureView 必须在硬件加速的窗口中使用。现在的移动设备基本都有GPU进行硬件加速渲染。
SurfaceTexture
在这篇文章中,我们已经知道了 Surface 是内存中一段绘图缓冲区
那么 SurfaceTexture 是什么呢?
SurfaceTexture 用来捕获视频流中的图像帧,视频流可以是相机预览或者是视频解码数据。
TextureView 可以通过 getSurfaceTexture() 方法来获取 TextureView 相应的 SurfaceTexture。
因此我们使用 TextureView 时,首先要获取到用于渲染内容的 SurfaceTexture 。具体做法是先创建 TextureView 对象,然后实现 SurfaceTextureListener 接口
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.main_layout);
myTexture = new TextureView(this);
myTexture.setSurfaceTextureListener(this);
}
Activity implements了SurfaceTextureListener接口因此activity中需要重写如下方法:
@Override
public void onSu