代码部分摘自http://www.fusijie.com/blog/2013/12/26/play-cocos2dx-33/
Cocos2d-x3.3RC0通过Jni嵌入Android的WebView空间,在Cocos2d-x中显示网页。直接上代码。
1、Java层代码
用ADT打开proj.android的project文件夹src文件夹下的org.cocos2dx.cpp文件夹下的AppActivity.java。加入例如以下代码:
//AppActivity.java /**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ package org.cocos2dx.cpp; import org.cocos2dx.lib.Cocos2dxActivity; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import com.pactera.webview.R; public class AppActivity extends Cocos2dxActivity { static AppActivity test = null;//AppActivity实例 WebView m_webView;//WebView控件 ImageView m_imageView;//ImageView控件 FrameLayout m_webLayout;//FrameLayout布局 LinearLayout m_topLayout;//LinearLayout布局 Button m_backButton;//返回关闭button @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); test = this; m_webLayout = new FrameLayout(this);//新建布局 FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(640,1000);//布局的尺寸 lytp.gravity = Gravity.CENTER;//布局的Type addContentView(m_webLayout, lytp);//增加ContentView } //初始化activity public static AppActivity getInstance(){ return test; } //显示webView public void openWebview(){ this.runOnUiThread(new Runnable() {//在主线程里载入控件 @Override public void run() { // TODO Auto-generated method stub //初始化webview m_webView = new WebView(test); //设置webview能够运行js脚本 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(); //假设页面中有链接,使链接在当前的浏览器中响应 m_webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub if(url.indexOf("tel:")<0){ view.loadUrl(url); } return true; } }); //背景 m_imageView = new ImageView(test); m_imageView.setImageResource(R.drawable.bg); m_imageView.setScaleType(ImageView.ScaleType.FIT_XY); //初始化线性布局,增加Buttonbutton的webView控件 m_topLayout = new LinearLayout(test); m_topLayout.setOrientation(LinearLayout.VERTICAL); //初始化返回button m_backButton = new Button(test); m_backButton.setBackgroundResource(R.drawable.back); LinearLayout.LayoutParams lypt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); lypt.gravity = Gravity.RIGHT;//设置布局类型 m_backButton.setLayoutParams(lypt);//设置button相对布局位置,靠右 m_backButton.setOnClickListener(new OnClickListener() { @Override//button点击响应事件,删除webView public void onClick(View arg0) { // TODO Auto-generated method stub removeWebView();//以下定义 } }); m_webLayout.addView(m_imageView);//把image增加的主布局 m_topLayout.addView(m_backButton);//把返回button增加的线性布局 m_topLayout.addView(m_webView);//把m_webView增加到线性布局 m_webLayout.addView(m_topLayout);//把线性布局增加的主布局 } }); } public void removeWebView(){ //删除m_imageView m_webLayout.removeView(m_imageView); m_imageView.destroyDrawingCache(); //删除m_topLayout m_webLayout.removeView(m_topLayout); m_topLayout.destroyDrawingCache(); //删除m_webView m_topLayout.removeView(m_webView); m_webView.destroy(); //删除m_backButton m_topLayout.removeView(m_backButton); m_backButton.destroyDrawingCache(); } public boolean onKeyDown(int keyCoder,KeyEvent event) { //假设网页能够后退,则网页后退。否则删除webView if(m_webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){ m_webView.goBack(); }else{ removeWebView(); } return false; } }
2、Jni层代码
在Jni/hellocpp文件夹下新建基于cocos2d-x的类test,加入例如以下代码://test.h #ifndef TEST_H_ #define TEST_H_ extern "C" { void showWeb(); } /* namespace cocos2d */ #endif /* TEST_H_ */
/* * test.cpp * * Created on: 2014-11-5 * Author: chen */ #include "test.h" #include "cocos2d.h" #include "platform/android/jni/JniHelper.h" #include <jni.h> #define CLASS_NAMEROOT "org/cocos2dx/cpp/AppActivity" USING_NS_CC; extern "C" { void showWeb() { JniMethodInfo t; //推断CLASS_NAME的类中是否存在showTipDialog函数,假设存在,则调用。 if(JniHelper::getStaticMethodInfo(t,CLASS_NAMEROOT,"getInstance","()Lorg/cocos2dx/cpp/AppActivity;")) { jobject jobj;//存储对象 jobj = t.env->CallStaticObjectMethod(t.classID,t.methodID); bool isHave = JniHelper::getMethodInfo(t,CLASS_NAMEROOT,"openWebview","()V"); if(isHave) { t.env->CallVoidMethod(jobj,t.methodID); } } } }
3、C++层代码
在classes文件夹下的HelloWorldScene.cpp中的头文件处加入例如以下代码#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "../proj.android/jni/hellocpp/test.h" #endif在menuCloseCallback中加入例如以下代码:
void HelloWorld::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) showWeb(); #endif }
此时就可实现点击button,载入Android的WebView控件了。可是,另一些工作要做,就是mk文件,
在LOCAL_SRC_FILES加入上新建的test.cpp文件。
例如以下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp LOCAL_SRC_FILES := hellocpp/main.cpp hellocpp/test.cpp ../../Classes/AppDelegate.cpp ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_STATIC_LIBRARIES := cocos2dx_static include $(BUILD_SHARED_LIBRARY) $(call import-module,.)有的project可能须要在AndroidManifest中加入网络的权限。例如以下:
<uses-permission android:name="android.permission.INTERNET"/>