TextureView 简介 SurfaceView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TextureView 简介 SurfaceView相关的知识,希望对你有一定的参考价值。


官方文档

Class Overview

A TextureView can be used to display a content stream. Such a content stream can for instance be a video or an OpenGL scene场景. The content stream can come from the application‘s process as well as a remote远程 process.

TextureView can only be used in a hardware accelerated硬件加速 window. When rendered in呈现在 software, TextureView will draw nothing.

Unlike SurfaceView, TextureView does not create a separate单独的 window but behaves as a regular像平常的 View. This key difference allows a TextureView to be moved, transformed, animated动画, etc. For instance, you can make a TextureView semi-translucent半透明 by calling myView.setAlpha(0.5f).

Using a TextureView is simple: all you need to do is get its SurfaceTexture. The SurfaceTexture can then be used to render展示 content. The following example demonstrates how to render the camera preview into a TextureView:


A TextureView‘s SurfaceTexture can be obtained或的 either by invoking引用 getSurfaceTexture() or by using a TextureView.SurfaceTextureListener. It is important to know that a SurfaceTexture is available only after the TextureView is attached to a window (and onAttachedToWindow() has been invoked.) It is therefore highly recommended推荐 you use a listener to be notified when the SurfaceTexture becomes available.

It is important to note that only one producer can use the TextureView. For instance, if you use a TextureView to display the camera preview, you cannot uselockCanvas() to draw onto the TextureView at the same time.


简介
应用程序的视频或者opengl内容往往是显示在一个特别的UI控件中:SurfaceView。SurfaceView的工作方式是创建一个置于应用窗口之后的新窗口。这种方式的效率非常高,因为SurfaceView窗口刷新的时候不需要重绘应用程序的窗口(android普通窗口的视图绘制机制是一层一层的,任何一个子元素或者是局部的刷新都会导致整个视图结构全部重绘一次,因此效率非常低下,不过满足普通应用界面的需求还是绰绰有余),但是SurfaceView也有一些非常不便的限制。
因为SurfaceView的内容不在应用窗口上,所以不能使用变换(平移、缩放、旋转等)。也难以放在ListView或者ScrollView中,不能使用UI控件的一些特性比如View.setAlpha()。
为了解决这个问题 Android 4.0中引入了TextureView。
与SurfaceView相比,TextureView并没有创建一个单独的Surface用来绘制,这使得它可以像一般的View一样执行一些变换操作,设置透明度等。另外,Textureview必须在硬件加速开启的窗口中。
TextureView的使用非常简单,你唯一要做的就是获取用于渲染内容的SurfaceTexture。

demo
@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;//权限【android.permission.CAMERA】
    private TextureView mTextureView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
        mTextureView.setRotation(45.0f);
        mTextureView.setAlpha(0.5f);
        setContentView(mTextureView);
    }
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException ioe) {
        }
    }
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, Camera does all the work for us
    }
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Invoked every time there‘s a new Camera preview frame
    }
}





以上是关于TextureView 简介 SurfaceView的主要内容,如果未能解决你的问题,请参考以下文章

用TextureView播放网络视频

TextureView 的裁剪相机预览

textureView

在 TextureView 上播放视频

Android Camera2 拍照——使用TextureView

android中的VideoView和TextureView和Suraceview有啥区别?