Center Crop an Android VideoView

后端 未结 4 872
既然无缘
既然无缘 2020-12-05 23:46

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 dim

相关标签:
4条回答
  • 2020-12-06 00:23

    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.

    0 讨论(0)
  • 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);
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-06 00:26
    //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;
    
        }
    
    0 讨论(0)
  • 2020-12-06 00:32

    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.

    0 讨论(0)
提交回复
热议问题