webview支持视频全屏播放以及方向随重力感应自适应变化

webview支持视频全屏播放以及方向随重力感应自适应变化,超实用,已经过测试准行!准行!准行!

直接上代码:

public class CustomWebView extends WebView {
    private boolean mIsFullScreen = false;
    private Context mActivity;
    private ProgressBar mProgressBar;
    private int perScreenStatus = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 记录视频播放前屏幕状态,默认竖屏

    /**
     * 是否使用默认的WebSettings相关设置
     */
    private boolean defaultSetting = false;
    /**
     * 页面加载是否显示进度条, 默认不显示
     */
    private boolean showProgressBar = false;
    /**
     * 视频播放是否可全屏播放,默认是
     */
    private boolean supportFullScreenPlayVideo = true;
    /**
     * 视频全屏播放时屏幕方向 默认 设置页面方向随重力感应自适应变化
     */
    private int videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;

    /**
     * 视频全屏参数
     */
    protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    private View customView;
    private FrameLayout fullscreenContainer;
    private WebChromeClient.CustomViewCallback customViewCallback;

    public CustomWebView(@NonNull Context context) {
        super(context);
        mActivity = context;
        initView();
    }

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mActivity = context;
        //属性
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomWebView);
        defaultSetting = mTypedArray.getBoolean(R.styleable.CustomWebView_defaultSetting, defaultSetting);
        showProgressBar = mTypedArray.getBoolean(R.styleable.CustomWebView_showProgressBar, showProgressBar);
        supportFullScreenPlayVideo = mTypedArray.getBoolean(R.styleable.CustomWebView_supportFullScreenPlayVideo, supportFullScreenPlayVideo);
        int gravityType = mTypedArray.getInt(R.styleable.CustomWebView_defaultSetting, videoScreenOrientation);
        switch (gravityType) {
            case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
                videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
                videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
                videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
                break;
        }

        initView();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mIsFullScreen) {
            int width = getDefaultSize(0, widthMeasureSpec);
            int height = getDefaultSize(0, heightMeasureSpec);
            setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMea
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值