Android APIs
public class

TextureView

extends View
java.lang.Object
   ↳ android.view.View
     ↳ android.view.TextureView

Class Overview

A TextureView can be used to display a content stream. Such a content stream can for instance be a video or an OpenGL scene. The content stream can come from the application's process as well as a remote process.

TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing.

Unlike SurfaceView, TextureView does not create a separate window but behaves as a regular View. This key difference allows a TextureView to be moved, transformed, animated, etc. For instance, you can make a TextureView semi-translucent by calling myView.setAlpha(0.5f).

Using a TextureView is simple: all you need to do is get its SurfaceTexture. The SurfaceTexture can then be used to render content. The following example demonstrates how to render the camera preview into a TextureView:

  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
      private Camera mCamera;
      private TextureView mTextureView;

      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          mTextureView = new TextureView(this);
          mTextureView.setSurfaceTextureListener(this);

          setContentView(mTextureView);
      }

      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
          mCamera = Camera.open();

          try {
              mCamera.setPreviewTexture(surface);
              mCamera.startPreview();
          } catch (IOException ioe) {
              // Something bad happened
          }
      }

      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
          // Ignored, Camera does all the work for us
      }

      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
          mCamera.stopPreview();
          mCamera.release();
          return true;
      }

      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
          // Invoked every time there's a new Camera preview frame
      }
  }
 

A TextureView's SurfaceTexture can be obtained either by invoking getSurfaceTexture() or by using a TextureView.SurfaceTextureListener. It is important to know that a SurfaceTexture is available only after the TextureView is attached to a window (and onAttachedToWindow() has been invoked.) It is therefore highly recommended you use a listener to be notified when the SurfaceTexture becomes available.

It is important to note that only one producer can use the TextureView. For instance, if you use a TextureView to display the camera preview, you cannot use lockCanvas() to draw onto the TextureView at the same time.

Summary

Nested Classes
interface TextureView.SurfaceTextureListener This listener can be used to be notified when the surface texture associated with this texture view is available. 
[Expand]
Inherited XML Attributes
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
TextureView(Context context)
Creates a new TextureView.
TextureView(Context context, AttributeSet attrs)
Creates a new TextureView.
TextureView(Context context, AttributeSet attrs, int defStyleAttr)
Creates a new TextureView.
TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Creates a new TextureView.
Public Methods
void buildLayer()
Calling this method has no effect.
final void draw(Canvas canvas)
Subclasses of TextureView cannot do their own rendering with the Canvas object.
Bitmap getBitmap(int width, int height)

Returns a Bitmap representation of the content of the associated surface texture.

Bitmap getBitmap()

Returns a Bitmap representation of the content of the associated surface texture.

Bitmap getBitmap(Bitmap bitmap)

Copies the content of this view's surface texture into the specified bitmap.

int getLayerType()
Always returns LAYER_TYPE_HARDWARE.
SurfaceTexture getSurfaceTexture()
Returns the SurfaceTexture used by this view.
TextureView.SurfaceTextureListener getSurfaceTextureListener()
Returns the TextureView.SurfaceTextureListener currently associated with this texture view.
Matrix getTransform(Matrix transform)
Returns the transform associated with this texture view.
boolean isAvailable()
Returns true if the SurfaceTexture associated with this TextureView is available for rendering.
boolean isOpaque()
Indicates whether this View is opaque.
Canvas lockCanvas(Rect dirty)
Just like lockCanvas() but allows specification of a dirty rectangle.
Canvas lockCanvas()

Start editing the pixels in the surface.

void setLayerPaint(Paint paint)
Updates the Paint object used with the current layer (used only if the current layer type is not set to LAYER_TYPE_NONE).
void setLayerType(int layerType, Paint paint)
The layer type of a TextureView is ignored since a TextureView is always considered to act as a hardware layer.
void setOpaque(boolean opaque)
Indicates whether the content of this TextureView is opaque.
void setSurfaceTexture(SurfaceTexture surfaceTexture)
Set the SurfaceTexture for this view to use.
void setSurfaceTextureListener(TextureView.SurfaceTextureListener listener)
Sets the TextureView.SurfaceTextureListener used to listen to surface texture events.
void setTransform(Matrix transform)

Sets the transform to associate with this texture view.

void unlockCanvasAndPost(Canvas canvas)
Finish editing pixels in the surface.
Protected Methods
void onAttachedToWindow()
This is called when the view is attached to a window.
final void onDraw(Canvas canvas)
Subclasses of TextureView cannot do their own rendering with the Canvas object.
void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
void onVisibilityChanged(View changedView, int visibility)
Called when the visibility of the view or an ancestor of the view has changed.
[Expand]
Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public Constructors

public TextureView (Context context)

Added in API level 14

Creates a new TextureView.

Parameters
context Context: The context to associate this view with.

public TextureView (Context context, AttributeSet attrs)

Added in API level 14

Creates a new TextureView.

Parameters
context Context: The context to associate this view with.
attrs AttributeSet: The attributes of the XML tag that is inflating the view.

public TextureView (Context context, AttributeSet attrs, int defStyleAttr)

Added in API level 14

Creates a new TextureView.

Parameters
context Context: The context to associate this view with.
attrs AttributeSet: The attributes of the XML tag that is inflating the view.
defStyleAttr int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

public TextureView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

Creates a new TextureView.

Parameters
context Context: The context to associate this view with.
attrs AttributeSet: The attributes of the XML tag that is inflating the view.
defStyleAttr int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
defStyleRes int: A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

Public Methods

public void buildLayer ()

Added in API level 14

Calling this method has no effect.

public final void draw (Canvas canvas)

Added in API level 14

Subclasses of TextureView cannot do their own rendering with the Canvas object.

Parameters
canvas Canvas: The Canvas to which the View is rendered.

public Bitmap getBitmap (int width, int height)

Added in API level 14

Returns a Bitmap representation of the content of the associated surface texture. If the surface texture is not available, this method returns null.

The bitmap returned by this method uses the ARGB_8888 pixel format.

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

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

Parameters
width int: The width of the bitmap to create
height int: The height of the bitmap to create
Returns
Bitmap A valid ARGB_8888 bitmap, or null if the surface texture is not available or width is <= 0 or height is <= 0

public Bitmap getBitmap ()

Added in API level 14

Returns a Bitmap representation of the content of the associated surface texture. If the surface texture is not available, this method returns null.

The bitmap returned by this method uses the ARGB_8888 pixel format and its dimensions are the same as this view's.

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

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

Returns
Bitmap A valid ARGB_8888 bitmap, or null if the surface texture is not available or the width <= 0 or the height <= 0

public Bitmap getBitmap (Bitmap bitmap)

Added in API level 14

Copies the content of this view's surface texture into the specified bitmap. If the surface texture is not available, the copy is not executed. The content of the surface texture will be scaled to fit exactly inside the specified bitmap.

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

If an error occurs, the bitmap is left unchanged.

Parameters
bitmap Bitmap: The bitmap to copy the content of the surface texture into, cannot be null, all configurations are supported
Returns
Bitmap The bitmap specified as a parameter
Throws
IllegalStateException if the hardware rendering context cannot be acquired to capture the bitmap

public int getLayerType ()

Added in API level 14

public SurfaceTexture getSurfaceTexture ()

Added in API level 14

Returns the SurfaceTexture used by this view. This method may return null if the view is not attached to a window or if the surface texture has not been initialized yet.

Returns
SurfaceTexture

See also:

public Matrix getTransform (Matrix transform)

Added in API level 14

Returns the transform associated with this texture view.

Parameters
transform Matrix: The Matrix in which to copy the current transform. Can be null.
Returns
Matrix The specified matrix if not null or a new Matrix instance otherwise.

public boolean isAvailable ()

Added in API level 14

Returns true if the SurfaceTexture associated with this TextureView is available for rendering. When this method returns true, getSurfaceTexture() returns a valid surface texture.

Returns
boolean

public boolean isOpaque ()

Added in API level 14

Indicates whether this View is opaque. An opaque View guarantees that it will draw all the pixels overlapping its bounds using a fully opaque color. Subclasses of View should override this method whenever possible to indicate whether an instance is opaque. Opaque Views are treated in a special way by the View hierarchy, possibly allowing it to perform optimizations during invalidate/draw passes.

Returns
boolean True if this View is guaranteed to be fully opaque, false otherwise.

public Canvas lockCanvas (Rect dirty)

Added in API level 14

Just like lockCanvas() but allows specification of a dirty rectangle. Every pixel within that rectangle must be written; however pixels outside the dirty rectangle will be preserved by the next call to lockCanvas(). This method can return null if the underlying surface texture is not available (see isAvailable() or if the surface texture is already connected to an image producer (for instance: the camera, OpenGL, a media player, etc.)

Parameters
dirty Rect: Area of the surface that will be modified.
Returns
Canvas A Canvas used to draw into the surface.

public Canvas lockCanvas ()

Added in API level 14

Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise cannot be edited. You will usually need to implement onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int) to find out when the Surface is available for use.

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved.

This method can only be used if the underlying surface is not already owned by another producer. For instance, if the TextureView is being used to render the camera's preview you cannot invoke this method.

Returns
Canvas A Canvas used to draw into the surface.

public void setLayerPaint (Paint paint)

Added in API level 17

Updates the Paint object used with the current layer (used only if the current layer type is not set to LAYER_TYPE_NONE). Changed properties of the Paint provided to setLayerType(int, android.graphics.Paint) will be used the next time the View is redrawn, but setLayerPaint(android.graphics.Paint) must be called to ensure that the view gets redrawn immediately.

A layer is associated with an optional Paint instance that controls how the layer is composed on screen. The following properties of the paint are taken into account when composing the layer:

If this view has an alpha value set to < 1.0 by calling setAlpha(float), the alpha value of the layer's paint is superseded by this view's alpha value.

Parameters
paint Paint: The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE

public void setLayerType (int layerType, Paint paint)

Added in API level 14

The layer type of a TextureView is ignored since a TextureView is always considered to act as a hardware layer. The optional paint supplied to this method will however be taken into account when rendering the content of this TextureView.

Parameters
layerType int: The ype of layer to use with this view, must be one of LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
paint Paint: The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE

public void setOpaque (boolean opaque)

Added in API level 14

Indicates whether the content of this TextureView is opaque. The content is assumed to be opaque by default.

Parameters
opaque boolean: True if the content of this TextureView is opaque, false otherwise

public void setSurfaceTexture (SurfaceTexture surfaceTexture)

Added in API level 16

Set the SurfaceTexture for this view to use. If a SurfaceTexture is already being used by this view, it is immediately released and not be usable any more. The onSurfaceTextureDestroyed(SurfaceTexture) callback is not called for the previous SurfaceTexture. Similarly, the onSurfaceTextureAvailable(SurfaceTexture, int, int) callback is not called for the SurfaceTexture passed to setSurfaceTexture. The SurfaceTexture object must be detached from all OpenGL ES contexts prior to calling this method.

Parameters
surfaceTexture SurfaceTexture: The SurfaceTexture that the view should use.

public void setSurfaceTextureListener (TextureView.SurfaceTextureListener listener)

Added in API level 14

Sets the TextureView.SurfaceTextureListener used to listen to surface texture events.

Parameters
listener TextureView.SurfaceTextureListener

public void setTransform (Matrix transform)

Added in API level 14

Sets the transform to associate with this texture view. The specified transform applies to the underlying surface texture and does not affect the size or position of the view itself, only of its content.

Some transforms might prevent the content from drawing all the pixels contained within this view's bounds. In such situations, make sure this texture view is not marked opaque.

Parameters
transform Matrix: The transform to apply to the content of this view.

public void unlockCanvasAndPost (Canvas canvas)

Added in API level 14

Finish editing pixels in the surface. After this call, the surface's current pixels will be shown on the screen, but its content is lost, in particular there is no guarantee that the content of the Surface will remain unchanged when lockCanvas() is called again.

Parameters
canvas Canvas: The Canvas previously returned by lockCanvas()

Protected Methods

protected void onAttachedToWindow ()

Added in API level 14

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

protected final void onDraw (Canvas canvas)

Added in API level 14

Subclasses of TextureView cannot do their own rendering with the Canvas object.

Parameters
canvas Canvas: The Canvas to which the View is rendered.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

Added in API level 14

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w int: Current width of this view.
h int: Current height of this view.
oldw int: Old width of this view.
oldh int: Old height of this view.

protected void onVisibilityChanged (View changedView, int visibility)

Added in API level 14

Called when the visibility of the view or an ancestor of the view has changed.

Parameters
changedView View: The view whose visibility changed. May be this or an ancestor view.
visibility int: The new visibility, one of VISIBLE, INVISIBLE or GONE.