Android_UI:TextureView

简介

TextureView是用来显示camera中的数据,只可以在“开启硬件加速”的设备中运行。

这里写图片描述

相关方法

方法说明
setSurfaceTextureListener(listener)监听,需重写4个方法
setAlpha(0.5f)透明度
setRotation(70.0f)旋转角度

setSurfaceTextureListener(listener)中需要实现接口SurfaceTextureListener ,会重写4个方法

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        //可用状态,开启camera等操作
    }

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

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        //销毁Camera
        return true;//默认false
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

Demo

Camera类: android.hardware.Camera

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

    private TextureView textureView;
    private Camera camera;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textureView = (TextureView) findViewById(R.id.textureView);

        textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        //android.hardware.Camera 已过时
        camera = Camera.open();
        try {
            camera.setPreviewTexture(surface);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        textureView.setAlpha(0.5f);
//        textureView.setRotation(70.0f);//旋转
    }

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

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        camera.stopPreview();//顺序不可乱
        camera.release();
        return true;//默认false
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}

其它

Dmeo: http://git.oschina.net/AndroidUI/textureview01

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值