(转载请注明原文地址:http://blog.csdn.net/guggy/article/details/10435157


在Cocos2dxActivity.java中,

(1) 增加函数onCreateLayout,

[java] view plain copy
  1. publicLinearLayoutonCreateLayout(Cocos2dxGLSurfaceViewsurfaceView){
  2. LinearLayoutlayout=newLinearLayout(this);
  3. layout.setOrientation(LinearLayout.VERTICAL);
  4. layout.addView(surfaceView);
  5. returnlayout;
  6. }

(2) 在this.mGLSurfaceView = this.onCreateView() 下面增加这一行:

[java] view plain copy
  1. LinearLayoutcontentLayout=this.onCreateLayout(mGLSurfaceView);

(3) 应用的Activity文件实现如下,

[java] view plain copy
  1. publicclassHelloCppextendsCocos2dxActivity{
  2. staticHelloCppsHelloCpp=null;
  3. LinearLayoutmContentLayout;
  4. Cocos2dxGLSurfaceViewmGlSurfaceView;
  5. LinearLayoutmWebLayout;
  6. WebViewmWebView;
  7. ButtonmBackButton;
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. }
  11. publicLinearLayoutonCreateLayout(Cocos2dxGLSurfaceViewsurfaceView){
  12. mGlSurfaceView=surfaceView;
  13. sHelloCpp=this;
  14. mContentLayout=newLinearLayout(this);
  15. mContentLayout.setOrientation(LinearLayout.VERTICAL);
  16. mContentLayout.addView(surfaceView);
  17. mWebLayout=newLinearLayout(this);
  18. mWebLayout.setOrientation(LinearLayout.VERTICAL);
  19. returnmContentLayout;
  20. }
  21. publicCocos2dxGLSurfaceViewonCreateView(){
  22. Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
  23. //TestCppshouldcreatestencilbuffer
  24. glSurfaceView.setEGLConfigChooser(5,6,5,0,16,8);
  25. returnglSurfaceView;
  26. }
  27. //此函数提供给jni调用,返回自身类的对象
  28. publicstaticHelloCppgetInstance(){//返回实例
  29. returnsHelloCpp;
  30. }
  31. publicvoidopenWebView(){
  32. this.runOnUiThread(newRunnable(){//在主线程里添加别的控件
  33. publicvoidrun(){
  34. //初始化webView
  35. mWebView=newWebView(HelloCpp.this);
  36. //设置webView能够执行javascript脚本
  37. mWebView.getSettings().setJavaScriptEnabled(true);
  38. //载入URL
  39. mWebView.loadUrl("file:///android_asset/index.html");
  40. //使页面获得焦点
  41. //mWebView.requestFocus();
  42. //如果页面中链接,如果希望点击链接继续在当前browser中响应
  43. mWebView.setWebViewClient(newWebViewClient(){
  44. publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
  45. if(url.indexOf("tel:")<0){
  46. view.loadUrl(url);
  47. }
  48. returntrue;
  49. }
  50. });
  51. /*初始化返回按钮*/
  52. mBackButton=newButton(HelloCpp.this);
  53. mBackButton.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  54. mBackButton.setText("Close");
  55. mBackButton.setTextColor(Color.argb(255,255,218,154));
  56. mBackButton.setTextSize(14);
  57. mBackButton.setOnClickListener(newOnClickListener(){
  58. publicvoidonClick(Viewv){
  59. removeWebView();
  60. mGlSurfaceView.setVisibility(View.VISIBLE);
  61. }
  62. });
  63. //把webView加入到线性布局
  64. mGlSurfaceView.setVisibility(View.GONE);
  65. mWebLayout.addView(mBackButton);
  66. mWebLayout.addView(mWebView);
  67. mContentLayout.addView(mWebLayout);
  68. }
  69. });
  70. }
  71. //移除webView把刚才加的所有控件都删掉
  72. publicvoidremoveWebView(){
  73. mContentLayout.removeView(mWebLayout);
  74. mWebLayout.destroyDrawingCache();
  75. mWebLayout.removeView(mWebView);
  76. mWebView.destroy();
  77. mWebLayout.removeView(mBackButton);
  78. mBackButton.destroyDrawingCache();
  79. }
  80. publicbooleanonKeyDown(intkeyCoder,KeyEventevent)//重载函数,android手机实体返回键回调函数
  81. {
  82. if(mWebView.canGoBack()&&keyCoder==KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView
  83. mWebView.goBack();
  84. }else{
  85. removeWebView();
  86. mGlSurfaceView.setVisibility(View.VISIBLE);
  87. }
  88. returnfalse;
  89. }
  90. static{
  91. System.loadLibrary("game");
  92. }

从cocos2d-x的界面中打开WebView的代码:

[cpp] view plain copy
  1. #if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
  2. //getStaticMethodInfo,判断Java静态函数是否存在,并且把信息保存到minfo里
  3. //参数1:JniMethodInfo
  4. //参数2:Java类包名+类名
  5. //参数3:Java函数名称
  6. //参数4:函数参数类型和返回值类型,这里的返回值类型是HelloCpp类的对象。写法:L+包名+;其他的类型请看上面的“JNI详细教程”
  7. JniMethodInfominfo;
  8. jobjectjobj;
  9. boolisHave=JniHelper::getStaticMethodInfo(minfo,"cn/livelog/popdiamond/HelloCpp","getInstance","()Lcn/livelog/popdiamond/HelloCpp;");
  10. if(isHave)
  11. {
  12. //调用Java静态函数,取得对象。
  13. jobj=minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);
  14. if(jobj!=NULL)
  15. {
  16. isHave=JniHelper::getMethodInfo(minfo,"cn/livelog/popdiamond/HelloCpp","openWebView","()V");
  17. if(isHave)
  18. {
  19. //调用java非静态函数,参数1:Java对象,上面已经取得参数2:方法ID
  20. minfo.env->CallVoidMethod(jobj,minfo.methodID);
  21. }
  22. }
  23. }
  24. #endif

同类文章参考:http://stackoverflow.com/questions/19535060/cocos2d-x-how-to-add-videoview-and-cocos2dxglsurfaceview-in-same-screen

更多相关文章

  1. C语言函数的递归(上)
  2. Android源码学习之五-Android的IPC机制
  3. Android之HttpURLConnection小结
  4. Windown类
  5. Android之使用PackageManager取得程序的包名、图标等
  6. 获取Android设备屏幕的相关参数
  7. Flutter 基础(二)基本组件ListView
  8. Android-NDK开发之基础--Android(安卓)JNI实例代码(四)-- JNI中
  9. android中的生命周期(新增2个函数)

随机推荐

  1. jquery获取input值的各种情况
  2. jQuery File Upload 单页面多实例的实现
  3. jquery每个添加类中间延迟
  4. Jquery ajax回调函数不执行
  5. 有什么方法可以让jQuery循环在到达终点或
  6. ajax 四级级联菜单(jquery实现)
  7. jquery监听事件on写法以及简单的拖拽效果
  8. 使用ASP时最好不要硬编码url。与JQuery净
  9. 在mvc中多次调用Ajax请求
  10. 请教一下ajax代替frame在后台管理系统布