Ravish Rajput Ravish Rajput - 3 months ago 44x
Android Question

Playing video on texture view with aspect ratio of video

I am playing video from my server on surface using texture view, but its stretching the video and not able maintain aspect ratio of video. But, I wish to maintain aspect ratio as per the video as in vine app or instagram app.


You can either change the size of the View (using a custom FrameLayout), or use the TextureView matrix to change the way the texture is rendered.

Examples of both can be found in Grafika. The "Play video (TextureView)" activity demonstrates configuring the matrix to match the aspect ratio of a video. See the adjustAspectRatio() method, the core of which is:

    Matrix txform = new Matrix();
    txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
    txform.postTranslate(xoff, yoff);

Note it centers as well as scales the video.