TextureView in android

you want to display a live video stream or any content stream such as video or an OpenGL scene, you can use TextureView provided by android in order to do that.

In order to use TextureView, all you need to do is get its SurfaceTexture.The SurfaceTexture can then be used to render content. In order to do this, you just need to do instantiate an object of this class and implement SurfaceTextureListener interface. Its syntax is given below −

private TextureView myTexture;
public class MainActivity extends Activity implements SurfaceTextureListener{
   protected void onCreate(Bundle savedInstanceState) {
      myTexture = new TextureView(this);
      myTexture.setSurfaceTextureListener(this);
      setContentView(myTexture);
   }
}

After that, what you need to do is to override its methods. The methods are listed as follows −

@Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int arg2) {
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
}

Any view that is displayed in the texture view can be rotated and its alpha property can be adjusted by using setAlpha and setRotation methods. Its syntax is given below −

myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);

Apart from these methods, there are other methods available in TextureView class. They are listed below −

Sr.No Method & description
1

getSurfaceTexture()

This method returns the SurfaceTexture used by this view.

2

getBitmap(int width, int height)

This method returns Returns a Bitmap representation of the content of the associated surface texture.

3

getTransform(Matrix transform)

This method returns the transform associated with this texture view.

4

isOpaque()

This method indicates whether this View is opaque.

5

lockCanvas()

This method start editing the pixels in the surface

6

setOpaque(boolean opaque)

This method indicates whether the content of this TextureView is opaque.

7

setTransform(Matrix transform)

This method sets the transform to associate with this texture view.

8

unlockCanvasAndPost(Canvas canvas)

This method finish editing pixels in the surface.