Chinaunix首页 | 论坛 | 博客
  • 博客访问: 476913
  • 博文数量: 280
  • 博客积分: 337
  • 博客等级: 二等列兵
  • 技术积分: 1957
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-17 21:36
文章分类

全部博文(280)

文章存档

2017年(13)

2016年(38)

2015年(78)

2014年(67)

2013年(70)

2012年(14)

分类: C/C++

2013-05-10 13:48:27

1.确认AndroidManifest.xml中相应的activity设置了orientation, 如

点击(此处)折叠或打开

  1. <activity android:name=\".Hello\"
  2.                   android:label=\"@string/app_name\"
  3.                   android:screenOrientation=\"landscape\"    
  4.                   android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"
  5.                   android:configChanges=\"orientation\">
  6.             <intent-filter>
  7.                 <action android:name=\"android.intent.action.MAIN\" />
  8.                 <category android:name=\"android.intent.category.LAUNCHER\" />
  9.             </intent-filter>
  10.         </activity>

2.修改 主activity类的java代码,添加修改屏幕方向的接口:

点击(此处)折叠或打开

  1. package com.gaofeng.game;

  2. import org.cocos2dx.lib.Cocos2dxActivity;

  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;

  6. public class Hello extends Cocos2dxActivity{

  7.     //added to save activity instance
  8.     static Hello myClass;
  9.     
  10.     protected void onCreate(Bundle savedInstanceState){
  11.         super.onCreate(savedInstanceState);
  12.         
  13.         //added to set activity instance
  14.         myClass = this;

  15.     }
  16.     
  17.     static {
             System.loadLibrary("game");
        }
        
        //interface added to be called by c language, return 1 success, 0 failed
        public static int changeOrientation(int orientation)
        {
            myClass.setRequestedOrientation(orientation);
            if(myClass.getRequestedOrientation() == orientation)
            {
                return 1;
            }
            return 0;
        }
    }


3.修改jni接口的main文件,加入调用java来修改orientation的c接口(如果c调java的地方多,可以考虑另写文件,这里只一个接口,main里有jni.h的包含比较方便)

点击(此处)折叠或打开

  1. #include \"AppDelegate.h\"
  2. #include \"platform/android/jni/JniHelper.h\"
  3. #include <jni.h>
  4. #include <android/log.h>

  5. #include \"HelloWorldScene.h\"
  6. #include <cstring>
  7. #include <cstdio>

  8. #define LOG_TAG \"main\"
  9. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

  10. using namespace cocos2d;

  11. extern \"C\"
  12. {

  13. JNIEnv *jniEnv = 0;
  14. JavaVM *jniVm = 0;
  15.   
  16.  CCSize orientationSize[2] = {{0, 0}, {0, 0}};
  17.  
  18. jint JNI_OnLoad(JavaVM *vm, void *reserved)
  19. {
  20.     //save vm, not needed
  21.     jniVm = vm;
  22.     
  23.     JniHelper::setJavaVM(vm);

  24.     return JNI_VERSION_1_4;
  25. }

  26. void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
  27. {
  28.     jniEnv = env;
  29.     
  30.     if (!CCDirector::sharedDirector()->getOpenGLView())
  31.     {
  32.         CCEGLView *view = CCEGLView::sharedOpenGLView();
  33.         view->setFrameSize(w, h);
  34.         
  35.         //save the orientation values, orientationSize[0] save the landscape value and [1] save portrait here
  36.         //u should adjust codes here according to your project
  37.         do{
  38.             orientationSize[0].width = w;
  39.             orientationSize[0].height = h;
  40.             orientationSize[1].width = h;
  41.             orientationSize[1].height = w;
  42.         }while(0);
  43.         
  44.         AppDelegate *pAppDelegate = new AppDelegate();
  45.         CCApplication::sharedApplication()->run();
  46.     }
  47.     else
  48.     {
  49.         ccDrawInit();
  50.         ccGLInvalidateStateCache();
  51.         
  52.         CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
  53.         CCTextureCache::reloadAllTextures();
  54.         CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
  55.         CCDirector::sharedDirector()->setGLDefaultValues();
  56.     }
  57. }

  58. int changeOrientation(int orientation)
  59. {
  60.     if(jniEnv)
  61.     {
  62.         //find main activity class
  63.         jclass cls = jniEnv->FindClass(\"com/gaofeng/game/Hello\");
  64.         
  65.         jmethodID mid = jniEnv->GetStaticMethodID(cls, \"changeOrientation\", \"(I)I\");
  66.         if(mid > 0)
  67.         {
  68.             if(jniEnv->CallStaticIntMethod(cls, mid, orientation))
  69.             {
  70.                 CCEGLView *view = CCEGLView::sharedOpenGLView();
  71.                 CCSize sz = view->getFrameSize();
  72.             
  73.                 //reset the framesize, if not, the framesize keep its pre values
  74.                  view->setFrameSize(orientationSize[orientation].width, orientationSize[orientation].height);
  75.                 return 1;
  76.             }
  77.         }
  78.     }
  79.     return 0;
  80. }


  81. }

4.需要切换横竖屏的地方调用changeOrientation
  我使用的例子是helloworld,只有一个场景,因为在游戏启动时调用applicationDidFinishLaunching()创建场景,包括
  了初始化类、设置精灵位置等行为,而重设屏幕方向并不会销毁场景重新创建,所以helloworld切换成竖屏时图片显
  示有问题。但实际需要切换横竖屏的时候往往要和切换场景一起进行,所以不会涉及这个问题。请以实际项目为准
  进行资源适配。

阅读(4515) | 评论(0) | 转发(0) |
0

上一篇:C++ class初始化

下一篇:android重启activity

给主人留下些什么吧!~~