1 ImageView * imageView = ImageView::create("cocosui/ccicon.png");//新建图片 2 3 imageView->setScale9Enabled(true); //是否启用九宫格显示模式 4 5 imageViewChild->setSizeType(Widget::SizeType::PERCENT);//设置大小类型 6 imageViewChild->setSizePercent(Vec2::ANCHOR_MIDDLE);//设置大小比例 7 imageViewChild->setPositionType(Widget::PositionType::PERCENT);//设置位置类型 8 imageViewChild->setPositionPercent(Vec2::ANCHOR_MIDDLE);//设置位置比例 9 imageViewChild->setPosition(Vec2(widgetSize.width / 2.0f, 10 widgetSize.height / 2.0f));//设置位置 11 imageView->addTouchEventListener([=](Ref * sender, Widget::TouchEventType type){ 12 13 if (type == Widget::TouchEventType::ENDED){ 14 15 float width = CCRANDOM_0_1() * 200 + 50; 16 17 float height = CCRANDOM_0_1() * 80 + 30; 18 19 imageVIew->setContentSize(Size(width, height)); 20 21 imageViewChild->setPositionPercent(Vec2(CCRANDOM_0_1(), CCRANDOM_0_1()); 22 23 } 24 25 }); 26 27 imageVIew->setColor(Color3B(159, 168, 187); //设置图片颜色 28 29 imageVIew->setContentSize(Size(250, 115)); //设置图片大小 30 31 imageView->setFilppedX(true); //在X轴上翻转 32 33 imageView->setScale(0.5); //设置图片放大系数,当前系数+1 34 35 imageView->ignoreContentAdaptWithSize(false); //如果设置为true,忽略内容适应,使用系统默认的渲染大小,与实际的图片资源大小无关 36 37