加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x中ImageView的使用

发布时间:2020-12-14 16:53:29 所属栏目:百科 来源:网络整理
导读:?? Size size = Director::getInstance()-getVisibleSize(); ImageView* imageView = ImageView::create("ccicon.png"); imageView-setPosition(Vec2(size.width /2,size.height /2)); imageView-ignoreContentAdaptWithSize(false); imageView-setScale9Ena
??

Size size = Director::getInstance()->getVisibleSize();
ImageView* imageView = ImageView::create("ccicon.png");
imageView->setPosition(Vec2(size.width /2,size.height /2));
imageView->ignoreContentAdaptWithSize(false);
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(100,100));
imageView->setCapInsets(Rect(20,20,20));//设置伸缩后的内框大小
imageView->setTouchEnabled(true);
imageView->addTouchEventListener([=](Ref* sender,cocos2d::ui::Widget::TouchEventType type){
if (type == cocos2d::ui::Widget::TouchEventType::ENDED)
{
if (imageView->isScale9Enabled())
{
imageView->setScale9Enabled(false);
}
else
{
imageView->setScale9Enabled(true);
}

}
});

this->addChild(imageView);


//ImageView嵌套

ImageView* imageView = ImageView::create("cocosui/buttonHighlighted.png"); imageView->setScale9Enabled(true); imageView->setContentSize(Size(200,80)); imageView->setPosition(Vec2(widgetSize.width / 2.0f,widgetSize.height / 2.0f )); ImageView* imageViewChild = ImageView::create("cocosui/buttonHighlighted.png"); imageViewChild->setScale9Enabled(true); imageViewChild->setSizeType(Widget::SizeType::PERCENT); imageViewChild->setPositionType(Widget::PositionType::PERCENT); imageViewChild->setSizePercent(Vec2::ANCHOR_MIDDLE); imageViewChild->setPositionPercent(Vec2::ANCHOR_MIDDLE); imageViewChild->setPosition(Vec2(widgetSize.width / 2.0f,widgetSize.height / 2.0f)); ImageView* imageViewChild2 = ImageView::create("cocosui/buttonHighlighted.png"); imageViewChild2->setScale9Enabled(true); imageViewChild2->setSizeType(Widget::SizeType::PERCENT); imageViewChild2->setPositionType(Widget::PositionType::PERCENT); imageViewChild2->setSizePercent(Vec2::ANCHOR_MIDDLE); imageViewChild2->setPositionPercent(Vec2::ANCHOR_MIDDLE); imageViewChild->addChild(imageViewChild2); imageView->addChild(imageViewChild); imageView->setTouchEnabled(true); imageView->addTouchEventListener([=](Ref* sender,Widget::TouchEventType type){ if (type == Widget::TouchEventType::ENDED) { float width = CCRANDOM_0_1() * 200 + 50; float height = CCRANDOM_0_1() * 80 + 30; imageView->setContentSize(Size(width,height)); imageViewChild->setPositionPercent(Vec2(CCRANDOM_0_1(),CCRANDOM_0_1())); status->setString(StringUtils::format("child ImageView position percent: %f,%f",imageViewChild->getPositionPercent().x,imageViewChild->getPositionPercent().y)); } }); _uiLayer->addChild(imageView);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读