29

I am looking for something like the CENTER_CROP in ImageView.ScaleType

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop"

but for a VideoView. Does anything like this exist?

clocksmith
  • 6,226
  • 3
  • 32
  • 45
  • There is an old duplicate question here: http://stackoverflow.com/questions/11736311/android-videoview-proportional-scaling – Warpzit Jan 08 '14 at 11:25
  • Hope this may help you out - [Position Video Inside a VideoView](http://stackoverflow.com/questions/4619563/position-video-inside-a-videoview/4855315#4855315) – Tabrej Khan Jan 23 '14 at 14:38
  • You could use TextureView instead of VideoView(SurfaceView), It's possinle through TextureView. – Jambaaz Jan 24 '14 at 06:14
  • This solution for a VideoView scales Center Crop or Center Inside keeping the aspect ratio of the video like an ImageView does. Hope it helps someone! https://stackoverflow.com/a/53641686/6082973 – Carlitos Dec 06 '18 at 02:57

5 Answers5

17

You can only achieve this with a TextureView. (surfaceView won't work either). Here's a lib for playing video in a textureView with center crop function. TextureView can only be used in api level 14 and up unfortunately.

https://github.com/dmytrodanylyk/android-video-crop

Another possibility is to zoom in the videoview just right, but I haven't tried that yet.

Jordy
  • 1,764
  • 1
  • 22
  • 32
11

The simple and easy way if you are using ConstraintLayout.

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="@dimen/dimen_0dp"
        android:layout_height="@dimen/dimen_0dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

then

In Kotlin:

videoView.setOnPreparedListener { mediaPlayer ->
    val videoRatio = mediaPlayer.videoWidth / mediaPlayer.videoHeight.toFloat()
    val screenRatio = videoView.width / videoView.height.toFloat()
    val scaleX = videoRatio / screenRatio
    if (scaleX >= 1f) {
        videoView.scaleX = scaleX
    } else {
        videoView.scaleY = 1f / scaleX
    }
}

See my Java version answer here: https://stackoverflow.com/a/59069357/6255841

And this worked for me.

Nabin
  • 1,451
  • 3
  • 19
  • 26
3

Nabin's answer worked for me.

Here is the Java version:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        float videoRatio = mp.getVideoWidth() / (float) mp.getVideoHeight();
        float screenRatio = videoView.getWidth() / (float) videoView.getHeight();
        float scaleX = videoRatio / screenRatio;
        if (scaleX >= 1f) {
            videoView.setScaleX(scaleX);
        } else {
            videoView.setScaleY(1f / scaleX);
        }
    }
});
Nabin
  • 1,451
  • 3
  • 19
  • 26
InnisBrendan
  • 2,079
  • 2
  • 19
  • 21
1
//store the SurfaceTexture to set surface for MediaPlayer
mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() {
@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface,
            int width, int height) {
        FullScreenActivity.this.mSurface = surface;

    }
Hitesh Singh
  • 1,951
  • 1
  • 10
  • 15
0

Just manage overhang of the video out of FrameLayout

<FrameLayout
                android:id="@+id/videoViewHolder"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                tools:visibility="visible">
    
                <VideoView
                    android:id="@+id/videoView"
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"
                    android:layout_gravity="center"/>
            </FrameLayout>
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62