Android APIs
public class

Surface

extends Object
implements Parcelable
java.lang.Object
   ↳ android.view.Surface

Class Overview

Handle onto a raw buffer that is being managed by the screen compositor.

Summary

Nested Classes
class Surface.OutOfResourcesException Exception thrown when a Canvas couldn't be locked with lockCanvas(Rect), or when a SurfaceTexture could not successfully be allocated. 
Constants
int ROTATION_0 Rotation constant: 0 degree rotation (natural orientation)
int ROTATION_180 Rotation constant: 180 degree rotation.
int ROTATION_270 Rotation constant: 270 degree rotation.
int ROTATION_90 Rotation constant: 90 degree rotation.
[Expand]
Inherited Constants
From interface android.os.Parcelable
Fields
public static final Creator<Surface> CREATOR
Public Constructors
Surface(SurfaceTexture surfaceTexture)
Create Surface from a SurfaceTexture.
Public Methods
int describeContents()
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
boolean isValid()
Returns true if this object holds a valid surface.
Canvas lockCanvas(Rect inOutDirty)
Gets a Canvas for drawing into this surface.
void readFromParcel(Parcel source)
void release()
Release the local reference to the server-side surface.
String toString()
Returns a string containing a concise, human-readable description of this object.
void unlockCanvas(Canvas canvas)
void unlockCanvasAndPost(Canvas canvas)
Posts the new contents of the Canvas to the surface and releases the Canvas.
void writeToParcel(Parcel dest, int flags)
Flatten this object in to a Parcel.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.os.Parcelable

Constants

public static final int ROTATION_0

Added in API level 1

Rotation constant: 0 degree rotation (natural orientation)

Constant Value: 0 (0x00000000)

public static final int ROTATION_180

Added in API level 1

Rotation constant: 180 degree rotation.

Constant Value: 2 (0x00000002)

public static final int ROTATION_270

Added in API level 1

Rotation constant: 270 degree rotation.

Constant Value: 3 (0x00000003)

public static final int ROTATION_90

Added in API level 1

Rotation constant: 90 degree rotation.

Constant Value: 1 (0x00000001)

Fields

public static final Creator<Surface> CREATOR

Added in API level 1

Public Constructors

public Surface (SurfaceTexture surfaceTexture)

Added in API level 14

Create Surface from a SurfaceTexture. Images drawn to the Surface will be made available to the SurfaceTexture, which can attach them to an OpenGL ES texture via updateTexImage().

Parameters
surfaceTexture The SurfaceTexture that is updated by this Surface.
Throws
Surface.OutOfResourcesException if the surface could not be created.

Public Methods

public int describeContents ()

Added in API level 1

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

Returns
  • a bitmask indicating the set of special object types marshalled by the Parcelable.

public boolean isValid ()

Added in API level 1

Returns true if this object holds a valid surface.

Returns
  • True if it holds a physical surface, so lockCanvas() will succeed. Otherwise returns false.

public Canvas lockCanvas (Rect inOutDirty)

Added in API level 1

Gets a Canvas for drawing into this surface. After drawing into the provided Canvas, the caller must invoke unlockCanvasAndPost(Canvas) to post the new contents to the surface.

Parameters
inOutDirty A rectangle that represents the dirty region that the caller wants to redraw. This function may choose to expand the dirty rectangle if for example the surface has been resized or if the previous contents of the surface were not available. The caller must redraw the entire dirty region as represented by the contents of the inOutDirty rectangle upon return from this function. The caller may also pass null instead, in the case where the entire surface should be redrawn.
Returns
  • A canvas for drawing into the surface.
Throws
IllegalArgumentException If the inOutDirty rectangle is not valid.
Surface.OutOfResourcesException If the canvas cannot be locked.

public void readFromParcel (Parcel source)

Added in API level 1

public void release ()

Added in API level 14

Release the local reference to the server-side surface. Always call release() when you're done with a Surface. This will make the surface invalid.

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public void unlockCanvas (Canvas canvas)

Added in API level 1

This method was deprecated in API level 17.
This API has been removed and is not supported. Do not use.

public void unlockCanvasAndPost (Canvas canvas)

Added in API level 1

Posts the new contents of the Canvas to the surface and releases the Canvas.

Parameters
canvas The canvas previously obtained from lockCanvas(Rect).

public void writeToParcel (Parcel dest, int flags)

Added in API level 1

Flatten this object in to a Parcel.

Parameters
dest The Parcel in which the object should be written.
flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

Protected Methods

protected void finalize ()

Added in API level 1

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable