在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
- public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
- LinearLayout layout = new LinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.addView(surfaceView);
- return layout;
- }
(2) 在 this.mGLSurfaceView = this.onCreateView() 下面增加这一行:
- LinearLayout contentLayout = this.onCreateLayout(mGLSurfaceView);
(3) 应用的Activity文件实现如下,
- public class HelloCpp extends Cocos2dxActivity{
- static HelloCpp sHelloCpp = null;
- LinearLayout mContentLayout;
- Cocos2dxGLSurfaceView mGlSurfaceView;
- LinearLayout mWebLayout;
- WebView mWebView;
- Button mBackButton;
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- }
- public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView 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 should create stencil buffer
- glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
- return glSurfaceView;
- }
- //此函数提供给jni调用,返回自身类的对象
- public static HelloCpp getInstance() {//返回实例
- return sHelloCpp;
- }
- public void openWebView() {
- this.runOnUiThread(new Runnable() {//在主线程里添加别的控件
- public void run() {
- //初始化webView
- mWebView = new WebView(HelloCpp.this);
- //设置webView能够执行javascript脚本
- mWebView.getSettings().setJavaScriptEnabled(true);
- //载入URL
- mWebView.loadUrl("file:///android_asset/index.html");
- //使页面获得焦点
- //mWebView.requestFocus();
- //如果页面中链接,如果希望点击链接继续在当前browser中响应
- mWebView.setWebViewClient(new WebViewClient(){
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if(url.indexOf("tel:")<0){
- view.loadUrl(url);
- }
- return true;
- }
- });
- /*初始化返回按钮*/
- mBackButton = new Button(HelloCpp.this);
- mBackButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 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);
- }
- });
- //把webView加入到线性布局
- mGlSurfaceView.setVisibility(View.GONE);
- mWebLayout.addView(mBackButton);
- mWebLayout.addView(mWebView);
- mContentLayout.addView(mWebLayout);
- }
- });
- }
- //移除webView 把刚才加的所有控件都删掉
- 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) //重载函数,android手机实体返回键回调函数
- {
- if(mWebView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView
- mWebView.goBack();
- }else{
- removeWebView();
- mGlSurfaceView.setVisibility(View.VISIBLE);
- }
- return false;
- }
- static {
- System.loadLibrary("game");
- }
从cocos2d-x的界面中打开WebView的代码:
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- //getStaticMethodInfo,判断Java静态函数是否存在,并且把信息保存到minfo里
- //参数1:JniMethodInfo
- //参数2:Java类包名+类名
- //参数3:Java函数名称
- //参数4:函数参数类型和返回值类型,这里的返回值类型是HelloCpp类的对象。写法:L+包名+; 其他的类型请看上面的“JNI详细教程”
- JniMethodInfo minfo;
- jobject jobj;
- bool isHave = JniHelper::getStaticMethodInfo(minfo, "cn/livelog/popdiamond/HelloCpp","getInstance","()Lcn/livelog/popdiamond/HelloCpp;");
- if (isHave)
- {
- //调用Java静态函数,取得对象。
- jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
- if (jobj != NULL)
- {
- isHave = JniHelper::getMethodInfo(minfo,"cn/livelog/popdiamond/HelloCpp","openWebView","()V");
- if (isHave)
- {
- //调用java非静态函数, 参数1:Java对象,上面已经取得 参数2:方法ID
- minfo.env->CallVoidMethod(jobj, minfo.methodID);
- }
- }
- }
- #endif
解决方案2:
Android平台嵌入 需求:点击一个按钮打开Android WebView
1. 通过JNI从cocos2d-x的C++代码调用java代码。
这是Android实现WebView嵌入的前提条件,详细JNI教程:http://www.himigame.com/android-game/725.html
2.Xcode代码
1.导入头文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)//预编译 判断是否是android平台 #include <jni.h> #include "platform/android/jni/JniHelper.h" #include <android/log.h> #endif
2.在按钮回调方法代码
void HelloWorld::openWebView(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //Jni对象,可以看成结构体 JniMethodInfo minfo; //getStaticMethodInfo,判断Java静态函数是否存在,并且把信息保存到minfo里 //参数1:JniMethodInfo //参数2:Java类包名+类名 //参数3:Java函数名称 //参数4:函数参数类型和返回值类型,这里的返回值类型是Majiang类的对象。写法:L+包名+; 其他的类型请看上面的“JNI详细教程” bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/yourcompany/Majiang/Majiang","getInstance","()Lcom/yourcompany/Majiang/Majiang;"); jobject jobj;//存对象 if (isHave) { CCLog("静态函数存在"); //调用Java静态函数,取得对象。 jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); } //getMethodInfo,判断Java非静态函数是否存在,并且把信息保存到minfo里 isHave = JniHelper::getMethodInfo(minfo,"com/yourcompany/Majiang/Majiang","openAndroidView","()V"); if (isHave) { CCLog("非静态函数存在"); //调用java非静态函数, 参数1:Java对象,上面已经取得 参数2:方法ID minfo.env->CallVoidMethod(jobj, minfo.methodID); } #endif }
3.Java代码
cocos2d-x的android项目,在eclipse下src目录里,有一个跟项目名称相同的类,就在这个类里写代码。
这个类初始代码:
package com.yourcompany.Majiang; import... public class Majiang extends Cocos2dxActivity{ protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); } static { System.loadLibrary("game"); } }
添加代码后:
public class Majiang extends Cocos2dxActivity{ WebView m_webView; ImageView m_imageView; static Majiang majiang = null; FrameLayout m_webLayout; LinearLayout topLayout; Button backButton; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //全局变量保存this majiang=this; //初始化一个空的布局 m_webLayout = new FrameLayout(this); addContentView(m_webLayout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } //此函数提供给jni调用,返回自身类的对象 public static Majiang getInstance() {//返回实例 return majiang; } public void openAndroidView() { this.runOnUiThread(new Runnable() {//在主线程里添加别的控件 public void run() { //初始化webView m_webView = new WebView(majiang); //设置webView能够执行javascript脚本 m_webView.getSettings().setJavaScriptEnabled(true); //设置可以支持缩放 m_webView.getSettings().setSupportZoom(true);//设置出现缩放工具 m_webView.getSettings().setBuiltInZoomControls(true); //载入URL m_webView.loadUrl("http://www.baidu.com"); //使页面获得焦点 m_webView.requestFocus(); //如果页面中链接,如果希望点击链接继续在当前browser中响应 m_webView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.indexOf("tel:")<0){ view.loadUrl(url); } return true; } }); /*背景图*/ m_imageView = new ImageView(majiang); m_imageView.setImageResource(R.drawable.lobbybg); m_imageView.setScaleType(ImageView.ScaleType.FIT_XY); /*初始化线性布局 里面加按钮和webView*/ topLayout = new LinearLayout(majiang); topLayout.setOrientation(LinearLayout.VERTICAL); /*初始化返回按钮*/ backButton = new Button(majiang); backButton.setBackgroundResource(R.drawable.backbutton); backButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); backButton.setText("关 闭"); backButton.setTextColor(Color.argb(255, 255, 218, 154)); backButton.setTextSize(14); backButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { removeWebView(); } }); //把image加到主布局里 m_webLayout.addView(m_imageView); //把webView加入到线性布局 topLayout.addView(backButton); topLayout.addView(m_webView); //再把线性布局加入到主布局 m_webLayout.addView(topLayout); } }); } //移除webView 把刚才加的所有控件都删掉 public void removeWebView() { m_webLayout.removeView(m_imageView); m_imageView.destroyDrawingCache(); m_webLayout.removeView(topLayout); topLayout.destroyDrawingCache(); topLayout.removeView(m_webView); m_webView.destroy(); topLayout.removeView(backButton); backButton.destroyDrawingCache(); } public boolean onKeyDown(int keyCoder,KeyEvent event)//重载函数,android手机实体返回键回调函数 { if(m_webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView m_webView.goBack(); }else{ removeWebView(); } return false; } }