本站文章均为李华明Himi原创,转载务必在明显处注明:
转载自【黑米GameDev街区】原文链接:http://www.himigame.com/android-game/799.html

          ☞ 点击订阅 ☜
本博客最新动态!及时将最新博文通知您!

本篇主要介绍Cocos2dx项目开发过程中或者说项目务必遇到的一些知识点(ps.貌似Himi博客写的都是务必的Himi认为写别人没写的才更容易吸引人不是~)

OK,不多说废话,第一个介绍的是修改项目配置让你的Android项目支持自适应屏幕;其实关于Android项目自适应屏幕这个问题,Himi实在不想再多费口舌,一方面因为Himi之前博文有说过,另外一方面现在Android开源缘故造成分辨率泛滥也成必然。大家注意做项目尽可能使用相对位置,别写死坐标,另外一点就是针对流行分辨率做适应就好了,如果你们公司很有必要铺Android市场的量,那么只能一个一个分辨率去搞了=。 = Himi身为Kjava(J2me)一路走过来的Dev来说,我是在是对自适应虐到习惯…..

1. 咳咳,本不想说,回到正题,那么对于Cocos2dx中如何设置项目Android版自适应,其实很easy,直接在编译好的Android项目中如下路径查找:

your Project name/Jni/helloworld/main.cpp

OK,找到main.cpp后双击打开,然后看到如下代码段:

1 2 // if you want to run in WVGA with HVGA resource, set it view->create( 480 , 320 ); // Please change it to (320, 480);if you're in portrait mode.

view->create(480,320);默认关闭的,这里打开即可;其实Himi也是从cocos2dx引擎框架中看到的,打开你的任意一个cocos2dx引擎框架的项目,然后打开AppDelegate.cpp 文件,就能看到:

2. 下面继续介绍如何让你的cocos2dx-Android项目设置缩放比例,一样很easy,设置代码如下:

1 CCDirector::sharedDirector()->setContentScaleFactor(2.0);

默认值是1.0,缩放2倍,从下面这两张图可以明显看出设置后的区别:(点击放大图片)

为了便于后续讲解更容易理解,那么这里Himi博文讲解使用的两行图片这里先给出,大家先看下:

rect.png 规格: 40*40 | rect-hd.png 规格:80*80

3.下面介绍如何让cocos2dx的Android版项目使用iOSRetina类似@2x的-hd功能也直接使用高清图片,当然cocos2dx引擎默认找的高清图为-hd;但是编译Xcode的cocos2dx项目到Android版后,Android版可不会那么聪明自动使用你的-hd的版图片,所以下面Himi来手把手教你设置;具体步骤如下:

3.1首先在你的项目下找到 CCEGLView_android.cpp ,双击打开:

找到 void CCEGLView::create(int width, int height) 函数,然后函数内替换成如下代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 void CCEGLView::create( int width, int height) { if (width == 0 || height == 0) { return ; } m_sSizeInPoint.width = width; m_sSizeInPoint.height = height; // calculate the factor and the rect of viewport m_fScreenScaleFactor = MIN(( float )m_sSizeInPixel.width / m_sSizeInPoint.width, ( float )m_sSizeInPixel.height / m_sSizeInPoint.height); CCLOG( "CCEGLView::Create / Screen Scale Factor = %f" , m_fScreenScaleFactor); if (m_fScreenScaleFactor >= 1.5f) { CCLOG( "CCEGLView::Create / HD Scale Factor => Increase Content Scale Factor" ); cocos2d::CCDirector::sharedDirector()->setContentScaleFactor(2.0f); } int viewPortW = ( int )(m_sSizeInPoint.width * m_fScreenScaleFactor); int viewPortH = ( int )(m_sSizeInPoint.height * m_fScreenScaleFactor); m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2; m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2; m_rcViewPort.size.width = viewPortW; m_rcViewPort.size.height = viewPortH; m_bNotHVGA = true ; }

3.2 继续在你的项目下找到CCFileUtils_android.cpp 类,双击打开:

找到 const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) 函数,然后替换如下内容:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 const char * CCFileUtils::fullPathFromRelativePath( const char *pszRelativePath) { if (CC_CONTENT_SCALE_FACTOR() == 2.0f) { //CC_RETINA_DISPLAY_FILENAME_SUFFIX // verifier si suffix deja present std::string path = pszRelativePath; std::string::size_type pos = path.rfind( "/" ) + 1; // the begin index of last part of path std::string::size_type suffixPos = path.rfind(CC_RETINA_DISPLAY_FILENAME_SUFFIX); if ((std::string::npos != suffixPos) && (suffixPos > pos)) { // => if yes, return path directly } else { // => if no, add "retina"/hd suffix and test if file exist CCString *pRet = new CCString(); pRet->autorelease(); pRet->m_sString = path.substr(0, path.rfind( "." )) + CC_RETINA_DISPLAY_FILENAME_SUFFIX + path.substr(path.rfind( "." ), path.length()); if (existFileData(pRet->m_sString.c_str())) { // => if yes, return path with suffix CCLog( "cocos2d: FilePath(%s) with suffix(%s) exist, use it." , pRet->m_sString.c_str(), CC_RETINA_DISPLAY_FILENAME_SUFFIX); return pRet->m_sString.c_str(); } else { // => if no, return path without suffix } } } return pszRelativePath; }

然后接着在本类添加如下两个函数:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 bool CCFileUtils::existFileData( const char * pszFileName) { string fullPath(pszFileName); if ((! pszFileName)) { return false ; } if (pszFileName[0] != '/' ) { // read from apk fullPath.insert(0, "assets/" ); return CCFileUtils::existFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str()); } else { do { // read rrom other path than user set it FILE *fp = fopen (pszFileName, "rb" ); if (fp != NULL) { fclose (fp); return true ; } } while (0); } return false ; } bool CCFileUtils::existFileDataFromZip( const char * pszZipFilePath, const char * pszFileName) { unzFile pFile = NULL; bool res = false ; do { CC_BREAK_IF(!pszZipFilePath || !pszFileName); CC_BREAK_IF( strlen (pszZipFilePath) == 0);

更多相关文章

  1. OSG for Android新手教程系列(三)——HelloWorld,第一个示例
  2. 利用Cordova对H5页面进行APP开发打包
  3. Android(安卓)程序读写Office文件
  4. Android(安卓)Studio如何取消与SVN的关联
  5. 关于Android中ANR的一些思考
  6. 移动端对html input标签文件选择支持
  7. Android小项目一:微信精选
  8. 项目总结-EMOJI表情处理详解(ios,android平台兼容)
  9. Android(安卓)6.0以上权限拒绝打开权限设置界面的解决方法

随机推荐

  1. android launcher使hotseat item显示标题
  2. Android 动态设置颜色
  3. Android Battery Dog开源项目介绍
  4. Android用属性动画拖动view到任意位置
  5. Android Studi3.1.4利用cmake搭建jni-Con
  6. android studio中安装parcelable插件
  7. 如何使用ProgressBar动态设置进度条渐变
  8. Android统一依赖多个不同版本的Support v
  9. android setting 設置<二>
  10. TabHost 切换带滑动及tab页的数据添加