在cocos2d-x界面中嵌入Android的WebView

时间:2023-03-09 02:48:32
在cocos2d-x界面中嵌入Android的WebView

在Cocos2dxActivity.java中,

(1) 增加函数onCreateLayout,

[java]  view plain copy
  1. public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
  2. LinearLayout layout = new LinearLayout(this);
  3. layout.setOrientation(LinearLayout.VERTICAL);
  4. layout.addView(surfaceView);
  5. return layout;
  6. }

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

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

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

[java] 
view plain
copy
  1. public class HelloCpp extends Cocos2dxActivity{
  2. static HelloCpp sHelloCpp = null;
  3. LinearLayout mContentLayout;
  4. Cocos2dxGLSurfaceView mGlSurfaceView;
  5. LinearLayout mWebLayout;
  6. WebView mWebView;
  7. Button mBackButton;
  8. protected void onCreate(Bundle savedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. }
  11. public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
  12. mGlSurfaceView = surfaceView;
  13. sHelloCpp = this;
  14. mContentLayout = new LinearLayout(this);
  15. mContentLayout.setOrientation(LinearLayout.VERTICAL);
  16. mContentLayout.addView(surfaceView);
  17. mWebLayout = new LinearLayout(this);
  18. mWebLayout.setOrientation(LinearLayout.VERTICAL);
  19. return mContentLayout;
  20. }
  21. public Cocos2dxGLSurfaceView onCreateView() {
  22. Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
  23. // TestCpp should create stencil buffer
  24. glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
  25. return glSurfaceView;
  26. }
  27. //此函数提供给jni调用,返回自身类的对象
  28. public static HelloCpp getInstance() {//返回实例
  29. return sHelloCpp;
  30. }
  31. public void openWebView() {
  32. this.runOnUiThread(new Runnable() {//在主线程里添加别的控件
  33. public void run() {
  34. //初始化webView
  35. mWebView = new WebView(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(new WebViewClient(){
  44. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  45. if(url.indexOf("tel:")<0){
  46. view.loadUrl(url);
  47. }
  48. return true;
  49. }
  50. });
  51. /*初始化返回按钮*/
  52. mBackButton = new Button(HelloCpp.this);
  53. mBackButton.setLayoutParams(new LinearLayout.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(new OnClickListener() {
  58. public void onClick(View v) {
  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. public void removeWebView() {
  73. mContentLayout.removeView(mWebLayout);
  74. mWebLayout.destroyDrawingCache();
  75. mWebLayout.removeView(mWebView);
  76. mWebView.destroy();
  77. mWebLayout.removeView(mBackButton);
  78. mBackButton.destroyDrawingCache();
  79. }
  80. public boolean onKeyDown(int keyCoder,KeyEvent event) //重载函数,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. return false;
  89. }
  90. static {
  91. System.loadLibrary("game");
  92. }

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

  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. JniMethodInfo minfo;
  8. jobject jobj;
  9. bool isHave = 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