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

PreviewView

open class PreviewView : FrameLayout
kotlin.Any
   ↳ 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 android.view.TextureView or android.view.SurfaceView to display the camera feed.

Summary

Nested classes

The implementation mode of a PreviewView

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

Public constructors

<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Public methods

open MeteringPointFactory
createMeteringPointFactory(@NonNull cameraSelector: CameraSelector)

Creates a MeteringPointFactory by a given CameraSelector

open Preview.SurfaceProvider
createSurfaceProvider(@Nullable cameraInfo: CameraInfo?)

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

open PreviewView.ImplementationMode

Returns the preferred ImplementationMode for preview.

open PreviewView.ScaleType

Returns the ScaleType currently applied to the preview.

open Unit

Specifies the preferred ImplementationMode to use for preview.

open Unit
setScaleType(@NonNull scaleType: PreviewView.ScaleType)

Applies a ScaleType to the preview.

Protected methods

open Unit

open Unit

Public constructors

<init>

PreviewView(@NonNull context: Context)

<init>

PreviewView(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?)

<init>

PreviewView(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?,
    defStyleAttr: Int)

<init>

PreviewView(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

createMeteringPointFactory

@NonNull open fun createMeteringPointFactory(@NonNull cameraSelector: CameraSelector): MeteringPointFactory

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.
Return
MeteringPointFactory a MeteringPointFactory

createSurfaceProvider

@NonNull @UiThread open fun createSurfaceProvider(@Nullable cameraInfo: CameraInfo?): Preview.SurfaceProvider

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

The returned Preview.SurfaceProvider will provide a preview android.view.Surface to the camera that's either managed by a android.view.TextureView or android.view.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 android.view.Surface provided by the returned Preview.SurfaceProvider.
Return
Preview.SurfaceProvider A Preview.SurfaceProvider used to start the camera preview.

getPreferredImplementationMode

@NonNull open fun getPreferredImplementationMode(): PreviewView.ImplementationMode

Returns the preferred ImplementationMode for preview.

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

Return
PreviewView.ImplementationMode The preferred ImplementationMode for preview.

getScaleType

@NonNull open fun getScaleType(): PreviewView.ScaleType

Returns the ScaleType currently applied to the preview.

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

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

setPreferredImplementationMode

open fun setPreferredImplementationMode(@NonNull preferredMode: PreviewView.ImplementationMode): Unit

Specifies the preferred ImplementationMode to use for preview.

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

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

setScaleType

open fun setScaleType(@NonNull scaleType: PreviewView.ScaleType): Unit

Applies a ScaleType to the preview.

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

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

Protected methods

onAttachedToWindow

protected open fun onAttachedToWindow(): Unit

onDetachedFromWindow

protected open fun onDetachedFromWindow(): Unit