Android 12 Developer Preview is here! Try it out, and give us your feedback!

PreviewView

public final 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 preview Surface's lifecycle. It internally uses either a TextureView or SurfaceView to display the camera feed, and applies required transformations on them to correctly display the preview, this involves correcting their aspect ratio, scale and rotation.

If PreviewView uses a SurfaceView to display the preview stream, be careful when overlapping a View that's initially not visible (either View.INVISIBLE or View.GONE) on top of it. When the SurfaceView is attached to the display window, it calls ViewParent.requestTransparentRegion(View) which requests a computation of the transparent regions on the display. At this point, the View isn't visible, causing the overlapped region between the SurfaceView and the View to be considered transparent. Later if the View becomes visible, it will not be displayed on top of SurfaceView. A way around this is to call ViewParent.requestTransparentRegion(View) right after making the View visible, or initially hiding the View by setting its opacity to 0, then setting it to 1.0F to show it.

Summary

Nested classes

enum PreviewView.ImplementationMode

The implementation mode of a PreviewView

enum PreviewView.ScaleType

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

enum PreviewView.StreamState

Definitions for the preview stream state. 

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

Bitmap getBitmap()

Returns a Bitmap representation of the content displayed on the PreviewView, or null if the camera preview hasn't started yet.

CameraController getController()

Get the CameraController.

PreviewView.ImplementationMode getImplementationMode()

Returns the PreviewView.ImplementationMode.

MeteringPointFactory getMeteringPointFactory()

Gets the MeteringPointFactory for the camera currently connected to the PreviewView, if any.

LiveData<PreviewView.StreamState> getPreviewStreamState()

Gets a LiveData for the preview PreviewView.StreamState.

PreviewView.ScaleType getScaleType()

Returns the PreviewView.ScaleType currently applied to the preview.

Preview.SurfaceProvider getSurfaceProvider()

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

ViewPort getViewPort()

Gets a ViewPort based on the current status of PreviewView.

ViewPort getViewPort(int targetRotation)

Gets a ViewPort with custom target rotation.

boolean onTouchEvent(MotionEvent event)
boolean performClick()
void setController(CameraController cameraController)

Sets the CameraController.

void setImplementationMode(PreviewView.ImplementationMode implementationMode)

Sets the PreviewView.ImplementationMode for the PreviewView.

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

getBitmap

public Bitmap getBitmap ()

Returns a Bitmap representation of the content displayed on the PreviewView, or null if the camera preview hasn't started yet.

The returned Bitmap uses the Bitmap.Config.ARGB_8888 pixel format and its dimensions are the same as this view's.

Do not invoke this method from a drawing method (View.onDraw(Canvas) for instance).

If an error occurs during the copy, an empty Bitmap will be returned.

If the preview hasn't started yet, the method may return null or an empty Bitmap. Use getPreviewStreamState() to get the PreviewView.StreamState and wait for PreviewView.StreamState.STREAMING to make sure the preview is started.

Returns
Bitmap A Bitmap.Config.ARGB_8888 Bitmap representing the content displayed on the PreviewView, or null if the camera preview hasn't started yet.

getController

public CameraController getController ()

Get the CameraController.

Returns
CameraController

getImplementationMode