Android programming for WebView full-screen playback

Source: Internet
Author: User

This article mainly describes the Android programming to achieve webview full-screen playback method, combined with the case of the form of more detailed analysis of the Android WebView full-screen playback of the layout and function-related skills, the need for friends can refer to the next!

The examples in this paper describe how Android programming implements WebView full-screen playback. Share to everyone for your reference, as follows:

Recently because the project to use WebView load HTML5 video, start not full-screen broadcast, did a long time to make! So, according to my understanding, how to achieve full screen it.

First write the layout file 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= "Full screen does not display the button, click to toggle Horizontal Screen"android:gravity= "Center"/> <WebView Android:id= "@+id/video_webview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"/> </LinearLayout>
View Code

Principle: To achieve full-screen when the WebView video into a view (the layout of the Video_view control) inside, and then the WebView hidden away! This enables full-screen playback!
Now take a look at how it's implemented:

First put code Mainactivity.java:

 Public classMainactivityextendsActivity {PrivateFramelayout Videoview;//Video Load View when full screen PrivateButton Videolandport;PrivateWebView Videowebview;PrivateBoolean Islandport =true;//True indicates that this is a vertical screen and false indicates a horizontal screen at this time.  PrivateView Xcustomview;Privatexwebchromeclient xwebchromeclient;PrivateString url = "http://look.appjx.cn/mobile_api.php?mod=news&id=12604"; Privatewebchromeclient.customviewcallback xcustomviewcallback; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (window.feature_no_title);//Remove app titleGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN  );  Setcontentview (R.layout.activity_main);  Initwidget ();  Initlistener (); Videowebview.loadurl (URL); } Private voidInitlistener () {//TODO auto-generated Method StubVideolandport.setonclicklistener (NewListener ()); } Private voidInitwidget () {//TODO auto-generated Method StubVideoview =(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 Enables or disables WebView access to file data setblocknetworkimage whether the network image is displayed * Setbuiltinzoomcontrols settings support scaling Setcachem Ode Set Buffered Mode * setdefaultfontsize set default font size setdefaulttextencodingname Set default encoding used when decoding * setfixedfontfamily set fixed used font set Javasciptenabled Settings Support JavaScript * Setlayoutalgorithm set layout mode setlighttouchenabled settings with mouse activation by option * Setsupportzoom settings support variable Coke **/Ws.setbuiltinzoomcontrols (true);//Hide Zoom buttonWs.setlayoutalgorithm (WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//Typesetting adaptation ScreenWs.setusewideviewport (true);//can be scaled at any scaleWs.setloadwithoverviewmode (true);//the Setusewideviewport method sets the window recommended for use by WebView. The Setloadwithoverviewmode method is to set the mode of WebView loaded pages. Ws.setsavepassword (true); Ws.setsaveformdata (true);//Save form dataWs.setjavascriptenabled (true); Ws.setgeolocationenabled (true);//enable geo-targetingWs.setgeolocationdatabasepath ("/data/data/org.itri.html5webview/databases/");//set the location of the database pathWs.setdomstorageenabled (true); Xwebchromeclient=Newxwebchromeclient ();  Videowebview.setwebchromeclient (xwebchromeclient); Videowebview.setwebviewclient (Newxwebviewclientent ()); } classListenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub   Switch(V.getid ()) { CaseR.id.video_landport:if(Islandport) {setrequestedorientation (activityinfo.screen_orientation_portrait); Videolandport.settext ("Full screen does not show the button, click to toggle Horizontal Screen"); }Else{setrequestedorientation (activityinfo.screen_orientation_landscape); Videolandport.settext ("Full screen does not show the button, click to toggle Vertical Screen"); }     Break; default:     Break; } }} @Override Public BooleanOnKeyDown (intKeyCode, 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; }  /*** Determine if it is full screen *@return  */   Public BooleanIncustomview () {return(Xcustomview! =NULL); }   /*** Press the back key to exit full screen when full screen method*/    Public voidHidecustomview () {Xwebchromeclient.onhidecustomview (); } /*** Handling JavaScript dialogs, website icons, website titles, page loading progress, etc. *@author  */  Public classXwebchromeclientextendswebchromeclient {PrivateBitmap Xdefaltvideo; PrivateView Xprogressvideo; @Override//the method that the full screen will be called when playing network video   Public voidOnshowcustomview (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 a view already exists, then immediately terminate and create a new   if(Xcustomview! =NULL) {Callback.oncustomviewhidden (); return;   } videoview.addview (view); Xcustomview=view; Xcustomviewcallback=callback;  Videoview.setvisibility (view.visible); } @Override//the video playback exits full screen will be called   Public voidOnhidecustomview () {if(Xcustomview = =NULL)//not full-screen playback status    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");  }  //Video Loading add default icon@Override PublicBitmap Getdefaultvideoposter () {//log.i (LogTag, "here in on Getdefaultvideoposter");   if(Xdefaltvideo = =NULL) {Xdefaltvideo=Bitmapfactory.decoderesource (Getresources (), R.drawable.videoicon); }   returnXdefaltvideo; }  //process loading when video is loaded@Override PublicView 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); }   returnXprogressvideo; }  //Page Title@Override Public voidOnreceivedtitle (WebView view, String title) {(mainactivity. This). Settitle (title); }//@Override//  //Update window Progress when WebView progress is changed//Public void onprogresschanged (WebView view, int newprogress) {//(mainactivity.this). GetWindow (). Setfeatureint (window.feature_progress, newprogress*100);//   }   } /*** Handling of various notifications, requests and other events *@author  */  Public classXwebviewcliententextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {log.i ("Webviewtest", "shouldoverrideurlloading:" +URL); return false; } } /*** This method is called when the screen is switched@author  */@Override Public voidonconfigurationchanged (Configuration newconfig) {log.i ("Testwebview", "=====<<< onconfigurationchanged >>>====="); Super. onconfigurationchanged (Newconfig); if(Newconfig.orientation = =Configuration.orientation_landscape) {LOG.I ("WebView", "now is horizontal screen 1"); Islandport=false; }Else if(Newconfig.orientation = =configuration.orientation_portrait) {LOG.I ("WebView", "now is the vertical screen 1"); Islandport=true; } }}
View Code

I wrote a lot of comments in the code, I believe that we can understand, of course, I am a rookie, there may be some places to understand the wrong! You know, you can tell me. Then I added a function in it, is to click on the WebView above button to switch the screen! There is also when clicking on full screen playback force horizontal screen playback, click Back to the vertical screen! I do not use mobile phone gravity sensor to switch the screen! The code also added a playback progress of the layout file, we can see in the source!

Finally say Androidmanifest.xml set;

Access network permissions plus this sentence:

<uses-permission android:name= "Android.permission.INTERNET"/>

When switching the screen in order to not recall OnCreate and other methods, to add this sentence:

Android:configchanges= "Orientation|keyboardhidden|screensize"

Almost so that you can achieve full-screen playback, if you do not understand the question can ask me,

Full instance code code click here to download this site.

Hopefully this article will help you with Android programming.

Transferred from: http://www.jb51.net/article/75110.htm

Android programming for WebView full-screen playback

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.