Related actions for full-screen video in WebView

Source: Internet
Author: User

In recent work, basically has been using webview, today put it down:

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

First of all you have to define, initialize a webview, in fact, the online sample is very much, I simply put some webview may be used in some of the very important properties and support for full-screen playback video How to implement the code to stick it, directly into the project can be


<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 the WebView property, you can run JavaScript script webview.getsettings (). Setjavascriptenabled (TRUE);//settings WebView can load many other 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. Assuming that 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 more than 2.2 full version number <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);// Open the form yourself voluntarily 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 to try to note the following two methods are important, many need to rewrite: <span style= "White-space:pre" ></span>// First set up your own defined 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 means to enter full screen, and onhidecustomview to exit full screen.

Interface words, is a webview, a framelayout, when the full screen when the settings webview hidden, so that the Framelayout full screen display, then the video on their own initiative to 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 used by the host application to notify the current page of its own defined view has been dismissed 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: Note that I have captured the flags in full-screen state and in the form state under their respective values. Visible such as the following code

/** * 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// Status under form: 1098973440}/** * exit full screen */private void Quitfullscreen () {//Declare the current screen state 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 self-defined webviewclient, inheriting 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 mobile 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 run some action when the error message is received, go this way} @Overridepublic void onpagestarted (WebView view, String URL, Bitmap Favicon) {super.onpagestarted (view, URL, favicon);//Want to run some actions when the page starts loading, go this way} @Overridepublic void Onpagefinished ( WebView view, String URL) {super.onpagefinished (view, URL);//You want to run some actions at the end of page loading, go this way}}
Finally another point is very important, in order to enter the same WebView browsing the Web page, 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, can contact me ha, in the following message is good, 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

Related Article

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.