欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中使用WebView实现全屏切换播放网页视频

程序员文章站 2022-05-03 22:15:50
首先写布局文件activity_main.xml: 原理:实现全屏的时候把webview里的视频放到一个View(布局里的video_view控件)里面,然后把webview隐藏掉!这样就实现了全屏播放的!现在具体来看看怎么实现的:先放代码MainActivity.java: 最后说下Android ......

首先写布局文件activity_main.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <framelayout 
        android:id="@+id/video_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        ></framelayout>
    <button 
        android:id="@+id/video_landport"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="全屏不显示该按扭,点击切换横屏"
        android:gravity="center"
        />
    <webview 
        android:id="@+id/video_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    </linearlayout>

  原理:实现全屏的时候把webview里的视频放到一个view(布局里的video_view控件)里面,然后把webview隐藏掉!这样就实现了全屏播放的!
现在具体来看看怎么实现的:
先放代码mainactivity.java:

public class mainactivity extends activity {
 
    private framelayout videoview;// 全屏时视频加载view
    private button videolandport;
    private webview videowebview;
    private boolean islandport = true;//true表示此时是竖屏,false表示此时横屏。
    private view xcustomview;
    private xwebchromeclient xwebchromeclient;
    private string url = "http://look.appjx.cn/mobile_api.php?mod=news&id=12604";
    private webchromeclient.customviewcallback     xcustomviewcallback;
 
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        requestwindowfeature(window.feature_no_title);//去掉应用标题
        getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
                windowmanager.layoutparams.flag_fullscreen);
        setcontentview(r.layout.activity_main);
        initwidget();
        initlistener();
        videowebview.loadurl(url);
    }
 
    private void initlistener() {
        // todo auto-generated method stub
        videolandport.setonclicklistener(new listener());
    }
 
    private void initwidget() {
        // todo auto-generated method stub
        videoview = (framelayout) findviewbyid(r.id.video_view);
        videolandport = (button) findviewbyid(r.id.video_landport);
        videowebview = (webview) findviewbyid(r.id.video_webview);
        websettings ws = videowebview.getsettings();
        /**
         * setallowfileaccess 启用或禁止webview访问文件数据 setblocknetworkimage 是否显示网络图像
         * setbuiltinzoomcontrols 设置是否支持缩放 setcachemode 设置缓冲的模式
         * setdefaultfontsize 设置默认的字体大小 setdefaulttextencodingname 设置在解码时使用的默认编码
         * setfixedfontfamily 设置固定使用的字体 setjavasciptenabled 设置是否支持javascript
         * setlayoutalgorithm 设置布局方式 setlighttouchenabled 设置用鼠标激活被选项
         * setsupportzoom 设置是否支持变焦
         * */
        ws.setbuiltinzoomcontrols(true);// 隐藏缩放按钮
        ws.setlayoutalgorithm(websettings.layoutalgorithm.narrow_columns);// 排版适应屏幕
        ws.setusewideviewport(true);// 可任意比例缩放
        ws.setloadwithoverviewmode(true);// setusewideviewport方法设置webview推荐使用的窗口。setloadwithoverviewmode方法是设置webview加载的页面的模式。
        ws.setsavepassword(true);
        ws.setsaveformdata(true);// 保存表单数据
        ws.setjavascriptenabled(true);
        ws.setgeolocationenabled(true);// 启用地理定位
        ws.setgeolocationdatabasepath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径
        ws.setdomstorageenabled(true);
        xwebchromeclient = new xwebchromeclient();
        videowebview.setwebchromeclient(xwebchromeclient);
        videowebview.setwebviewclient(new xwebviewclientent());
    }
 
    class listener implements onclicklistener {
        @override
        public void onclick(view v) {
            // todo auto-generated method stub
            switch (v.getid()) {
            case r.id.video_landport:
                if (islandport) {
                    setrequestedorientation(activityinfo.screen_orientation_portrait);
                    videolandport.settext("全屏不显示该按扭,点击切换横屏");
                }else {
                    setrequestedorientation(activityinfo.screen_orientation_landscape); 
                    videolandport.settext("全屏不显示该按扭,点击切换竖屏");
                }
                break;
            default:
                break;
            }
        }
    }
       @override
        public boolean onkeydown(int keycode, keyevent event) {
            if (keycode == keyevent.keycode_back) {
                if (incustomview()) {
                    hidecustomview();
                    return true;
                }else {
                videowebview.loadurl("about:blank");
//                    mtestwebview.loaddata("", "text/html; charset=utf-8", null);
                mainactivity.this.finish();
                    log.i("testwebview", "===>>>2");
            }
            }
            return true;
        }
       /**
        * 判断是否是全屏
        * @return
        */
        public boolean incustomview() {
             return (xcustomview != null);
         }
         /**
          * 全屏时按返加键执行退出全屏方法
          */
         public void hidecustomview() {
             xwebchromeclient.onhidecustomview();
         }
    /**
     * 处理javascript的对话框、网站图标、网站标题以及网页加载进度等
     * @author
     */
    public class xwebchromeclient extends webchromeclient {
        private bitmap xdefaltvideo;
        private view xprogressvideo;
        @override
        //播放网络视频时全屏会被调用的方法
        public void onshowcustomview(view view, webchromeclient.customviewcallback callback)
        {
            if (islandport) {
            }
            else{
                 
//                ii = "1";
//                setrequestedorientation(activityinfo.screen_orientation_portrait); 
            }
            setrequestedorientation(activityinfo.screen_orientation_landscape); 
            videowebview.setvisibility(view.gone);
            //如果一个视图已经存在,那么立刻终止并新建一个
            if (xcustomview != null) {
                callback.oncustomviewhidden();
                return;
            }            
            videoview.addview(view);
            xcustomview = view;
            xcustomviewcallback = callback;
            videoview.setvisibility(view.visible);
        }
         
        @override
        //视频播放退出全屏会被调用的
        public void onhidecustomview() {
             
            if (xcustomview == null)//不是全屏播放状态
                return;                      
            // hide the custom view.
            setrequestedorientation(activityinfo.screen_orientation_portrait); 
            xcustomview.setvisibility(view.gone);
             
            // remove the custom view from its container.
            videoview.removeview(xcustomview);
            xcustomview = null;
            videoview.setvisibility(view.gone);
            xcustomviewcallback.oncustomviewhidden();
             
            videowebview.setvisibility(view.visible);
             
            //log.i(logtag, "set it to webvew");
        }
        //视频加载添加默认图标
        @override
        public bitmap getdefaultvideoposter() {
            //log.i(logtag, "here in on getdefaultvideoposter");   
            if (xdefaltvideo == null) {
                xdefaltvideo = bitmapfactory.decoderesource(
                        getresources(), r.drawable.videoicon);
            }
            return xdefaltvideo;
        }
        //视频加载时进程loading
        @override
        public view getvideoloadingprogressview() {
            //log.i(logtag, "here in on getvideoloadingpregressview");
             
            if (xprogressvideo == null) {
                layoutinflater inflater = layoutinflater.from(mainactivity.this);
                xprogressvideo = inflater.inflate(r.layout.video_loading_progress, null);
            }
            return xprogressvideo; 
        }
        //网页标题
         @override
         public void onreceivedtitle(webview view, string title) {
            (mainactivity.this).settitle(title);
         }
 
//         @override
//       //当webview进度改变时更新窗口进度
//         public void onprogresschanged(webview view, int newprogress) {
//             (mainactivity.this).getwindow().setfeatureint(window.feature_progress, newprogress*100);
//         }      
    }
    /**
     * 处理各种通知、请求等事件
     * @author
     */
    public class xwebviewclientent extends webviewclient {
         @override
            public boolean shouldoverrideurlloading(webview view, string url) {
                log.i("webviewtest", "shouldoverrideurlloading: "+url);
                return false;
            }
    }
    /**
     * 当横竖屏切换时会调用该方法
     * @author
     */
    @override
    public void onconfigurationchanged(configuration newconfig) {
        log.i("testwebview", "=====<<<  onconfigurationchanged  >>>=====");
         super.onconfigurationchanged(newconfig);
          
         if(newconfig.orientation == configuration.orientation_landscape){
             log.i("webview", "   现在是横屏1");
             islandport = false;
            }else if(newconfig.orientation == configuration.orientation_portrait){
             log.i("webview", "   现在是竖屏1");
             islandport = true;
            }
    }
}

  最后说下androidmanifest.xml设置;
访问网络权限加上这句
<uses-permission android:name="android.permission.internet"/>
当切换横竖屏时为了不重新调用oncreate等方法,要加个这句:
android:configchanges="orientation|keyboardhidden|screensize"

完成

参考于:https://www.cnblogs.com/lee0oo0/p/4039654.html