【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebView
16lz
2021-01-24
(转载请注明原文地址:http://blog.csdn.net/guggy/article/details/10435157)
在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
[java] view plain copy- publicLinearLayoutonCreateLayout(Cocos2dxGLSurfaceViewsurfaceView){
- LinearLayoutlayout=newLinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.addView(surfaceView);
- returnlayout;
- }
(2) 在this.mGLSurfaceView = this.onCreateView() 下面增加这一行: [java] view plain copy
- LinearLayoutcontentLayout=this.onCreateLayout(mGLSurfaceView);
(3) 应用的Activity文件实现如下, [java] view plain copy
- publicclassHelloCppextendsCocos2dxActivity{
- staticHelloCppsHelloCpp=null;
- LinearLayoutmContentLayout;
- Cocos2dxGLSurfaceViewmGlSurfaceView;
- LinearLayoutmWebLayout;
- WebViewmWebView;
- ButtonmBackButton;
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- }
- publicLinearLayoutonCreateLayout(Cocos2dxGLSurfaceViewsurfaceView){
- mGlSurfaceView=surfaceView;
- sHelloCpp=this;
- mContentLayout=newLinearLayout(this);
- mContentLayout.setOrientation(LinearLayout.VERTICAL);
- mContentLayout.addView(surfaceView);
- mWebLayout=newLinearLayout(this);
- mWebLayout.setOrientation(LinearLayout.VERTICAL);
- returnmContentLayout;
- }
- publicCocos2dxGLSurfaceViewonCreateView(){
- Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
- //TestCppshouldcreatestencilbuffer
- glSurfaceView.setEGLConfigChooser(5,6,5,0,16,8);
- returnglSurfaceView;
- }
- //此函数提供给jni调用,返回自身类的对象
- publicstaticHelloCppgetInstance(){//返回实例
- returnsHelloCpp;
- }
- publicvoidopenWebView(){
- this.runOnUiThread(newRunnable(){//在主线程里添加别的控件
- publicvoidrun(){
- //初始化webView
- mWebView=newWebView(HelloCpp.this);
- //设置webView能够执行javascript脚本
- mWebView.getSettings().setJavaScriptEnabled(true);
- //载入URL
- mWebView.loadUrl("file:///android_asset/index.html");
- //使页面获得焦点
- //mWebView.requestFocus();
- //如果页面中链接,如果希望点击链接继续在当前browser中响应
- mWebView.setWebViewClient(newWebViewClient(){
- publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
- if(url.indexOf("tel:")<0){
- view.loadUrl(url);
- }
- returntrue;
- }
- });
- /*初始化返回按钮*/
- mBackButton=newButton(HelloCpp.this);
- mBackButton.setLayoutParams(newLinearLayout.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(newOnClickListener(){
- publicvoidonClick(Viewv){
- removeWebView();
- mGlSurfaceView.setVisibility(View.VISIBLE);
- }
- });
- //把webView加入到线性布局
- mGlSurfaceView.setVisibility(View.GONE);
- mWebLayout.addView(mBackButton);
- mWebLayout.addView(mWebView);
- mContentLayout.addView(mWebLayout);
- }
- });
- }
- //移除webView把刚才加的所有控件都删掉
- publicvoidremoveWebView(){
- mContentLayout.removeView(mWebLayout);
- mWebLayout.destroyDrawingCache();
- mWebLayout.removeView(mWebView);
- mWebView.destroy();
- mWebLayout.removeView(mBackButton);
- mBackButton.destroyDrawingCache();
- }
- publicbooleanonKeyDown(intkeyCoder,KeyEventevent)//重载函数,android手机实体返回键回调函数
- {
- if(mWebView.canGoBack()&&keyCoder==KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView
- mWebView.goBack();
- }else{
- removeWebView();
- mGlSurfaceView.setVisibility(View.VISIBLE);
- }
- returnfalse;
- }
- static{
- System.loadLibrary("game");
- }
从cocos2d-x的界面中打开WebView的代码:
[cpp] view plain copy- #if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
- //getStaticMethodInfo,判断Java静态函数是否存在,并且把信息保存到minfo里
- //参数1:JniMethodInfo
- //参数2:Java类包名+类名
- //参数3:Java函数名称
- //参数4:函数参数类型和返回值类型,这里的返回值类型是HelloCpp类的对象。写法:L+包名+;其他的类型请看上面的“JNI详细教程”
- JniMethodInfominfo;
- jobjectjobj;
- boolisHave=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
更多相关文章
- C语言函数的递归(上)
- Android源码学习之五-Android的IPC机制
- Android之HttpURLConnection小结
- Windown类
- Android之使用PackageManager取得程序的包名、图标等
- 获取Android设备屏幕的相关参数
- Flutter 基础(二)基本组件ListView
- Android-NDK开发之基础--Android(安卓)JNI实例代码(四)-- JNI中
- android中的生命周期(新增2个函数)