标签:cocos2d-x3.3rc0 android webview
代码部分摘自http://www.fusijie.com/blog/2013/12/26/play-cocos2dx-33/
Cocos2d-x3.3RC0通过Jni嵌入Android的WebView空间,在Cocos2d-x中显示网页。直接上代码。
用ADT打开proj.android的工程目录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;//返回关闭按钮 @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); //初始化线性布局,增加Button按钮的webView控件 m_topLayout = new LinearLayout(test); m_topLayout.setOrientation(LinearLayout.VERTICAL); //初始化返回按钮 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);//设置按钮相对布局位置,靠右 m_backButton.setOnClickListener(new OnClickListener() { @Override//按钮点击响应事件,删除webView public void onClick(View arg0) { // TODO Auto-generated method stub removeWebView();//下面定义 } }); m_webLayout.addView(m_imageView);//把image加入的主布局 m_topLayout.addView(m_backButton);//把返回按钮加入的线性布局 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; } }
//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); } } } }
#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 }
此时就可实现点击按钮,加载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,.)有的工程可能需要在AndroidManifest中添加网络的权限,如下:
<uses-permission android:name="android.permission.INTERNET"/>
Cocos2d-x3.3RC0加载Android的WebView
标签:cocos2d-x3.3rc0 android webview
原文地址:http://blog.csdn.net/yuxikuo_1/article/details/40833845