Related actions for full-screen video in WebView

Source: Internet
Author: User

In recent work, basically has been using webview, today to tidy it up:

WebView as the name implies, is to put a Web page, a look is very simple, but it is not so simple to use the control.

First of all you have to define, initialize a webview, in fact, there are many examples of online, I will simply put some of the webview may be used in some of the important properties and support for full-screen video How to implement the code stick out, directly into the project to the line


<span style= "White-space:pre" ></span><pre name= "code" class= "java" style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>private WebView webview;<span style= "font-family:arial, Helvetica, Sans-serif; " ></span>

<span style= "White-space:pre" ></span>webview = (webview) Findviewbyid (R.id.webview);

  <span style= "White-space:pre" ></span>//set webview properties, capable of executing JavaScript script webview.getsettings (). Setjavascriptenabled (TRUE);//Set WebView to load more format pages <span style= "White-space:pre" ></span> Webview.getsettings (). Setloadwithoverviewmode (true);//settings WebView use a wide range of Windows <span style= "White-space:pre" ></ Span>webview.getsettings (). Setusewideviewport (true);//sets the WebView user-agent string. If the string "UA" is null or empty, it will use the system default User-agent string <span style= "White-space:pre" ></span>webview.getsettings (). Setuseragentstring ();//support gesture scaling <span style= "White-space:pre" ></span>webview.getsettings (). Setbuiltinzoomcontrols (TRUE);//support for over 2.2 all versions <span style= "White-space:pre" ></span>webview.getsettings ( ). Setpluginstate (Pluginstate.on);//Tell WebView to enable the application cache API. <span style= "White-space:pre" ></span>webview.getsettings (). Setappcacheenabled (true);//Set whether DOM is enabled Storage API. <span style= "White-space:pre" ></span>webview.getsettings (). Setdomstorageenabled (true);// Auto Open Window Webview.getsetTings (). Setjavascriptcanopenwindowsautomatically (true);//No words will black screen support plugin webview.getsettings (). setpluginsenabled ( true); /** * setallowfileaccess Enable or disable WebView access to file data setblocknetworkimage whether the network image is displayed * Setbuiltinzoomcontrols settings support Scaling Setcachemode Set Buffered mode * setdefaultfontsize set default font size SETDEFAULTTEXTENCODINGNAME set the default encoding to use when decoding * setfixedfontfamily Set the fixed-use font setjavasciptenabled settings support JavaScript * Setlayoutalgorithm set layout mode setlighttouchenabled settings with mouse activation by option * Setsupportzoom setting supports Zoom * *///typesetting adaptation screen. Setlayoutalgorithm (WebSettings.LayoutAlgorithm.NARROW_COLUMNS); web/ /Set up the web it is important to note the following two methods, many of which need rewriting: <span style= "White-space:pre" ></span>// First set up a custom webchromeclient to set some problems with video playback buswifiwebview.setwebchromeclient (new defaultwebchromeclient ()); Buswifiwebview.setwebviewclient (New Defaultwebviewclientclient ());
The first method: Webchromeclient:
To override the Onshowcustomview method to indicate a full screen entry, and Onhidecustomview to exit the full screen

Interface words, is a webview, a framelayout, when the full screen when the settings webview hidden, let the framelayout full screen display, then the video will automatically run to framelayout this inside put. Exit full screen, the same reason, here to use the callback function, you understand it ... I don't speak well ...

Private class Defaultwebchromeclient extends Webchromeclient {//A callback interface uses a host application that notifies the current page that a custom view has been removed Customviewcallback customviewcallback;//Enter full screen @overridepublic void Onshowcustomview (view view, Customviewcallback callback) {// Assign a value to Callbackcustomviewcallback = callback;//Set WebView hide Webview.setvisibility (view.gone);//Declare video, Put the video back in here, Framelayout. Framelayout Findviewbyid (R.id.video);//Put video in the current View Video.addview (view);// Horizontal screen display setrequestedorientation (activityinfo.screen_orientation_landscape);//Set Full screen Setfullscreen ();} Exit full screen when @overridepublic void Onhidecustomview () {if (customviewcallback! = null) {// Hide Customviewcallback.oncustomviewhidden ();} User's current preferred direction setrequestedorientation (activityinfo.screen_orientation_user);//Exit Full screen quitfullscreen ();// Set WebView visible webview.setvisibility (view.visible);} @Overridepublic void onprogresschanged (WebView view, int newprogress) {super.onprogresschanged (view, newprogress);}}
Here are two ways to set up full screen and exit full screen: Notice that I have captured the flags in full-screen state and in the window state respectively. The following code is visible

/** * Set full screen */private void Setfullscreen () {//Set full screen related properties, get current screen state, and then set full Screen GetWindow (). SetFlags ( Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);//full-screen status code: 1098974464// Window Status: 1098973440}/** * Exit full screen */private void Quitfullscreen () {//Declare the current screen state parameters and get final windowmanager.layoutparams attrs = GetWindow (). GetAttributes (); attrs.flags &= (~windowmanager.layoutparams.flag_fullscreen); GetWindow (). SetAttributes (attrs); GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);}
The second custom webviewclient, inherited webviewclient
/** * using system default WebView */private class Defaultwebviewclientclient extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading ( WebView view, String URL) {
<span style= "White-space:pre" ></span>return super.shouldoverrideurlloading (view, URL);// If you want to download the game on the page, or continue to click on the link in the page to go to the next page, override this method, or you will jump to the phone's own browser, and not continue to show in your webview} @Overridepublic void Onreceivederror (WebView view, int errorcode,string description, String failingurl) {<span style= "White-space:pre" ></span>//want to take some action when you get the error message, go this way} @Overridepublic void onpagestarted (WebView view, String URL, Bitmap Favicon) {super.onpagestarted (view, URL, favicon);//Want to do some action when the page starts to load, go this way} @Overridepublic void Onpagefinished ( WebView view, String URL) {super.onpagefinished (view, URL);//Want to do some action at the end of page loading, go this way}}
Finally, it is important to go to the same webview to browse the Web, and the point return key is to return in the WebView, rather than exit the program directly, then you have to rewrite the onkeydown method. And also

@Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = keyevent.keycode_back) {WebView WebView = ( WebView) Findviewbyid (R.id.webview_passenger), if (Webview.cangoback ()) {//GoBack () = return WebView of the previous page Webview.goback ( );//Exit Full screen Quitfullscreen ();} else {}return true;} Return Super.onkeydown (KeyCode, event);}
What is the problem, you can contact me ha, just leave a message on the next, every day online ...


Well, on top of that, it's been so much, studied for days. Attach a good demo, the original demo to 3 points, I as a VIP for free down to everyone


Demo:http://download.csdn.net/detail/lygscg123/7673123

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.