简介
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) {
}
}