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).
相机预览Demo
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:
publicclassLiveCameraActivityextendsActivityimplementsTextureView.SurfaceTextureListener{private Camera mCamera;private TextureView mTextureView;protectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
mTextureView =newTextureView(this);
mTextureView.setSurfaceTextureListener(this);setContentView(mTextureView);}publicvoidonSurfaceTextureAvailable(SurfaceTexture surface,int width,int height){
mCamera = Camera.open();try{
mCamera.setPreviewTexture(surface);
mCamera.startPreview();}catch(IOException ioe){// Something bad happened}}publicvoidonSurfaceTextureSizeChanged(SurfaceTexture surface,int width,int height){// Ignored, Camera does all the work for us}publicbooleanonSurfaceTextureDestroyed(SurfaceTexture surface){
mCamera.stopPreview();
mCamera.release();returntrue;}publicvoidonSurfaceTextureUpdated(SurfaceTexture surface){// Invoked every time there's a new Camera preview frame}}