ImageView源码如下
#ifndef __UIIMAGEVIEW_H__
#define __UIIMAGEVIEW_H__
#include "ui/UIWidget.h"
NS_CC_BEGIN
namespace ui {
/**
* @js NA
* @lua NA
*/
class ImageView : public Widget//公有继承自Widget
{
DECLARE_CLASS_GUI_INFO
public:
/**
* Default constructor
*/
ImageView();
/**
* Default destructor
*/
virtual ~ImageView();
/**
* Allocates and initializes.
*/
static ImageView* create();
/**
* create a imageview
*
* @param imageFileName file name of texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*/
static ImageView* create(const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
//create,第一个参数image的名称,第二个参数文理的类型
/**
* Load texture for imageview.
*
* @param fileName file name of texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*///重新载入纹理
void loadTexture(const std::string& fileName,TextureResType texType = TextureResType::LOCAL);
/**
* Updates the texture rect of the ImageView in points.
* It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.
*///设置纹理的区域
void setTextureRect(const Rect& rect);
/**
* Sets if imageview is using scale9 renderer.
*
* @param able true that using scale9 renderer, false otherwise.
*///设置Scale9 Enable
void setScale9Enabled(bool able);
//<span style="white-space:pre"> </span>判断是否开启Scale9
bool isScale9Enabled();
/**
* Sets capinsets for imageview, if imageview is using scale9 renderer.
*
* @param capInsets capinsets for imageview
*///
void setCapInsets(const Rect &capInsets);
const Rect& getCapInsets();
//override "ignoreContentAdaptWithSize" method of widget.
virtual void ignoreContentAdaptWithSize(bool ignore) override;
//忽略大小
/**
* Returns the "class name" of widget.
*///返回类的名称
virtual std::string getDescription() const override;
<span style="white-space:pre"> </span>//返回虚拟纹理大小
virtual const Size& getVirtualRendererSize() const override;
virtual Node* getVirtualRenderer() override;
CC_CONSTRUCTOR_ACCESS:
//initializes state of widget.
virtual bool init() override;
virtual bool init(const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
protected:
virtual void initRenderer() override;
virtual void onSizeChanged() override;
virtual void updateTextureColor() override;
virtual void updateTextureOpacity() override;
virtual void updateTextureRGBA() override;
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
void imageTextureScaleChangedWithSize();
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void adaptRenderers() override;
protected:
bool _scale9Enabled;
bool _prevIgnoreSize;
Rect _capInsets;
Node* _imageRenderer;
std::string _textureFile;
TextureResType _imageTexType;
Size _imageTextureSize;
bool _imageRendererAdaptDirty;
};
}
NS_CC_END
#endif /* defined(__CocoGUI__ImageView__) */
//代码示例
//在HelloWorld.cpp中得init函数中添加
ImageView* imageView = ImageView::create("buttonHighlighted.png");
imageView->setScale9Enabled(true);
imageView->setSize(Size(300,150));
imageView->setPosition(Vec2(winSize.width / 2.0f, winSize.height / 2.0f));
addChild(imageView);