WebView prend en charge la lecture vidéo en plein écran et les changements de direction adaptatifs avec détection de gravité

WebView prend en charge la lecture vidéo en plein écran et les changements de direction adaptatifs avec détection de la gravité. C'est super pratique et a été testé et fonctionne ! D'ACCORD! D'ACCORD!

Accédez directement au code :

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

Je suppose que tu aimes

Origine blog.csdn.net/ck3345143/article/details/130382492
conseillé
Classement