CocoStudio:ImageView分析
发布时间:2020-12-14 21:21:03 所属栏目:百科 来源:网络整理
导读:在使用CocoStudio做界面时,会大量使用到图片控件(ImageView),我们这个来分析一下ImageView的使用:1、在CocoStudio中,对于图片控件,我们可以通过把模式调整到Custom,来使用九宫格的方法,在Auto模式下是不能使用九宫格的。2、导出json文件中ImageView的
在使用CocoStudio做界面时,会大量使用到图片控件(ImageView), 我们这个来分析一下ImageView的使用: 1、 在CocoStudio中,对于图片控件,我们可以通过把模式调整到Custom, 来使用九宫格的方法,在Auto模式下是不能使用九宫格的。 2、 导出json文件中ImageView的配置: { "classname": "ImageView","name": null,"children": [],"options": { "__type": "ImageViewSurrogate:#EditorCommon.JsonModel.Component.GUI","classname": "ImageView","name": "Image_9","classType": "CocoStudio.EngineAdapterWrap.CSImageView","fileNameData": { "path": "dd/04.png",//图片路径 "plistFile": "","resourceType": 0 },"scale9Enable": false,"scale9Height": 43,"scale9Width": 43 } } 3、 ImageViewReader类,解析上面ImageView生成json对象的类,最终要的一个函数: void ImageViewReader::setPropsFromJsonDictionary(ui::Widget *widget,const rapidjson::Value &options) { ..... //jsonPath json文件存放路径,不包括json文件名,只有路径 std::string jsonPath = GUIReader::shareReader()->getFilePath(); ui::ImageView* imageView = (ui::ImageView*)widget; //resourceType:使用的资源类型,如两种:单独图片文件或者打包成.plist的图片文件 // const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options,"fileNameData"); int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic,"resourceType"); switch (imageFileNameType) { case 0: //单独图片 { /* "fileNameData": { "path": "dd/04.png",//图片名 "plistFile": "","resourceType": 0 },*/ std::string tp_i = jsonPath; //获取资源图片path,并加上jsonPath路径,组成最终的图片全路径; //所以这里就有一个限制,就是在工程资源中,图片文件一定要和json文件放到同一个目录。 //我们可以通过修改代码的方式屏蔽这个问题。 const char* imageFileName = DICTOOL->getStringValue_json(imageFileNameDic,"path"); const char* imageFileName_tp = NULL; if (imageFileName && (strcmp(imageFileName,"") != 0)) { imageFileName_tp = tp_i.append(imageFileName).c_str(); imageView->loadTexture(imageFileName_tp); //这个函数很重要,后面分析 } break; } case 1: //打包的图片 { /* "fileNameData": { "path": "actor_attrFontNormal.png","plistFile": "hero.plist","resourceType": 1 },*/ const char* imageFileName = DICTOOL->getStringValue_json(imageFileNameDic,"path"); //注意:这里imageFileName只是图片path,并没有加上json文件路径 imageView->loadTexture(imageFileName,ui::UI_TEX_TYPE_PLIST); break; } default: break; } //九宫格相关 bool scale9EnableExist = DICTOOL->checkObjectExist_json(options,"scale9Enable"); bool scale9Enable = false; if (scale9EnableExist) { scale9Enable = DICTOOL->getBooleanValue_json(options,"scale9Enable"); } imageView->setScale9Enabled(scale9Enable); if (scale9Enable) { ...... imageView->setCapInsets(CCRectMake(cx,cy,cw,ch)); } WidgetReader::setColorPropsFromJsonDictionary(widget,options); } 4、上面有一个loadTexture函数,这个函数很重要: loadTexture函数是ImageView类的成员函数: 参数: fileName:图片文件名 texType:文件类型,按照我们上面的分析,总共有两种 typedef enum { UI_TEX_TYPE_LOCAL,UI_TEX_TYPE_PLIST }TextureResType; void ImageView::loadTexture(const char *fileName,TextureResType texType) { if (!fileName || strcmp(fileName,"") == 0) { return; } _textureFile = fileName; _imageTexType = texType; switch (_imageTexType) { case UI_TEX_TYPE_LOCAL://单个图片文件 if (_scale9Enabled) 如果使用九宫格,则使用CCScale9Sprite* { extension::CCScale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE; imageRendererScale9->initWithFile(fileName); imageRendererScale9->setCapInsets(_capInsets); } else //如果不是九宫格,则使用CCSprite* { //#define STATIC_CAST_CCSPRITE static_cast<CCSprite*>(_imageRenderer) //其实ImageView就是包含了一个CCSprite*成员变量_imageRenderer, //真正显示图片的是这个成员变量。 /* bool CCSprite::initWithFile(const char *pszFilename) { CCAssert(pszFilename != NULL,"Invalid filename for sprite"); //这里会根据我们传进来的图片路径,去加载纹理。 CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename); if (pTexture) { CCRect rect = CCRectZero; rect.size = pTexture->getContentSize(); return initWithTexture(pTexture,rect); } // don't release here. // when load texture failed,it's better to get a "transparent" sprite than a crashed program // this->release(); return false; } */ CCSprite* imageRenderer = STATIC_CAST_CCSPRITE; imageRenderer->initWithFile(fileName); } break; case UI_TEX_TYPE_PLIST://.plist图片文件 if (_scale9Enabled) { extension::CCScale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE; imageRendererScale9->initWithSpriteFrameName(fileName); imageRendererScale9->setCapInsets(_capInsets); } else { //如果是.plist格式的图片文件,则使用initWithSpriteFrameName方法初始化 //使用这种方法就需要提前把.plist文件加载进来。 /* bool CCSprite::initWithSpriteFrameName(const char *pszSpriteFrameName) { CCAssert(pszSpriteFrameName != NULL,""); CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName); return initWithSpriteFrame(pFrame); } */ CCSprite* imageRenderer = STATIC_CAST_CCSPRITE; imageRenderer->initWithSpriteFrameName(fileName); } break; default: break; } _imageTextureSize = _imageRenderer->getContentSize(); imageTextureScaleChangedWithSize(); updateAnchorPoint(); updateFlippedX(); updateFlippedY(); updateRGBAToRenderer(_imageRenderer); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |