如何使VideoView全屏显示 [英] How to make VideoView full screen

查看:1131
本文介绍了如何使VideoView全屏显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用VideoViewActivity中播放视频,并在单击Button时使其全屏显示和landscape模式(隐藏虚拟按钮和状态栏).

I want to play a video in my Activity using a VideoView,and make it fullscreen and landscape mode (with hiding virtual button and status bar)when I click a Button.

但是它不能隐藏虚拟按钮,并且底部有一条白线.

But it can not hide the virtual button and it has a white line in bottom.

这是我的活动代码:

This my activity code:

public class VideoActivity extends Activity {
private VideoView mVideoView;
private String mUrl;
private Button mFullScreen;
private static String TAG = VideoActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate");
    setContentView(R.layout.video);
    mVideoView = (VideoView) findViewById(R.id.video);
    mFullScreen = (Button) findViewById(R.id.fullscreen);
    File file = new File(Environment.getExternalStorageDirectory(),"video.mp4");
    mVideoView.setVideoPath(file.getPath());
    mVideoView.start();
    mFullScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enterFullScreen();
            mFullScreen.setVisibility(View.GONE);
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy");
}

private void enterFullScreen(){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//常亮
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT,
      RelativeLayout.LayoutParams.MATCH_PARENT
    );
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  }
}

video.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"/>
    <Button
        android:id="@+id/fullscreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="Fullscreen"/>
</RelativeLayout>

推荐答案

在横向模式下尝试此操作.

Try this on your landscape mode.

<VideoView android:id="@+id/video"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true"
   android:layout_alignParentBottom="true"
   android:layout_alignParentTop="true"
    />

隐藏虚拟按钮添加以下代码:

Hide virtual buttons add this code:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

原始大小

         DisplayMetrics metrics = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metrics);
         android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
         params.width =  (int) (300*metrics.density);
         params.height = (int) (250*metrics.density);
         params.leftMargin = 30;
         videoView.setLayoutParams(params);

全屏尺寸

         DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
         android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
         params.width =  metrics.widthPixels;
         params.height = metrics.heightPixels;
         params.leftMargin = 0;
         videoView.setLayoutParams(params);

这篇关于如何使VideoView全屏显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆