代码之家  ›  专栏  ›  技术社区  ›  Martin Erlic

我如何才能保持我的Android视频视图的长宽比只与它的高度成比例?

  •  0
  • Martin Erlic  · 技术社区  · 6 年前

    我想设置一个视频填充屏幕,并保持其纵横比。但是,我不希望视频与手机的纵横比匹配。

    如果屏幕的宽高比为18.9:9,视频的宽高比为16:9,那么当我将视频放在继承fill_parent的视频视图中时,视频将显示为拉伸。如果我没有设置fill_parent,则视频将保留其纵横比,但只占用屏幕的1/3以下,因为纵横比由其宽度而不是高度决定。这是不可取的。

    我想用视频垂直填充屏幕,不管部分宽度是否被剪掉。

    当前代码:

    window.setFormat(PixelFormat.TRANSLUCENT)
    val videoView = findViewById < VideoView > (videoView)
    val video = Uri.parse("android.resource://" + packageName + "/" +
        R.raw.bgvideo)
    videoView.setVideoURI(video)
    videoView.setOnPreparedListener {
        mp: MediaPlayer - >
            mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
        mp.isLooping = true
        mp.setScreenOnWhilePlaying(false)
    }
    videoView.start()
    

    XML:

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="2000dp"
        android:layout_height="match_parent" />
    
    <!--- Sample Foreground Content !--->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="24dp"
        android:contentDescription="@string/logo"
        android:src="@drawable/logo" />
    

    当前结果:

    enter image description here

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  1
  •   Martin Erlic    6 年前

    调整您的 VideoView

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <!--parent needs to be matching the screen height-->
        <!--VideoView layout_width doesn't matter-->
    
        <VideoView
            android:id="@+id/video_player"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </FrameLayout>
    
    // at onCreate or anywhere
    
            // post, because view dimension is not initialized yet in here
            videoView.post(new Runnable() {
                @Override
                public void run() {
                    // do resizing here
                    // AR = aspect ratio
    
                    float videoARWidth = 16.f;
                    float videoARHeight = 9.f;
    
                    // Phone screen aspect ratio height
                    float screenARHeight = 9.f;
    
                    // scale to screen AR height
                    float videoScale = screenARHeight / videoARHeight;
    
                    float videoARRatio = videoARWidth / videoARHeight;
    
                    // scale the ratio to screen
                    float videoScaledARRatio = videoARRatio * videoScale;
    
                    ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
    
                    // make sure the VideoView matches the screen height
                    layoutParams.width = (int)(videoView.getHeight() * videoScaledARRatio);
                    videoView.setLayoutParams(layoutParams);
                }
            });
    
    

    此代码不关心屏幕宽度。

    宽度将始终按屏幕高度缩放。实际上,它总是按自己的高度缩放。但正如我对代码所评论的,使用layout_height=“match_parent”来匹配屏幕。

    我不确定你是否想让宽度适合屏幕宽度更大的情况。

    如果屏幕宽度较大,视频宽度将不会填充屏幕。