android 视频横竖屏切换

java 代码:

package com.xgs.panku;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import com.xgs.panku.ui.BaseActivity;
import com.xgs.panku.utils.DensityUtils;
import com.xgs.panku.utils.L;

/**
* 类描述:
* 作者:zyx
* 时间:2016/8/30 17:08
* 邮箱:zhangyuanxiao@monster-tld.com
*/
public class TestActivity extends BaseActivity {
MediaController mediaco;
private Button btn;
private boolean isFulllScreen = false;
private boolean isShow = false;
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
mediaco = new MediaController(this);
Uri uri = Uri.parse("http://www.bilibili.com/video/av6042017/");
videoView = (VideoView) this.findViewById(R.id.video1);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(uri);
//videoView.start();
videoView.requestFocus();
// videoView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// if(isFulllScreen&&!isShow){
// showStatusBar();
// }else{
// hideStatusBar();
// }
// }
// });
findViewById(R.id.btn_qp).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// isFulllScreen = !isFulllScreen;
// if (isFulllScreen) {
// WindowManager.LayoutParams params = getWindow().getAttributes();
// params.flags|= WindowManager.LayoutParams.FLAG_FULLSCREEN;
// getWindow().setAttributes(params);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// } else {
// WindowManager.LayoutParams params = getWindow().getAttributes();
// params.flags&=(~WindowManager.LayoutParams.FLAG_FULLSCREEN) ;
// getWindow().setAttributes(params);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// }
if(isFulllScreen==false)
{
setFullScreen();
isFulllScreen=true;
}
else
{
setNormalScreen();
isFulllScreen=false;
}
}
});
}


@Override
public int getLayout() {
return R.layout.activity_test;
}


private void setFullScreen()
{
FrameLayout.LayoutParams lp1=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
FrameLayout.LayoutParams lp2=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
videoView.setLayoutParams(lp2);
}


private void setNormalScreen()
{
FrameLayout.LayoutParams lp1=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(this, 150));
FrameLayout.LayoutParams lp2=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,DensityUtils.dp2px(this, 180));
FrameLayout.LayoutParams lp3=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
FrameLayout.LayoutParams lp4=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
lp1.gravity= Gravity.TOP;
lp2.gravity=Gravity.TOP;
lp3.gravity=Gravity.TOP;
lp2.setMargins(0, DensityUtils.dp2px(this, 45), 0,0);
lp3.setMargins(0,DensityUtils.dp2px(this, 225), 0,0);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
videoView.setLayoutParams(lp4);

}



private void hideStatusBar() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}

private void showStatusBar() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}


}

布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/video1"
android:layout_width="match_parent"
android:layout_height="400dp" />
<Button
android:id="@+id/btn_qp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全屏"/>
</FrameLayout>

转载于:https://www.cnblogs.com/zyx20160131/p/5824985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值