The second Android 11 Developer Preview is now available, test it out and share your feedback.

PreviewView

public class PreviewView
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ androidx.camera.view.PreviewView


Custom View that displays the camera feed for CameraX's Preview use case.

This class manages the Surface lifecycle, as well as the preview aspect ratio and orientation. Internally, it uses either a TextureView or SurfaceView to display the camera feed.

Summary

Nested classes

enum PreviewView.ImplementationMode

The implementation mode of a PreviewView

Specifies how the Preview surface will be implemented internally: Using a SurfaceView or a TextureView (which is the default)

 

enum PreviewView.ScaleType

Options for scaling the preview vis-à-vis its container PreviewView

Inherited constants

Inherited fields

Public constructors

PreviewView(Context context)
PreviewView(Context context, AttributeSet attrs)
PreviewView(Context context, AttributeSet attrs, int defStyleAttr)
PreviewView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

MeteringPointFactory createMeteringPointFactory(CameraSelector cameraSelector)

Creates a MeteringPointFactory by a given CameraSelector

This MeteringPointFactory is capable of creating a MeteringPoint by a (x, y) in the PreviewView.

Preview.SurfaceProvider createSurfaceProvider(CameraInfo cameraInfo)

Gets the Preview.SurfaceProvider to be used with Preview.setSurfaceProvider(Executor, Preview.SurfaceProvider).

PreviewView.ImplementationMode getPreferredImplementationMode()

Returns the preferred PreviewView.ImplementationMode for preview.

PreviewView.ScaleType getScaleType()

Returns the PreviewView.ScaleType currently applied to the preview.

void setPreferredImplementationMode(PreviewView.ImplementationMode preferredMode)

Specifies the preferred PreviewView.ImplementationMode to use for preview.

void setScaleType(PreviewView.ScaleType scaleType)

Applies a PreviewView.ScaleType to the preview.

Protected methods

void onAttachedToWindow()
void onDetachedFromWindow()

Inherited methods

Public constructors

PreviewView

public PreviewView (Context context)

Parameters
context Context

PreviewView

public PreviewView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

PreviewView

public PreviewView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

PreviewView

public PreviewView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

createMeteringPointFactory

public MeteringPointFactory createMeteringPointFactory (CameraSelector cameraSelector)

Creates a MeteringPointFactory by a given CameraSelector

This MeteringPointFactory is capable of creating a MeteringPoint by a (x, y) in the PreviewView. It converts the points by current scaleType.

Parameters
cameraSelector CameraSelector: the CameraSelector which the Preview is bound to.

Returns
MeteringPointFactory a MeteringPointFactory

createSurfaceProvider

public Preview.SurfaceProvider createSurfaceProvider (CameraInfo cameraInfo)

Gets the Preview.SurfaceProvider to be used with Preview.setSurfaceProvider(Executor, Preview.SurfaceProvider).

The returned Preview.SurfaceProvider will provide a preview Surface to the camera that's either managed by a TextureView or SurfaceView. This option is determined by the preferred implementation mode and the device's capabilities.

Parameters
cameraInfo CameraInfo: The CameraInfo of the camera that will use the Surface provided by the returned Preview.SurfaceProvider.

Returns
Preview.SurfaceProvider A Preview.SurfaceProvider used to start the camera preview.

getPreferredImplementationMode

public PreviewView.ImplementationMode getPreferredImplementationMode ()

Returns the preferred PreviewView.ImplementationMode for preview.

If the preferred PreviewView.ImplementationMode hasn't been set using setPreferredImplementationMode(ImplementationMode), it defaults to PreviewView.ImplementationMode.SURFACE_VIEW.

Returns
PreviewView.ImplementationMode The preferred PreviewView.ImplementationMode for preview.

getScaleType

public PreviewView.ScaleType getScaleType ()

Returns the PreviewView.ScaleType currently applied to the preview.

By default, PreviewView.ScaleType.FILL_CENTER is applied to the preview.

Returns
PreviewView.ScaleType The PreviewView.ScaleType currently applied to the preview.

setPreferredImplementationMode

public void setPreferredImplementationMode (PreviewView.ImplementationMode preferredMode)

Specifies the preferred PreviewView.ImplementationMode to use for preview.

When the preferred PreviewView.ImplementationMode is PreviewView.ImplementationMode.SURFACE_VIEW but the device doesn't support this mode (e.g. devices with a supported camera hardware level CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY), the actual implementation mode will be PreviewView.ImplementationMode.TEXTURE_VIEW.

Parameters
preferredMode PreviewView.ImplementationMode: SURFACE_VIEW if a SurfaceView should be used to display the camera feed -when possible-, or TEXTURE_VIEW to use a TextureView.

setScaleType

public void setScaleType (PreviewView.ScaleType scaleType)

Applies a PreviewView.ScaleType to the preview.

Note that the PreviewView.ScaleType.FILL_CENTER is applied to the preview by default.

Parameters
scaleType PreviewView.ScaleType: A PreviewView.ScaleType to apply to the preview.

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onDetachedFromWindow

protected void onDetachedFromWindow ()