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来用就可以了

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值