最新 cocos2d-x自适应android不同分辨率,使用多套资源
2013-12-16 09:56
330 查看
虽然 cocos2d-x自带了三种模式来适应屏幕,但还是存在一些问题,会产生黑边,所以还是需要多套资源来适应屏幕。
这里我选择了四种分辨率9:16 3:5 2:3 3:4,在resource资源目录下新建了四个文件夹放置多套背景图片,在AppDelegate中添加如下函数调用即可。
[cpp] view
plaincopy
void AppDelegate::ScreenAdaptive()
{
CCSize szFrame = CCEGLView::sharedOpenGLView()->getFrameSize();
float Proportion = szFrame.width/szFrame.height;
const int num = 4;
float diff[num]={fabs(Proportion-(9.0/16)),
fabs(Proportion-(3.0/5)),fabs(Proportion-(2.0/3)),fabs(Proportion-(3.0/4))};
int yy = -1;
for (int i=0;i<num;i++)
if((diff[i] >-0.000001) && (diff[i] < 0.000001))
{
yy = i;
break;
}
if (yy == -1)
{
int min = diff[0];
yy = 0;
for (int i=1;i<num;i++)
if (diff[i]<min)
{
min = diff[i];
yy = i;
}
}
switch(yy)
{
case 0:
{
std::vector<string> searchPaths;
searchPaths.push_back("9x16");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(648,1152, kResolutionShowAll);
break;
}
case 1:
{
std::vector<string> searchPaths;
searchPaths.push_back("3x5");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,1040, kResolutionShowAll);
break;
}
case 2:
{
std::vector<string> searchPaths;
searchPaths.push_back("2x3");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640,960, kResolutionShowAll);
break;
}
case 3:
{
std::vector<string> searchPaths;
searchPaths.push_back("3x4");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,832, kResolutionShowAll);
break;
}
}
}
这里简单介绍一下setSearchPaths,在最近更新的版本里,已经没有了setResourceDirectory,取而代之的是setSearchPaths,以下是关于它的描述:
[cpp] view
plaincopy
/**
* Sets the array of search paths.
*
* You can use this array to modify the search path of the resources.
* If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array.
*
* @note This method could access relative path and absolute path.
* If the relative path was passed to the vector, CCFileUtils will add the default resource directory before the relative path.
* For instance:
* On Android, the default resource root path is "assets/".
* If "/mnt/sdcard/" and "resources-large" were set to the search paths vector,
* "resources-large" will be converted to "assets/resources-large" since it was a relative path.
*
* @param searchPaths The array contains search paths.
* @see fullPathForFilename(const char*)
* @since v2.1
*/
[cpp] view
plaincopy
// set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example"
vector<string> searchPaths;
searchPaths.push_back("/mnt/sd/example");
searchPaths.push_back("/data/data/org.cocos2dx.example");
CCFileUtils::setSearchPaths(searchPaths);
// engine will find "1.png" in /mnt/sd/example, if there it is not found, then engine will find "1.png" in /data/data/org.cocos2dx.example
// if not found, engine will find "1.png" in Resources/ (this path is platform dependent)
[cpp] view
plaincopy
CCSprite *pSprite = CCSprite::create("1.png");
这里我选择了四种分辨率9:16 3:5 2:3 3:4,在resource资源目录下新建了四个文件夹放置多套背景图片,在AppDelegate中添加如下函数调用即可。
[cpp] view
plaincopy
void AppDelegate::ScreenAdaptive()
{
CCSize szFrame = CCEGLView::sharedOpenGLView()->getFrameSize();
float Proportion = szFrame.width/szFrame.height;
const int num = 4;
float diff[num]={fabs(Proportion-(9.0/16)),
fabs(Proportion-(3.0/5)),fabs(Proportion-(2.0/3)),fabs(Proportion-(3.0/4))};
int yy = -1;
for (int i=0;i<num;i++)
if((diff[i] >-0.000001) && (diff[i] < 0.000001))
{
yy = i;
break;
}
if (yy == -1)
{
int min = diff[0];
yy = 0;
for (int i=1;i<num;i++)
if (diff[i]<min)
{
min = diff[i];
yy = i;
}
}
switch(yy)
{
case 0:
{
std::vector<string> searchPaths;
searchPaths.push_back("9x16");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(648,1152, kResolutionShowAll);
break;
}
case 1:
{
std::vector<string> searchPaths;
searchPaths.push_back("3x5");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,1040, kResolutionShowAll);
break;
}
case 2:
{
std::vector<string> searchPaths;
searchPaths.push_back("2x3");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640,960, kResolutionShowAll);
break;
}
case 3:
{
std::vector<string> searchPaths;
searchPaths.push_back("3x4");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,832, kResolutionShowAll);
break;
}
}
}
这里简单介绍一下setSearchPaths,在最近更新的版本里,已经没有了setResourceDirectory,取而代之的是setSearchPaths,以下是关于它的描述:
[cpp] view
plaincopy
/**
* Sets the array of search paths.
*
* You can use this array to modify the search path of the resources.
* If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array.
*
* @note This method could access relative path and absolute path.
* If the relative path was passed to the vector, CCFileUtils will add the default resource directory before the relative path.
* For instance:
* On Android, the default resource root path is "assets/".
* If "/mnt/sdcard/" and "resources-large" were set to the search paths vector,
* "resources-large" will be converted to "assets/resources-large" since it was a relative path.
*
* @param searchPaths The array contains search paths.
* @see fullPathForFilename(const char*)
* @since v2.1
*/
[cpp] view
plaincopy
// set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example"
vector<string> searchPaths;
searchPaths.push_back("/mnt/sd/example");
searchPaths.push_back("/data/data/org.cocos2dx.example");
CCFileUtils::setSearchPaths(searchPaths);
// engine will find "1.png" in /mnt/sd/example, if there it is not found, then engine will find "1.png" in /data/data/org.cocos2dx.example
// if not found, engine will find "1.png" in Resources/ (this path is platform dependent)
[cpp] view
plaincopy
CCSprite *pSprite = CCSprite::create("1.png");
相关文章推荐
- cocos2d-x自适应android不同分辨率,使用多套资源
- cocos2d-x自适应android不同分辨率,使用多套资源
- cocos2d-x-2.1项目android自适应不同分辨率
- Android不同分辨率屏幕下自适应资源文件
- Android不同分辨率屏幕下自适应资源文件 .
- Android不同手机屏幕分辨率自适应
- Android手机在不同分辨率情况下字体自适应大小
- Android手机在不同分辨率情况下字体自适应大小
- Android 打包, 不同渠道使用 不同的资源文件
- Android自适应不同分辨率或不同屏幕大小
- Android自适应不同分辨率或不同屏幕大小
- Android 使用正则表达式来快速生成多套dpi及分分辨率适配的UI
- Android自适应不同分辨率或不同屏幕大小的layout布局(横屏|竖屏)
- Android 不同分辨率下字体自适应大小
- Android不同手机屏幕分辨率自适应
- Android自适应不同分辨率的屏幕
- cocos2d-x项目在不同平台设置所使用的资源目录(多个Resources文件夹)
- Android不同手机屏幕分辨率自适应
- Android不同手机屏幕分辨率自适应
- Android基础——使用Fragment适应不同屏幕和分辨率