2

I followed the guides on cameraX which all use PreviewView for displaying the camera output using preview.setSurfaceProvider(PreviewView.createSurfaceProvider());

How do I set the preview to use a textureview instead of a PreviewView

  • 1
    May I ask why do you need to use TextureView? PreviewView uses TextureView internally. Displaying the preview correctly is no trivial matter. If you wish to DIY, you can take a look at PreviewView's source code on how it's handled. https://github.com/androidx/androidx/blob/androidx-main/camera/camera-view/src/main/java/androidx/camera/view/PreviewView.java – Xi 张熹 Mar 19 '21 at 16:53

1 Answers1

3

You can specify which view to use in PreviewView between SurfaceView and TextureView. Before setting surfaceProvider, set the implementation mode

previewView.implementationMode = PreviewView.ImplementationMode.COMPATIBLE

Here previewView is the XML id of PreviewView. PreviewView.ImplementationMode.COMPATIBLE is for TextureView. After setting this, the PreviewView will use TextureView to show Camera.

The implementation mode for SurfaceView is PreviewView.ImplementationMode.PERFORMANCE

This is compatible with

androidx.camera:camera-view:1.0.0-alpha22
Shohel Rana
  • 2,332
  • 19
  • 24