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.
4 Answers
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();
mTextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
txform.postTranslate(xoff, yoff);
mTextureView.setTransform(txform);
Note it centers as well as scales the video.

- 51,356
- 5
- 116
- 166
-
@fadden please check this question https://stackoverflow.com/questions/48988063/how-can-i-scale-video-in-exoplayer-v2-play-video-in-full-screen using expoplayer is it possible to ply in center – Sagar Feb 26 '18 at 12:39
Well I am too late to answer but as I got from Grafika you may use this function
private void adjustAspectRatio(int videoWidth, int videoHeight) {
int viewWidth = mTextureView.getWidth();
int viewHeight = mTextureView.getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
int newWidth, newHeight;
if (viewHeight > (int) (viewWidth * aspectRatio)) {
// limited by narrow width; restrict height
newWidth = viewWidth;
newHeight = (int) (viewWidth * aspectRatio);
} else {
// limited by short height; restrict width
newWidth = (int) (viewHeight / aspectRatio);
newHeight = viewHeight;
}
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
Log.v(TAG, "video=" + videoWidth + "x" + videoHeight +
" view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight +
" off=" + xoff + "," + yoff);
Matrix txform = new Matrix();
mTextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
//txform.postRotate(10); // just for fun
txform.postTranslate(xoff, yoff);
mTextureView.setTransform(txform);
}

- 4,170
- 3
- 23
- 35
-
-
@SagarHudge yeah sure you can use exolplayer to play videos, this question is for those who are not using any thirdparty library for playing videos – Umar Ata Feb 26 '18 at 11:36
-
I want to play video in full screen vertically but it stretch vertically on `FILL` resize ,I want to fill and center crop – Sagar Feb 26 '18 at 11:42
-
ask a seperate question with the code you tried and also attach some screenshots – Umar Ata Feb 26 '18 at 11:44
-
please check this question https://stackoverflow.com/questions/48988063/exoplayer-play-video-in-full-screen thank you – Sagar Feb 26 '18 at 12:02
-
@UmarAta Say, do you know perhaps how to do it for top-scale-crop, instead of center-crop? I asked about it here: https://stackoverflow.com/q/54216273/878126 – android developer Jan 20 '19 at 08:39
You can use ExoPlayer https://github.com/google/ExoPlayer# and check the full demo. It maintains the video aspect ratio perfectly.

- 17,195
- 9
- 77
- 119
-
-
it can depending of the View you use to display the video. (SurfaceView or TextureView, google the diff for more details) – Hugo Gresse Aug 05 '20 at 07:46
Expanding on fadden's answer above: If you want to align the video in another way other than just center it, modify these lines in the Grafika's "adjustAspectRatio()" code:
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
to:
int xoff = 0; //align left
or
int xoff = (viewWidth - newWidth); // align right
or
int yoff = 0; // align top
or
int yoff = (viewHeight - newHeight); // align bottom

- 1
- 1