1.设计分辨率
配资源使用的分辨率大小,是1334*750。
2.屏幕分辨率
实际上用户屏幕的分辨率大小,不确定。
这里是实现代码:
- //
- // FixUIUtils.h
- // MapTest
- //
- // Created by Alostz on 16/4/16.
- //
- //
- #ifndef FixUIUtils_h
- #define FixUIUtils_h
- #include "cocos2d.h"
- using namespace cocos2d;
- using namespace std;
- class FixUIUtils
- {
- public:
- FixUIUtils();
- ~FixUIUtils();
- static void init();
- static void setFixScale(Node *node);
- static void setScaleMin(Node* node);
- static void setScaleMax(Node* node);
- static void setRootNodewithFIXED(Node* node);
- static void fixScene(Node* node);
- static void fixUI(Node* node);
- };
- #endif /* FixUIUtils_h */
- //
- // FixUIUtils.cpp
- // MapTest
- //
- // Created by Alostz on 16/4/16.
- //
- //
- #include <FixUIUtils.h>
- static cocos2d::Size designSize = cocos2d::Size(750,1334);
- static cocos2d::Size screenSize;
- static float minScale;
- static float maxScale;
- static float xScale;
- static float yScale;
- FixUIUtils::FixUIUtils(){
- }
- FixUIUtils::~FixUIUtils(){
- }
- void FixUIUtils::init(){
- screenSize = Director::getInstance()->getVisibleSize();
- xScale = designSize.width / screenSize.width;
- yScale = designSize.height / screenSize.height;
- minScale = MIN(screenSize.height/designSize.height,screenSize.width/designSize.width);
- maxScale = MAX(screenSize.height/designSize.height,screenSize.width/designSize.width);
- log("xScale = %f * xyScale = %f \n screenSize.width = %f * screenSize.height = %f \n",xScale,yScale,screenSize.width,screenSize.height);
- }
- //1、先适配layout层到屏幕大小
- void FixUIUtils::setFixScale(Node *node){
- auto nodeX = node->getScaleX();
- auto nodeY = node->getScaleY();
- log("setFixScale nodeX = %f * nodeX = %f \n",nodeX,nodeY);
- nodeX = nodeX * xScale;
- nodeY = nodeY * yScale;
- log("setFixScale nodeX = %f * nodeX = %f \n",nodeY);
- node->setScaleX(nodeX);
- node->setScaleY(nodeY);
- }
- //屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(大)者作为宽、高的缩放因子。
- //适用于控件的缩放
- void FixUIUtils::setScaleMax(Node *node){
- auto nodeX = node->getScaleX();
- auto nodeY = node->getScaleY();
- log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);
- nodeX = nodeX * maxScale;
- nodeY = nodeY * maxScale;
- log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);
- node->setScaleX(nodeX);
- node->setScaleY(nodeY);
- }
- //屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(小)者作为宽、高的缩放因子。
- //适用于背景的缩放
- void FixUIUtils::setScaleMin(Node *node){
- auto nodeX = node->getScaleX();
- auto nodeY = node->getScaleY();
- log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);
- nodeX = nodeX * minScale;
- nodeY = nodeY * minScale;
- log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);
- node->setScaleX(nodeX);
- node->setScaleY(nodeY);
- }
- void FixUIUtils::setRootNodewithFIXED(Node* node){
- auto moveX = (designSize.width - screenSize.width);
- auto moveY = (designSize.height - screenSize.height);
- node->setPosition(Point(-moveX,-moveY));
- }
- void FixUIUtils::fixScene(Node* node){
- fixUI(node);
- FixUIUtils::setRootNodewithFIXED(node);
- }
- void FixUIUtils::fixUI(Node* node){
- Vector<Node*> ChildrenList = node->getChildren();
- for (Node* child: ChildrenList) {
- log("fixUI nodeX = %d \n",child->getTag());
- FixUIUtils::setScaleMin(child);
- }
- }
调用:
- //加载Cocos Studio编辑好的资源
- auto rootNode = CSLoader::createNode("test.csb");
- FixUIUtils::init();
- FixUIUtils::setFixScale(rootNode);
- FixUIUtils::fixScene(rootNode);
- this->addChild(rootNode);