Search code examples
androidandroid-jetpackandroid-camerax

CameraX cameraView and previewView


What is different between camerax cameraView and previewView? because i can't find resources or articles about comparing 2 view for previewing camerax


Solution

  • PreviewView is a custom View you can use to display a camera preview. You can add it to your XML layout, then in your Activity/Fragment, hook it up with a Preview use case to get the preview stream started. So while using PreviewView, you're responsible for setting up the camera, creating the Preview use case (and other use cases you may want to use), and binding them to a lifecycle. You can read more about PreviewView and how it works in this article.

    CameraView is a custom View that does many things, it was built to allow users to quickly set up a "camera app" by adding the View in their XML layout. It allows to display a camera preview, take pictures, record videos, zoom in/out on the viewfinder, and provides different APIs to configure the camera (lens facing, flash, scale type, capture mode, etc). It handles setting up the camera, binding and unbinding the use cases and what not. An important thing to note is that for displaying a preview, it internally uses PreviewView. I haven't come across many articles online on CameraView, I'd recommend this one though.

    If you need flexibility in setting up the camera and building/configuring the use cases, or if you need to use the ImageAnalysis use case, then CameraView is probably not the right choice to go with. PreviewView was built to offload the burden of setting up and handling the preview surface used by the camera, and handling the hard bits of displaying a preview (scaling, rotating, aspect ratio, etc).