Ravish Rajput Ravish Rajput - 1 year ago 307
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download