简体   繁体   English

Android-VideoView的全屏布局

[英]Android - Fullscreen Layout for VideoView

I want to show my Video with normal size on portrait, and with Full Size on Landscape. 我想在纵向上以正常大小显示视频,在横向上以全尺寸显示视频。 So when the users starts to watch at portrait and switches to Landscape(Rotates the device) It goes fullscreen; 因此,当用户开始以人像观看并切换为横向(旋转设备)时,它将全屏显示; when vice versa, It goes back to original size. 反之亦然,它恢复到原始大小。 I've checked internet but couldn't find a good example. 我已经检查过互联网,但找不到很好的例子。 My codes are below. 我的代码如下。

在此处输入图片说明在此处输入图片说明

VideoActivity.java: VideoActivity.java:

public class VideoActivity extends Activity { 公共类VideoActivity扩展了Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_video);


        Bundle bund = getIntent().getExtras();
        String s = bund.getString("LINK");
        VideoView videoView = (VideoView) findViewById(R.id.videoView);
        MediaController controller = new MediaController(this);
        videoView.setVideoPath(s);
        videoView.setMediaController(controller);
        videoView.start();
    }
}

activity_video.xml : activity_video.xml:

<RelativeLayout 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="${relativePackage}.${activityClass}"
    android:background="#000000"
    >

    <VideoView
        android:id="@+id/videoView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"

         />

</RelativeLayout>

Basically. 基本上。 When user starts the video it can be original size (If Portrait Mode) but whenever he turns the phone around I want it to go fullscreen automatically. 当用户启动视频时,它可以是原始大小(如果是“纵向”模式),但是只要他转过手机,我都希望它自动全屏显示。 And when turned back to portrait, original size again. 当转回肖像时,再次恢复原始尺寸。

Thanks. 谢谢。

Create a new file named bools.xml in values folder as below: values文件夹中创建一个名为bools.xml的新文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <item type="bool" name="isPortLayout">true</item>
</resources>

create another file in values-land folder named bools.xml as below: values-land文件夹中创建另一个文件,名为bools.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <item type="bool" name="isPortLayout">false</item>
</resources>

Now in your activity before calling to setContentView get this resource and based on its value set Activity as full screen or not: 现在,在您的活动中,调用setContentView之前,请获取此资源并根据其值将Activity设置为全屏或不全屏:

boolean isPortLayout = getResources().getBoolean(R.bool.isLargeLayout);
if(isPortLayout) {
    // PORT
} else {
    // LAND
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Set your VideoView 's width and height match_parent . 设置VideoView的宽度和高度match_parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM