從開發iOS起地九切片技術的使用就不熟練,最近遇到的需求是要根據麻將牌的數量來適應背景圖片,下面就是有效的實現縮放的方法:
//9宮格圖片 //確定大小 ImageView *imageBg = ImageView::create("smallBG.png"); Rect fullRect = Rect(0,0, imageBg->getContentSize().width, imageBg->getContentSize().height); Rect insetRect = Rect(10,10,imageBg->getContentSize().width-20, imageBg->getContentSize().height-20); //創建九宮格精靈 auto sSprite = cocos2d::ui::Scale9Sprite::create("smallBG.png",fullRect, insetRect); sSprite->setAnchorPoint(Vec2(0, 0)); sSprite->setContentSize(Size(Vec2(allCardWidth + (m_iMaimaCnt) * 5 + 25, imageBg->getContentSize().height))); sSprite->setPosition(Vec2(30, 2));