Cocos2dx-android screen Adaptation Scheme

Source: Internet
Author: User

This article is reproduced in http://www.cnblogs.com/zisou/p/cocos2d-xJIqiao3.html

I'm just going to sigh. ~~android various resolutions various adaptation abuse me times, every new project I still treat it like first love

Each company has its own project engineering adaptation program, this thing is not the best, only the most suitable!!!

This new project specifically targeted Android, the purpose of strong, suitable for the program I think 2 sets of maps to compatible with the various resolutions of Android;

Let's look at the screen density on your Android phone:

Android mainly has the following screens:

QVGA and WQVGA screen density=120;

HVGA screen density=160;

WVGA screen density=240 .... More density values indicate how many display points per inch;

And the resolution is not the same, most of the applications can go through the screen density, then the game can also be similar to take this route;

But now the emergence of ultra-high-definition screen, such as Xiaomi, Samsung a little more high-end a new model of the resolution are very high to achieve FHD;

The FHD level is what we call the screen pixel reached the 1920*1080p format, that is, Full HD screen abbreviation, to adapt to this screen to be individually adapted;

No more nonsense, just go straight to the solution:

Idea 1: Background adaptation, then add part of the UI to the background to the original method. Solve the background adaptation first;
Idea 2: According to the screen size, to adapt to the "screen window UI original", does not belong to any of the original directly add to Cclayer;

The code is as follows:

First set up a Visiblerect class to get the screen size through the Cceglview, and then take eight points as a static method and then directly as the target location use

#ifndef __visiblerect_h__#define __visiblerect_h__#include "Cocos2d.h" Using_ns_cc;class visiblerect{public:    Static Ccrect Getvisiblerect ();    Static Ccpoint Left ();    Static Ccpoint right ();    Static Ccpoint top ();    static Ccpoint bottom ();    Static Ccpoint Center ();    Static Ccpoint lefttop ();    Static Ccpoint righttop ();    Static Ccpoint Leftbottom ();    Static Ccpoint Rightbottom ();p rivate:    static void Lazyinit ();    Static Ccrect S_visiblerect;}; #endif/* __visiblerect_h__ */

VisibleRect.cpp

#include "VisibleRect.h" Ccrect visiblerect::s_visiblerect;void visiblerect::lazyinit () {if (s_ VisibleRect.size.width = = 0.0f && s_visibleRect.size.height = = 0.0f) {cceglview* Peglview = Cceglview::        Sharedopenglview ();        S_visiblerect.origin = Peglview->getvisibleorigin ();    S_visiblerect.size = Peglview->getvisiblesize ();    }}ccrect Visiblerect::getvisiblerect () {lazyinit (); Return Ccrectmake (s_visiblerect.origin.x, S_VISIBLERECT.ORIGIN.Y, S_visibleRect.size.width, S_ VisibleRect.size.height);}    Ccpoint Visiblerect::left () {lazyinit (); Return CCP (s_visiblerect.origin.x, S_VISIBLERECT.ORIGIN.Y+S_VISIBLERECT.SIZE.HEIGHT/2);}    Ccpoint visiblerect::right () {lazyinit (); Return CCP (S_visiblerect.origin.x+s_visiblerect.size.width, S_VISIBLERECT.ORIGIN.Y+S_VISIBLERECT.SIZE.HEIGHT/2);}    Ccpoint Visiblerect::top () {lazyinit (); Return CCP (S_VISIBLERECT.ORIGIN.X+S_VISIBLERECT.SIZE.WIDTH/2, s_visiblerect.origin.y+s_visiblerect.size.height);} CCPOint Visiblerect::bottom () {lazyinit (); Return CCP (S_VISIBLERECT.ORIGIN.X+S_VISIBLERECT.SIZE.WIDTH/2, S_VISIBLERECT.ORIGIN.Y);}    Ccpoint Visiblerect::center () {lazyinit (); Return CCP (S_VISIBLERECT.ORIGIN.X+S_VISIBLERECT.SIZE.WIDTH/2, S_VISIBLERECT.ORIGIN.Y+S_VISIBLERECT.SIZE.HEIGHT/2 );}    Ccpoint Visiblerect::lefttop () {lazyinit (); Return CCP (s_visiblerect.origin.x, s_visiblerect.origin.y+s_visiblerect.size.height);}    Ccpoint Visiblerect::righttop () {lazyinit (); Return CCP (S_visiblerect.origin.x+s_visiblerect.size.width, s_visiblerect.origin.y+s_visiblerect.size.height);}    Ccpoint Visiblerect::leftbottom () {lazyinit (); return s_visiblerect.origin;}    Ccpoint Visiblerect::rightbottom () {lazyinit (); Return CCP (S_visiblerect.origin.x+s_visiblerect.size.width, S_VISIBLERECT.ORIGIN.Y);}

With this tool class can do a lot of things;

Below we need to adapt to the background, the specific method is as follows:

ccsprite* Publicshowui::settagscale (ccsprite* tagsprite) {    float last_x,last_y;    float X2 = Tagsprite->getcontentsize (). width;    float Y2 = tagsprite->getcontentsize (). Height;    last_x = ((float) visiblerect::getvisiblerect (). size.width/x2);    Last_y = ((float) visiblerect::getvisiblerect (). size.height/y2);    Tagsprite->setscalex (last_x);    Tagsprite->setscaley (last_y);    return tagsprite;    }

To adapt to a variety of size resolution, a set of graphs is not enough, according to the needs of their own projects to make 2 sets of diagrams, I recommend the following resolution set diagram:

800*480 Set

1136*640 two sets

HD HD Third Set 1920 * 1080 at present the higher on the phone, it is said that more than 2000 of Samsung, you can set their own!

With these graphs, organize your own resource folders and then go to fit, the code is as follows:

/****************** * Get screen resolution * compute which set of resources to use based on resolution ******************/int publicshowui::getinch (void) {    int lastinch =- 1;    Ccsize winsizeinpixels = screensize;    if (winsizeinpixels.width>=800&&winsizeinpixels.width<=960)    {        Lastinch = INCH_1;// ihpone3.5 inch    }    else if (winsizeinpixels.width>960&&winsizeinpixels.width<=1136)    {        Lastinch = Inch_2;//ihpone4 inch and most android4 inches around the screen    }    else if (winsizeinpixels.width>1136&& winsizeinpixels.width<=1920)    {        lastinch = inch_max;//Ultra HD screen    }    else    {        Lastinch = inch_2;    }    return lastinch;} /****************** * Path to different sets of graphs according to the custom picture path *imgres format: Imgdir%d/imgname.png ******************/ccstring* Publicshowui:: Getresimgpath (char* imgres) {    sprintf (str, imgres, Getinch ());    return ccstring::create (str);}

Get screen resolution ScreenSize:

cceglview* Peglview = Cceglview::sharedopenglview ();pD Irector->setopenglview (Peglview); Ccsize screensize = Peglview->getframesize ();

Use:

View_room = new View_room (this, Publicshowui::getresimgpath (Img_room_background), 1, Visiblerect::center ());

The background map must be centered:

Visiblerect::center ()

OK above is my adaptation of the plan and ideas;

Below my 2 iOS simulator on the diagram, I deliberately again the background of the border to add a green line, to show the difference display full screen, and then use two sets of graphs to match!

The resolution for 3.5 inch iOS is as follows:

The 4-inch resolution is as follows:

Cocos2dx-android screen Adaptation Scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.