public final class Preview
extends UseCase


A use case that provides a camera preview stream for displaying on-screen.

The preview stream is connected to the Surface provided via Preview.SurfaceProvider. The application decides how the Surface is shown, and is responsible for managing the Surface lifecycle after providing it.

To display the preview with the correct orientation, app needs to take different actions based on the source of the Surface. If the Surface is backed by a SurfaceView, it will always be in the device's display orientation. If the Surface is backed by ImageReader, MediaCodec or other objects, it's the application's responsibility to calculate the rotation. If the Surface is backed by a SurfaceTexture, SurfaceTexture.getTransformMatrix(float[]) can be used to transform the preview to natural orientation. The value is available after a frame is pushed to the SurfaceTexture and its SurfaceTexture.OnFrameAvailableListener.onFrameAvailable(SurfaceTexture) has been called. TextureView handles this automatically and always puts the preview in the natural orientation. To further transform the TextureView to display orientation, the app needs to apply the current display rotation. Example:

         switch (getWindowManager().getDefaultDisplay().getRotation()) {
             case Surface.ROTATION_0:
                 displayRotation = 0;
             case Surface.ROTATION_90:
                 displayRotation = 90;
             case Surface.ROTATION_180:
                 displayRotation = 180;
             case Surface.ROTATION_270:
                 displayRotation = 270;
                 throw new UnsupportedOperationException(
                         "Unsupported display rotation: " + displayRotation);
         matrix.postRotate(-displayRotation, centerX, centerY);


Nested classes

class Preview.Builder

Builder for a Preview

interface Preview.SurfaceProvider

A interface implemented by the application to provide a Surface for Preview

Public methods

ResolutionInfo getResolutionInfo()

Gets selected resolution information of the Preview.

int getTargetRotation()

Returns the rotation that the intended target resolution is expressed in.

void setSurfaceProvider(Executor executor, Preview.SurfaceProvider surfaceProvider)

Sets a Preview.SurfaceProvider to provide a Surface for Preview.