需求是全屏播放视频,刘海屏上面也要播放。
下面是我实现的方式:
首先创建 CustomVideoView 工具类:
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
/**
* Name: 吴庆森
* Date: 2019/7/9
* Mailbox: [email protected]
* Describe:
*/
public class CustomVideoView extends VideoView {
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
//int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
然后在xml文件中应用:
最后在 activity 中使用:
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import ceshi.com.myapp.R;
import ceshi.com.myapp.utils.CustomVideoView;
/**
* Name: 吴庆森
* Date: 2019/7/9
* Mailbox: [email protected]
* Describe:
*/
public class VideoActivity1 extends FragmentActivity {
CustomVideoView videoPlay;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInit();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.video_activivy1);
videoPlay = findViewById(R.id.videoPlay);
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息(单位:px)
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeigh = dm.heightPixels;
int notificationBar = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
//30dp转化为px
final float scale = getResources().getDisplayMetrics().density;
//由30dp转化来的px
int px = (int) (notificationBar * scale + 0.5f);
//动态设置控件宽高
ViewGroup.LayoutParams params = videoPlay.getLayoutParams();
params.height = screenHeigh + (px);
params.width = screenWidth ;
videoPlay.setLayoutParams(params);
videoPlay.setVideoPath("https://vdse.bdstatic.com/90bbe567a344884644b7046ba4b9906e.mp4?authorization=bce-auth-v1%2F40f207e648424f47b2e3dfbb1014b1a5%2F2017-05-11T09%3A02%3A31Z%2F-1%2F%2F1addef5f6cf2e56204e1f49e982c26e425b0fb9d9f845231bd7db8c541dcaeee");
videoPlay.start();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setInit() {
//沉浸状态栏
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(Color.TRANSPARENT);
//设置页面全屏显示
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
//设置页面延伸到刘海区显示
getWindow().setAttributes(lp);
}
}
这样就可以全屏幕播放视频了,但是视频会有一定的拉伸。