【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebView
2013-12-11 19:12
483 查看
(转载请注明原文地址:http://blog.csdn.net/guggy/article/details/10435157)
在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
[java] view
plaincopy
public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(surfaceView);
return layout;
}
(2) 在 this.mGLSurfaceView = this.onCreateView() 下面增加这一行:
[java] view
plaincopy
LinearLayout contentLayout = this.onCreateLayout(mGLSurfaceView);
(3) 应用的Activity文件实现如下,
[java] view
plaincopy
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的代码:
[cpp] view
plaincopy
#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
同类文章参考:http://stackoverflow.com/questions/19535060/cocos2d-x-how-to-add-videoview-and-cocos2dxglsurfaceview-in-same-screen
在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
[java] view
plaincopy
public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(surfaceView);
return layout;
}
(2) 在 this.mGLSurfaceView = this.onCreateView() 下面增加这一行:
[java] view
plaincopy
LinearLayout contentLayout = this.onCreateLayout(mGLSurfaceView);
(3) 应用的Activity文件实现如下,
[java] view
plaincopy
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的代码:
[cpp] view
plaincopy
#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
同类文章参考:http://stackoverflow.com/questions/19535060/cocos2d-x-how-to-add-videoview-and-cocos2dxglsurfaceview-in-same-screen
相关文章推荐
- 【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebView
- Cocos2d-x游戏开发之Cocos2dx通过JNI调用Android的Java代码(webView实例)
- 在cocos2dx界面中嵌入android的webview。(c++篇,包含c++和java的交互)
- android游戏开发初学之SurfaceView绘制界面
- 使用python开发android应用程序(4):使用WebView制作程序界面
- 在cocos2d-x界面中嵌入Android的WebView
- 【COCOS2DX-ANDROID-游戏开发之二二】之 调用Cocos2dxGLSurfaceView
- Android WebView 应用界面开发教程
- cocos2dx 嵌入Android webview
- 在游戏中实现跳转到webView界面(Android实现)
- 在cocos2d-x界面中嵌入Android的WebView
- 在cocos2d-x界面中嵌入Android的WebView
- Android -- 样式和主题,用代码方式编写UI,用WebView通过html开发界面
- Android 浏览网页:WebView 嵌入浏览器(浏览历史返回、自定义加载失败界面、支持缩放、获取标题栏)
- Android 浏览网页:WebView 嵌入浏览器(浏览历史返回、自定义加载失败界面、支持缩放、获取标题栏)
- Android实现点击WebView界面中图片滑动浏览与保存图片功能
- 【Android】Web开发之使用WebView控件展示Web页面
- Android WebView 开发 资源释放,处理详解
- 【Android游戏开发之二】剖析游戏开发用view还是surfaceView ?!
- Android开发如何正确使用WebView