Embed Android WebView in the cocos2d-x Interface

Source: Internet
Author: User

In Cocos2dxActivity. in java, (1) added the onCreateLayout function, [java public LinearLayout onCreateLayout (Cocos2dxGLSurfaceView surfaceView) {LinearLayout layout = new LinearLayout (this); layout. setOrientation (LinearLayout. VERTICAL); layout. addView (surfaceView); return layout;} (2) in this. mGLSurfaceView = this. add this line under onCreateView (): [java] LinearLayout contentLayout = this. (3) Implement the application Activity file as follows: [java] public class HelloCpp extends Cocos2dxActivity {static HelloCpp sHelloCpp = null; LinearLayout layout; extends mGLSurfaceView; LinearLayout mWebLayout; webView mWebView; Button mBackButton; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} public LinearLayout onCreateLayout (Response surfaceView) {mGlSurfaceView = surfaceView; sHelloCpp = this; mContentLayout = new LinearLayout (this); mContentLayout. setOrientation (LinearLayout. VERTICAL); mContentLayout. addView (surfaceView); mWebLayout = new LinearLayout (this); mWebLayout. setOrientation (LinearLayout. VERTICAL); return mContentLayout;} public Cocos2dxGLSurfaceView onCreateView () {Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView (this); // TestCpp showould create stengle buffer glSurfaceView. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} // This function is provided to the jni call and the object public static HelloCpp getInstance () of the class is returned () {// return instance return sHelloCpp;} public void openWebView () {this. runOnUiThread (new Runnable () {// Add another control in the main thread public void run () {// initialize webView mWebView = new WebView (HelloCpp. this); // set webView to execute the javascript script mWebView. getSettings (). setJavaScriptEnabled (true); // load the URL mWebView. loadUrl (" file:///android_asset/index.html "); // Get the page focus // mWebView. requestFocus (); // if the link on the page is to be clicked, continue responding to mWebView in the current browser. setWebViewClient (new WebViewClient () {public boolean shouldOverrideUrlLoading (WebView view, String url) {if (url. indexOf ("tel:") <0) {view. loadUrl (url) ;}return true ;}});/* initialize the return Button */mBackButton = new Button (HelloCpp. this); mBackButton. setLayoutParams (new LinearLayout. layoutParams (LinearLayout. layoutParams. WRAP_CO NTENT, LinearLayout. layoutParams. WRAP_CONTENT); mBackButton. setText ("Close"); mBackButton. setTextColor (Color. argb (255,255,218,154); mBackButton. setTextSize (14); mBackButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {removeWebView (); mGlSurfaceView. setVisibility (View. VISIBLE) ;}}); // Add the webView to the linear layout mGlSurfaceView. setVisibility (View. GONE); mWebLayout. addView (mBac KButton); mWebLayout. addView (mWebView); mContentLayout. addView (mWebLayout) ;}}) ;}// remove webView to delete all the controls added earlier. public void removeWebView () {mContentLayout. removeView (mWebLayout); mWebLayout. destroyDrawingCache (); mWebLayout. removeView (mWebView); mWebView. destroy (); mWebLayout. removeView (mBackButton); mBackButton. destroyDrawingCache ();} public boolean onKeyDown (int keyCoder, KeyEvent event) // overload function, The callback function {if (mWebView. canGoBack () & keyCoder = KeyEvent. KEYCODE_BACK) {// If the webpage can be rolled back, it will be rolled back. If not, it will be removed from WebView mWebView. goBack () ;}else {removeWebView (); mGlSurfaceView. setVisibility (View. VISIBLE);} return false;} static {System. loadLibrary ("game");} open the WebView code from the cocos2d-x interface: [cpp] # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) // getStaticMethodInfo to determine whether Java static functions exist, and save the information to minfo. // parameter 1: JniMet HodInfo // parameter 2: Java class package name + class name // parameter 3: Java function name // parameter 4: function parameter type and return value type, here, the return value type is the object of the HelloCpp class. Writing Method: L + package name +; for other types, see the above "JNI detailed tutorial" JniMethodInfo minfo; jobject jobj; bool isHave = JniHelper: getStaticMethodInfo (minfo, "cn/livelog/popdiamond/HelloCpp", "getInstance", "() Lcn/livelog/popdiamond/HelloCpp;"); if (isHave) {// call Java static functions, get object. Jobj = minfo. env-> CallStaticObjectMethod (minfo. classID, minfo. methodID); if (jobj! = NULL) {isHave = JniHelper: getMethodInfo (minfo, "cn/livelog/popdiamond/HelloCpp", "openWebView", "() V"); if (isHave) {// call a non-static java function. Parameter 1: Java object. Parameter 2 has been obtained above: Method ID minfo. env-> CallVoidMethod (jobj, minfo. methodID) ;}}# endif

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.