1.确认AndroidManifest.xml中相应的activity设置了orientation, 如
-
<activity android:name=\".Hello\"
-
android:label=\"@string/app_name\"
-
android:screenOrientation=\"landscape\"
-
android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\"
-
android:configChanges=\"orientation\">
-
<intent-filter>
-
<action android:name=\"android.intent.action.MAIN\" />
-
<category android:name=\"android.intent.category.LAUNCHER\" />
-
</intent-filter>
-
</activity>
2.修改 主activity类的java代码,添加修改屏幕方向的接口:
-
package com.gaofeng.game;
-
-
import org.cocos2dx.lib.Cocos2dxActivity;
-
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.util.Log;
-
-
public class Hello extends Cocos2dxActivity{
-
-
//added to save activity instance
-
static Hello myClass;
-
-
protected void onCreate(Bundle savedInstanceState){
-
super.onCreate(savedInstanceState);
-
-
//added to set activity instance
-
myClass = this;
-
-
}
-
-
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的包含比较方便)
-
#include \"AppDelegate.h\"
-
#include \"platform/android/jni/JniHelper.h\"
-
#include <jni.h>
-
#include <android/log.h>
-
-
#include \"HelloWorldScene.h\"
-
#include <cstring>
-
#include <cstdio>
-
-
#define LOG_TAG \"main\"
-
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
-
-
using namespace cocos2d;
-
-
extern \"C\"
-
{
-
-
JNIEnv *jniEnv = 0;
-
JavaVM *jniVm = 0;
-
-
CCSize orientationSize[2] = {{0, 0}, {0, 0}};
-
-
jint JNI_OnLoad(JavaVM *vm, void *reserved)
-
{
-
//save vm, not needed
-
jniVm = vm;
-
-
JniHelper::setJavaVM(vm);
-
-
return JNI_VERSION_1_4;
-
}
-
-
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
-
{
-
jniEnv = env;
-
-
if (!CCDirector::sharedDirector()->getOpenGLView())
-
{
-
CCEGLView *view = CCEGLView::sharedOpenGLView();
-
view->setFrameSize(w, h);
-
-
//save the orientation values, orientationSize[0] save the landscape value and [1] save portrait here
-
//u should adjust codes here according to your project
-
do{
-
orientationSize[0].width = w;
-
orientationSize[0].height = h;
-
orientationSize[1].width = h;
-
orientationSize[1].height = w;
-
}while(0);
-
-
AppDelegate *pAppDelegate = new AppDelegate();
-
CCApplication::sharedApplication()->run();
-
}
-
else
-
{
-
ccDrawInit();
-
ccGLInvalidateStateCache();
-
-
CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
-
CCTextureCache::reloadAllTextures();
-
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
-
CCDirector::sharedDirector()->setGLDefaultValues();
-
}
-
}
-
-
int changeOrientation(int orientation)
-
{
-
if(jniEnv)
-
{
-
//find main activity class
-
jclass cls = jniEnv->FindClass(\"com/gaofeng/game/Hello\");
-
-
jmethodID mid = jniEnv->GetStaticMethodID(cls, \"changeOrientation\", \"(I)I\");
-
if(mid > 0)
-
{
-
if(jniEnv->CallStaticIntMethod(cls, mid, orientation))
-
{
-
CCEGLView *view = CCEGLView::sharedOpenGLView();
-
CCSize sz = view->getFrameSize();
-
-
//reset the framesize, if not, the framesize keep its pre values
-
view->setFrameSize(orientationSize[orientation].width, orientationSize[orientation].height);
-
return 1;
-
}
-
}
-
}
-
return 0;
-
}
-
-
-
}
4.需要切换横竖屏的地方调用changeOrientation
。
我使用的例子是helloworld,只有一个场景,因为在游戏启动时调用applicationDidFinishLaunching()创建场景,包括
了初始化类、设置精灵位置等行为,而重设屏幕方向并不会销毁场景重新创建,所以helloworld切换成竖屏时图片显
示有问题。但实际需要切换横竖屏的时候往往要和切换场景一起进行,所以不会涉及这个问题。请以实际项目为准
进行资源适配。
阅读(4515) | 评论(0) | 转发(0) |