【问题标题】:Android Video not fitting the width in portrait of the videoview and not occupying full screen in landscapeAndroid Video不适合videoview的纵向宽度并且横向不占据全屏
【发布时间】:2013-08-14 08:56:49
【问题描述】:

Android 视频在纵向时不适合 videoview 的宽度,如何使视频的宽度适合纵向时 videoview 的宽度,当我将屏幕方向更改为横向时,我想要视频像 youtube 应用一样填满整个屏幕。纵向时,视频从屏幕顶部到屏幕中间开始,横向时则填满整个屏幕。这是我尝试过的,我还添加了完整的链接screenshot

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:weightSum="100" >

<VideoView
    android:id="@+id/videoview"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_weight="53" >
</VideoView>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:background="@drawable/top_bar"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/settings"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/settings" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/low_tab"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/low"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/low" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/high_tab"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/high"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/high" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/audio_tab"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/audio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/audio" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/full_screen"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/full"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/full" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/fresh"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/refresh" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:background="@drawable/comment_bar"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/liveblog_tab"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/live_blog" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/addcom_tab"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/desc"
            android:src="@drawable/add_comment" />
    </LinearLayout>
</LinearLayout>

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="33" >

    <LinearLayout
        android:id="@+id/liveblog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/browser"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </WebView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/comments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</FrameLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-layout fullscreen android-videoview portrait


    【解决方案1】:

    您需要有两种不同的布局。一张是人像,一张是风景。 创建两个具有相同名称的 xml 文件,并将它们放入文件夹“layout-land”(横向)和“layout-port”(纵向)。

    Here 你可以看看如何处理方向变化。

    这可以是使视频全屏显示的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" >
    <VideoView android:id="@+id/myvideoview"
             android:layout_width="fill_parent"
             android:layout_alignParentRight="true"
             android:layout_alignParentLeft="true"
             android:layout_alignParentTop="true"
             android:layout_alignParentBottom="true"
             android:layout_height="fill_parent">
      </VideoView>
    </RelativeLayout>
    

    编辑:这是您可以在方法中处理它的方式。

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    
      // Checks the orientation of the screen
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(Your Landscape layout);
      } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(Your portrait layout);
      }
    }
    

    【讨论】:

    • 我在我的清单 android:configChanges="orientation|keyboardHidden" 中有这个,这会阻止我的 layout-land 被识别。如果我决定从清单中删除它,我只能使用您在布局中提供的上述代码
    • 如果清单中有这个,那么您需要自己处理配置更改。在这种情况下,您的 Activity 会调用 onConfigurationChanged() 方法。在这里,您可以设置新的内容视图并处理 Activity 中已有的数据。例如您可以在此处恢复您的视频。
    • 最近投了反对票。请解释拒绝投票的原因,而不仅仅是拒绝投票和离开。如果您认为给出的答案有问题,您应该与认为答案正确的人分享您的想法。
    【解决方案2】:

    对于 linearlayout 我不确定。但是使用 relativelayout 您可以按如下方式进行。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    
    <VideoView
        android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >
    </VideoView>
    
    //your other xml content 
    ....
    ....
    ....
    
    </RelativeLayout>
    

    如果你想让 videoview 也被填满高度。那么你需要做这个代码:

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true">
    </VideoView>
    
    //your other xml content 
    ....
    ....
    ....
    
    </RelativeLayout>
    

    如果有帮助,请告诉我。谢谢。

    【讨论】:

    • @blessed 3 年后我们还在等待。
    • 5 年了
    【解决方案3】:
      private void fitStretchClick() {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(this, Uri.parse(arrayListVideos.get(mPosition).getSongPath()));
        Bitmap bmp = retriever.getFrameAtTime();
    
        float width = ((float) bmp.getWidth()) / ((float) bmp.getHeight());
        int width2 = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();
        float f = (float) width2;
        float f2 = (float) height;
        float f3 = f / f2;
        ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();
        ViewGroup.LayoutParams layoutParams2 = mVideoView.getLayoutParams();
        if (changedNow) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
            params.width = metrics.widthPixels;
            params.height = metrics.heightPixels;
            params.leftMargin = 0;
            mVideoView.setLayoutParams(params);
            Toast.makeText(this, "Stretch To Screen", Toast.LENGTH_SHORT).show();
            changedNow = false;
            return;
        } else {
            changedNow = true;
            Toast.makeText(this, "Fit To Screen", Toast.LENGTH_SHORT).show();
            if (width > f3) {
                layoutParams.width = width2;
                layoutParams.height = (int) (f / width);
                mVideoView.setLayoutParams(layoutParams);
                return;
            }
            layoutParams.width = (int) (width * f2);
            layoutParams.height = height;
            mVideoView.setLayoutParams(layoutParams);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多