CocoStudio:ImageView分析

CocoStudio:ImageView分析,第1张

概述在使用CocoStudio做界面时,会大量使用到图片控件(ImageView),我们这个来分析一下ImageView的使用:1、在CocoStudio中,对于图片控件,我们可以通过把模式调整到Custom,来使用九宫格的方法,在Auto模式下是不能使用九宫格的。2、导出json文件中ImageView的配置:{ "classname": "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);}
总结

以上是内存溢出为你收集整理的CocoStudio:ImageView分析全部内容,希望文章能够帮你解决CocoStudio:ImageView分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存