TextureView 应用

简介

TextureView可以用于展示内容流. 例如像视频或者OpenGL场景的内容流. 内容流可以来自本应用程序以及其他进程

TextureView只能应用于开启硬件加速的窗口

不像SurfaceView那样,TextureView不会创建一个单独的窗口但是可以作为一个普通的View. 不同的是TextureView可以移动,平移,动画等. 例如,通过调用myView.setAlpha(0.5f)将TextureView设置成半透明

应用

这里简单贴出一段代码,改编自源码实例

public class CustomCamera extends TextureView implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    public CustomCamera(Context context) {
        super(context);
        init();
    }
    public CustomCamera(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public CustomCamera(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    void init() {
        mCamera = Camera.open();
        setSurfaceTextureListener(this);
    }
    public void takePic(Camera.ShutterCallback shutter, Camera.PictureCallback raw,                        Camera.PictureCallback postview, Camera.PictureCallback jpeg) {
        if (mCamera != null) {
            mCamera.takePicture(shutter, raw, postview, jpeg);
        }
    }
    public void takePic(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) {
        if (mCamera != null) {
            mCamera.takePicture(shutter, raw, jpeg);
        }
    }
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        if (mCamera == null) {
            mCamera = Camera.open();
        }
        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {    
    }
}

就直接当做一个自定义view来用就可以了

    原文作者:子墨_guo
    原文地址: https://www.jianshu.com/p/a33add0d38c4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞