Android Guides | Samples

Android.Views.TextureView Class

java Example

  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
      }
  }

See Also: TextureView

Syntax

[Android.Runtime.Register("android/view/TextureView", DoNotGenerateAcw=true)]
public class TextureView : View

Remarks

java Example

  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 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:

A TextureView's SurfaceTexture can be obtained either by invoking TextureView.SurfaceTexture or by using a NoType:android/view/TextureView$SurfaceTextureListener;Href=../../../reference/android/view/TextureView.SurfaceTextureListener.html. It is important to know that a SurfaceTexture is available only after the TextureView is attached to a window (and TextureView.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 TextureView.LockCanvas to draw onto the TextureView at the same time.

See Also

[Android Documentation]

Requirements

Namespace: Android.Views
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 14

The members of Android.Views.TextureView are listed below.

See Also: View

Public Constructors

Creates a new TextureView.
Creates a new TextureView.
Creates a new TextureView.
Creates a new TextureView.

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Properties

[read-only]
BitmapBitmap.
[read-only]
IsAvailableBoolean. Returns true if the SurfaceTexture associated with this TextureView is available for rendering.
SurfaceTextureSurfaceTexture. Returns the SurfaceTexture used by this view.
SurfaceTextureListenerTextureView+ISurfaceTextureListener. Returns the NoType:android/view/TextureView$SurfaceTextureListener;Href=../../../reference/android/view/TextureView.SurfaceTextureListener.html currently associated with this texture view.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

override
Draw(Canvas)
Subclasses of TextureView cannot do their own rendering with the Canvas object.
GetBitmap(Bitmap) : Bitmap
GetBitmap(Int32, Int32) : Bitmap
GetTransform(Matrix) : Matrix
Returns the transform associated with this texture view.
LockCanvas() : Canvas
LockCanvas(Rect) : Canvas
Just like TextureView.LockCanvas but allows specification of a dirty rectangle.
SetOpaque(Boolean)
Indicates whether the content of this TextureView is opaque.
SetTransform(Matrix)
UnlockCanvasAndPost(Canvas)
Finish editing pixels in the surface.

Protected Methods

override
OnDraw(Canvas)
Subclasses of TextureView cannot do their own rendering with the Canvas object.

Public Events