• 在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.   
    9.     protected void onCreate(Bundle savedInstanceState){  
    10.         super.onCreate(savedInstanceState);  
    11.     }  
    12.   
    13.     public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {  
    14.         mGlSurfaceView = surfaceView;  
    15.         sHelloCpp = this;  
    16.   
    17.         mContentLayout = new LinearLayout(this);  
    18.         mContentLayout.setOrientation(LinearLayout.VERTICAL);  
    19.         mContentLayout.addView(surfaceView);  
    20.   
    21.         mWebLayout = new LinearLayout(this);  
    22.         mWebLayout.setOrientation(LinearLayout.VERTICAL);  
    23.   
    24.         return mContentLayout;  
    25.     }  
    26.   
    27.     public Cocos2dxGLSurfaceView onCreateView() {  
    28.         Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);  
    29.         // TestCpp should create stencil buffer  
    30.         glSurfaceView.setEGLConfigChooser(5650168);  
    31.   
    32.         return glSurfaceView;  
    33.     }  
    34.   
    35.     //此函数提供给jni调用,返回自身类的对象  
    36.     public static HelloCpp getInstance() {//返回实例  
    37.         return sHelloCpp;  
    38.     }  
    39.   
    40.     public void openWebView() {  
    41.         this.runOnUiThread(new Runnable() {//在主线程里添加别的控件  
    42.             public void run() {  
    43.                 //初始化webView  
    44.                 mWebView = new WebView(HelloCpp.this);  
    45.                 //设置webView能够执行javascript脚本  
    46.                 mWebView.getSettings().setJavaScriptEnabled(true);  
    47.                 //载入URL  
    48.                 mWebView.loadUrl("file:///android_asset/index.html");  
    49.                 //使页面获得焦点  
    50.                 //mWebView.requestFocus();  
    51.                 //如果页面中链接,如果希望点击链接继续在当前browser中响应  
    52.                 mWebView.setWebViewClient(new WebViewClient(){  
    53.                     public boolean shouldOverrideUrlLoading(WebView view, String url) {  
    54.                         if(url.indexOf("tel:")<0){  
    55.                             view.loadUrl(url);  
    56.                         }  
    57.                         return true;  
    58.                     }  
    59.                 });  
    60.   
    61.                 /*初始化返回按钮*/  
    62.                 mBackButton = new Button(HelloCpp.this);  
    63.                 mBackButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));  
    64.                 mBackButton.setText("Close");  
    65.                 mBackButton.setTextColor(Color.argb(255255218154));  
    66.                 mBackButton.setTextSize(14);  
    67.                 mBackButton.setOnClickListener(new OnClickListener() {  
    68.                     public void onClick(View v) {  
    69.                         removeWebView();  
    70.                         mGlSurfaceView.setVisibility(View.VISIBLE);  
    71.                     }  
    72.                 });  
    73.   
    74.                 //把webView加入到线性布局  
    75.                 mGlSurfaceView.setVisibility(View.GONE);  
    76.                 mWebLayout.addView(mBackButton);  
    77.                 mWebLayout.addView(mWebView);  
    78.                 mContentLayout.addView(mWebLayout);  
    79.             }  
    80.         });  
    81.     }  
    82.     //移除webView  把刚才加的所有控件都删掉  
    83.     public void removeWebView() {  
    84.         mContentLayout.removeView(mWebLayout);  
    85.         mWebLayout.destroyDrawingCache();  
    86.   
    87.         mWebLayout.removeView(mWebView);  
    88.         mWebView.destroy();  
    89.   
    90.         mWebLayout.removeView(mBackButton);  
    91.         mBackButton.destroyDrawingCache();  
    92.     }  
    93.   
    94.     public boolean onKeyDown(int keyCoder,KeyEvent event) //重载函数,android手机实体返回键回调函数  
    95.     {  
    96.          if(mWebView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView  
    97.              mWebView.goBack();  
    98.          }else{  
    99.              removeWebView();  
    100.              mGlSurfaceView.setVisibility(View.VISIBLE);  
    101.          }  
    102.          return false;  
    103.     }  
    104.   
    105.     static {  
    106.         System.loadLibrary("game");  
    107.     }  

    从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.   
    8.         JniMethodInfo minfo;  
    9.         jobject jobj;  
    10.         bool isHave = JniHelper::getStaticMethodInfo(minfo, "cn/livelog/popdiamond/HelloCpp","getInstance","()Lcn/livelog/popdiamond/HelloCpp;");  
    11.         if (isHave)  
    12.         {  
    13.             //调用Java静态函数,取得对象。  
    14.             jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);  
    15.             if (jobj != NULL)  
    16.             {  
    17.                 isHave = JniHelper::getMethodInfo(minfo,"cn/livelog/popdiamond/HelloCpp","openWebView","()V");  
    18.                 if (isHave)  
    19.                 {  
    20.                     //调用java非静态函数, 参数1:Java对象,上面已经取得   参数2:方法ID  
    21.                     minfo.env->CallVoidMethod(jobj, minfo.methodID);  
    22.                 }  
    23.             }  
    24.         }  
    25. #endif  
  • 相关阅读:
    浏览器
    背景图片设置
    用CSS画平行四边形
    git常用操作
    函数Function
    Object类型的创建和访问
    执行环境及作用域
    传值和传引用
    String类型
    HTML< legend >标签
  • 原文地址:https://www.cnblogs.com/james1207/p/3327380.html
Copyright © 2020-2023  润新知