Added in API level 14

TextureView


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

A TextureView can be used to display a content stream, such as that coming from a camera preview, 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.

TextureView vs. SurfaceView Capabilities

  TextureView SurfaceView
Supports View alpha X U+
Supports rotations X  
Supports clipping X  
HDR support Limited (on Android T+) Full
Renders DRM content   X

Unlike SurfaceView, TextureView does not create a separate window but behaves as a regular View. This key difference allows a TextureView to have translucency, arbitrary rotations, and complex clipping. For example, you can make a TextureView semi-translucent by calling myView.setAlpha(0.5f).

One implication of this integration of TextureView into the view hierarchy is that it may have slower performance than SurfaceView. TextureView contents must be copied, internally, from the underlying surface into the view displaying those contents. For that reason, SurfaceView is recommended as a more general solution to problems requiring rendering to surfaces.

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 a video into a TextureView:

public class MyActivity extends Activity implements TextureView.SurfaceTextureListener {
       private MediaPlayer mMediaPlayer;
       private TextureView mTextureView;
 
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
 
           mMediaPlayer = new MediaPlayer();
 
           mTextureView = new TextureView(this);
           mTextureView.setSurfaceTextureListener(this);
           setContentView(mTextureView);
       }
 
       public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture,
                                             int width, int height) {
           AssetFileDescriptor fileDescriptor = // get file descriptor
           mMediaPlayer.setDataSource(fileDescriptor);
           mMediaPlayer.setSurface(new Surface(surfaceTexture));
           mMediaPlayer.prepareAsync();
           mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
               @Override
               public void onPrepared(MediaPlayer mp) {
                   mMediaPlayer.start();
               }
           });
          } catch (IOException e) {
              e.printStackTrace();
          }
       }
 
      @Override
      public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture,
                                              int width, int height) {
          // Handle size change depending on media needs
      }
 
      @Override
      public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
          // Release unneeded resources
          mMediaPlayer.stop();
          mMediaPlayer.release();
          return true;
      }
 
      @Override
      public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
           // Invoked every time there's a new video frame
      }
 
   }
  

Similarly, TextureView can supply the surface needed for GL rendering or camera previews. Camera2 APIs require the surface created by TextureView, although developers are recommended to use the CameraX APIs instead, for which PreviewView creates its own TextureView or SurfaceView internally.

A TextureView's SurfaceTexture can be obtained either by invoking getSurfaceTexture() or by using a 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
abstract

This listener can be used to be notified when the surface texture associated with this texture view is available.

Inherited XML attributes
Inherited constants
Public constructors
TextureView(context: Context)

Creates a new TextureView.

TextureView(context: Context, attrs: AttributeSet?)

Creates a new TextureView.

TextureView(context: Context, attrs: AttributeSet?, defStyleAttr: Int)

Creates a new TextureView.

TextureView(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Creates a new TextureView.

Public methods
open Unit

Calling this method has no effect.

Unit
draw(canvas: Canvas)

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

open CharSequence!

open Bitmap?

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

open Bitmap
getBitmap(bitmap: Bitmap)

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

open Bitmap?
getBitmap(width: Int, height: Int)

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

open Int

Always returns LAYER_TYPE_HARDWARE.

open SurfaceTexture?

Returns the SurfaceTexture used by this view.

open TextureView.SurfaceTextureListener?

Returns the SurfaceTextureListener currently associated with this texture view.

open Matrix
getTransform(transform: Matrix?)

Returns the transform associated with this texture view.

open Boolean

Returns true if the SurfaceTexture associated with this TextureView is available for rendering.

open Boolean

Indicates whether this View is opaque.

open Canvas?

Start editing the pixels in the surface.

open Canvas?
lockCanvas(dirty: Rect?)

Just like lockCanvas() but allows specification of a dirty rectangle.

open Unit

open Unit
setForeground(foreground: Drawable!)

open Unit

Updates the Paint object used with the current layer (used only if the current layer type is not set to LAYER_TYPE_NONE).

open Unit
setLayerType(layerType: Int, paint: Paint?)

The layer type of a TextureView is ignored since a TextureView is always considered to act as a hardware layer.

open Unit
setOpaque(opaque: Boolean)

Indicates whether the content of this TextureView is opaque.

open Unit

Set the SurfaceTexture for this view to use.

open Unit

Sets the SurfaceTextureListener used to listen to surface texture events.

open Unit
setTransform(transform: Matrix?)

Sets the transform to associate with this texture view.

open Unit

Finish editing pixels in the surface.

Protected methods
open Unit

Unit
onDraw(canvas: Canvas)

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

open Unit
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)

open Unit
onVisibilityChanged(changedView: View, visibility: Int)

Inherited functions