cocos2d-x 屏幕适配方案


手机游戏开发不可跨越的障碍就是多分辨率下屏幕如何适配。

网上已经有很多中方式了,我这里简单把现在项目中用到的方法记录下来


1:cocos2d-x自带一个函数,设置屏幕适配方式,不过由于从横比的关系,总会有黑边,或截取的情况发生。


2:采用多种资源配置是最不可取的方式


3:通过计算多种分辨率的共同可视区域,将关键,重要的UI放在这个共同可视区域中也是一种不错的方法。


下面就是我采用的方法,在写UI过程中通过缩放,进行适配。


关键的时在程序启动时,就获取设备的各项重要参数。以苹果4的分辨率为基准值


头文件:

class ScreenUtil
{
public:

	const static int STANDARD_SCREEN_WIDTH = 960;			// 屏幕的标准宽度值
	const static int STANDARD_SCREEN_HEIGHT = 640;			// 屏幕的标准高度值

	const static int STANDARD_SCREEN_HALF_WIDTH = 480;		// 屏幕的标准宽度值一半
	const static int STANDARD_SCREEN_HALF_HEIGHT = 320;		// 屏幕的标准高度值一半


	static float screenWidth;						// 屏幕宽度
	static float screenHeight;						// 屏幕高度

	static float screenHalfWidth;					// 屏幕宽度一半
	static float screenHalfHeight;					// 屏幕高度一半

	static float scaleX;							// X上的放大系数
	static float scaleY;							// Y上的放大系数

	static float standardScale;						// 标准放大系数

	static float radio;								// 对角角度

public:
	static void init();
    static bool SCREEN_IPHONE5();
    static bool SCREEN_IPAD();
};

cpp文件:

/**
 * 屏幕宽高属性
 */
float ScreenUtil::screenWidth = 960.0;
float ScreenUtil::screenHeight = 640.0;

float ScreenUtil::screenHalfWidth = 480.0;
float ScreenUtil::screenHalfHeight = 320.0;

/**
 * x,y上的分辨放大系数
 */
float ScreenUtil::scaleX = 1.0;
float ScreenUtil::scaleY = 1.0;

/**
 * 对角角度
 */
float ScreenUtil::radio = 0.0;

/**
 * 屏幕的标准放大系数
 */
float ScreenUtil::standardScale = 1.0;

void ScreenUtil::init()
{
	CCDirector *pDirector = CCDirector::sharedDirector();
	// 获取屏幕分辨率属性
	CCSize size = pDirector->getWinSizeInPixels();
	screenWidth = size.width;
	screenHeight = size.height;

	screenHalfWidth = screenWidth / 2;
	screenHalfHeight = screenHeight / 2;

	// 根据屏幕属性比例,计算比值
	scaleX = (float) screenWidth / STANDARD_SCREEN_WIDTH;
	scaleY = (float) screenHeight / STANDARD_SCREEN_HEIGHT;

    // 设置放大倍数
	if (scaleX > scaleY)
	{
		standardScale = scaleY;
	}
	else
	{
		standardScale = scaleX;
	}

	// 设置对角角度
	radio = atan(screenHalfWidth / screenHalfHeight) * 180 / M_PI;

	CCLOG("ScreenUtil::  scaleX = %f, scaleY = %f, standScale = %f", scaleX, scaleY, standardScale);
}

bool ScreenUtil::SCREEN_IPHONE5()
{
    if(screenWidth == 1136 && screenHeight == 640)
    {
        return true;
    }
    return false;
}

bool ScreenUtil::SCREEN_IPAD()
{
    if((screenWidth == 1024 && screenHeight == 768) ||
       (screenWidth == 1024 * 2 && screenHeight == 768 * 2))
    {
        return true;
    }
    return false;
}

示例:比如某个UI。

CCMenuItemSprite* item = CCMenuItemSprite::create(s0, s1, s2, handler, sel);

	if(fitWinSize)
	{
		// Place the image on the position of the screen
		float x = position.x * ScreenUtil::scaleX;
		float y = position.y * ScreenUtil::scaleY;
		item->setPosition(ccp(x, y));

		// set the scale by screenUtil
		item->setScale(ScreenUtil::standardScale);
	}
	else
	{
		item->setPosition(position);
	}

若是全屏的背景图之类的可以采用X,Y方向的缩放系数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值