这篇文章是描述怎么样在iosandroid上设置设备的方向。

正如你所知道的,有两种方法来设置设备方向:

1. OpenGL / Cocos2d-x方式

这种方式很快,但是不会旋转UIKit对象(ios)或者小部件(android)。

2. ViewController方式(IOS)或者设置xmlandroid

这种方法有一点慢,但是UIKit对象(IOS)或者小部件(android)就会正确的位置和方向。

openGL方式

你可以像下面那样设置设备方向:

CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);

这种方式只能旋转cocos2d引擎渲染的对象,例如精灵或者标签。

ViewController方式(IOS

让设备方向检测起作用可以像下面这样:

告诉系统那些方向是我们需要的支持(RootViewController.mm

  • // Override to allow orientations other than the default landscape orientation.
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  • return UIInterfaceOrientationIsLandscape( interfaceOrientation );
  • }

openGL渲染的view替换root view controllerviewAppController.mm

  • // Use RootViewController manage EAGLView
  • viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
  • viewController.wantsFullScreenLayout = YES;
  • viewController.view = __glView;

这个代码是使用cocos2d-x实现的,可以作为IOS的一个范例。

通过AndroidManifest.xml设置设备的方向

  • <application android:label="@string/app_name" android:debuggable="true" android:icon="@drawable/icon">
  • <activity android:name=".ApplicationDemo"
  • 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>
  • </application>

总结

1. 假如想要在ios中使用OpenGL的方式。那么就不要使用了RootViewController

2. 假如想要在Android中使用OpenGL的方式,那么在AndroidManifest.xml中设置设备方向为竖直(portrait)。

3. 假如你不想要使用任何的UIkit的东西(ios)或者小部件(android),那么建议使用OpenGL的方式,这样最快。

支持多方向

以下设置多方向的方式是适用于cocos2d-x 2.0.2所创建的工作空间。

这种补丁可能不会在以后的cocos2d-x所支持,或者部分被弃用。

IOS

1.注释掉cocos2dx/platform/ios/CCEGLView.mm::setContentScaleFactor(…)中的assert(m_eResolutionPolicy kResolutionUnKnown);

2.注释掉cocos2dx/platform/CCEGLViewProtocol.cpp::setDesignResolutionSize(…)中的CCAssert(m_bIsRetinaEnabled false, “can not enable retina while set design resolution size!”);

3.shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation的方法里面返回true

4.(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation中增加下面的代码:

  • CGSize s;
  • if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation)) {
  • s = CGSizeMake(std::max<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height), std::min<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
  • } else {
  • s = CGSizeMake(std::min<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height), std::max<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
  • }
  • cocos2d::CCDirector* director = cocos2d::CCDirector::sharedDirector();
  • director->enableRetinaDisplay(false);
  • director->getOpenGLView()->setFrameSize(s.width, s.height);
  • director->getOpenGLView()->setDesignResolutionSize(s.width, s.height, kResolutionShowAll);
  • director->enableRetinaDisplay(true);

Android

1. nativeInit(w, h);添加到cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxRenderer.java -> void onSurfaceChanged(GL10 gl, int w, int h).中。

2. 把下面的代码添加到void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h):

  • cocos2d::CCEGLView* view = cocos2d::CCDirector::sharedDirector()->getOpenGLView();
  • if (!view) {
  • ...
  • } else {
  • ...
  • if (view->getFrameSize().width != w || view->getFrameSize().height != h) {
  • view->setFrameSize(w, h);
  • view->setDesignResolutionSize(w, h, kResolutionShowAll);
  • }
  • }

更多相关文章

  1. Android 创建桌面快捷方式
  2. Android中屏幕的方向
  3. 将Android设备变为IBeacon设备
  4. android设备唯一码的获取,cpu号,mac地址
  5. Android中解析JSON的两种方式
  6. 【Android Developers Training】 74. 序言:通过无线连接设备
  7. python获取android设备的GPS信息脚本分享

随机推荐

  1. android Textview颜色渐变
  2. android用于打开各种文件的intent
  3. android PopupWindow 底部阴影外部空间取
  4. Android(安卓)BaseExpandableListAdapter
  5. Android:横行滚动且隔行变色的ListView控
  6. Android屏幕点亮(常亮)及屏幕解锁和锁定
  7. Android(安卓)关于wifi管理的代码
  8. Android(安卓)proguard(混淆)模板
  9. Android快速开发之封装标题栏
  10. android 实现自定义隐藏式菜单