Added in API level 1

SurfaceView


open class SurfaceView : View
kotlin.Any
   ↳ android.view.View
   ↳ android.view.SurfaceView

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen

The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.

The transparent region that makes the surface visible is based on the layout positions in the view hierarchy. If the post-layout transform properties are used to draw a sibling view on top of the SurfaceView, the view may not be properly composited with the surface.

Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder.

The Surface will be created for you while the SurfaceView's window is visible; you should implement SurfaceHolder.Callback.surfaceCreated and SurfaceHolder.Callback.surfaceDestroyed to discover when the Surface is created and destroyed as the window is shown and hidden.

One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:

Note: Starting in platform version android.os.Build.VERSION_CODES#N, SurfaceView's window position is updated synchronously with other View rendering. This means that translating and scaling a SurfaceView on screen will not cause rendering artifacts. Such artifacts may occur on previous versions of the platform when its window is positioned asynchronously.

Note: Starting in platform version android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, SurfaceView will support arbitrary alpha blending. Prior platform versions ignored alpha values on the SurfaceView if they were between 0 and 1. If the SurfaceView has a composition order greater than or equal to 0, (see setCompositionOrder(int)), then the alpha is applied directly to the Surface. If the SurfaceView has a composition order less than 0, then the alpha is applied to the hole punch directly. Note that when the composition order is below the window overlapping SurfaceViews may not blend properly as a consequence of not applying alpha to the surface content directly.

Summary

Inherited XML attributes
Constants
static Int

Default lifecycle of the Surface owned by this SurfaceView.

static Int

The Surface lifecycle is tied to SurfaceView attachment.

static Int

The Surface lifecycle is tied to SurfaceView visibility.

Inherited constants
Public constructors
SurfaceView(context: Context!)

SurfaceView(context: Context!, attrs: AttributeSet!)

SurfaceView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

SurfaceView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Unit

Adds a transaction that would be applied synchronously with displaying the SurfaceView's next frame.

open Unit

Clears the SurfaceControlViewHost.SurfacePackage that was set on this SurfaceView.

open Unit
draw(canvas: Canvas)

Manually render this view (and all of its children) to the given Canvas.

open Boolean

This is used by the ViewRoot to perform an optimization when the view hierarchy contains one or several SurfaceView.

open CharSequence!

Return the class name of this object to be used for accessibility purposes.

open SurfaceControlViewHost.SurfacePackage?

Returns the SurfaceControlViewHost.SurfacePackage that was set on this SurfaceView.

open Int

Returns the composition order of the SurfaceView.

open SurfaceHolder!

Return the SurfaceHolder providing access and control over this SurfaceView's underlying surface.

open IBinder?

A token used for constructing SurfaceControlViewHost.

open Int

Gets the mode for determining whether this View is important for accessibility.

open SurfaceControl!

Return a SurfaceControl which can be used for parenting Surfaces to this SurfaceView.

open Boolean

Returns whether this View has content which overlaps.

open Unit
setAlpha(alpha: Float)

If the SurfaceView has a composition order greater than or equal to 0, (see setCompositionOrder(int)), the alpha value set is applied directly to the Surface's content.

open Unit

Displays the view-hierarchy embedded within a SurfaceControlViewHost.SurfacePackage within this SurfaceView.

open Unit
setClipBounds(clipBounds: Rect!)

Sets a rectangular area on this view to which the view will be clipped when it is drawn.

open Unit
setCompositionOrder(compositionOrder: Int)

Controls the composition order of the SurfaceView.

open Unit
setDesiredHdrHeadroom(desiredHeadroom: Float)

Sets the desired amount of HDR headroom to be used when HDR content is presented on this SurfaceView.

open Unit
setSecure(isSecure: Boolean)

Control whether the surface view's content should be treated as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays.

open Unit
setSurfaceLifecycle(lifecycleStrategy: Int)

Controls the lifecycle of the Surface owned by this SurfaceView.

open Unit
setVisibility(visibility: Int)

Set the visibility state of this view.

open Unit
setZOrderMediaOverlay(isMediaOverlay: Boolean)

Control whether the surface view's surface is placed on top of another regular surface view in the window (but still behind the window itself).

open Unit

Control whether the surface view's surface is placed on top of its window.

Protected methods
open Unit

Called by draw to draw the child views.

open Unit

This is called when the view is attached to a window.

open Unit

This is called when the view is detached from a window.

open Unit
onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?)

Called by the view system when the focus state of this view changes.

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Measure the view and its content to determine the measured width and the measured height.

open Boolean
onSetAlpha(alpha: Int)

Invoked if there is a Transform that involves alpha.

open Unit

Called when the window containing has change its visibility (between GONE, INVISIBLE, and VISIBLE).

Inherited functions