cocos代碼研究(19)Widget子類ImageView學習筆記

來源:互聯網
上載者:User

標籤:

理論基礎

顯示圖片的小控制項,繼承自 Widget 。

代碼實踐

static ImageView * create()
建立一個空的ImageView

static ImageView * create(const std::string &imageFileName,//紋理的檔案名稱字。
TextureResType texType=TextureResType::LOCAL)
根據圖片名字建立一個ImageView。

void loadTexture (const std::string &fileName,//紋理的檔案名稱字。
TextureResType texType=TextureResType::LOCAL)
為ImageView對象載入紋理

void setTextureRect (const Rect &rect)
更新ImageView紋理矩形的點 它將調用setTextureRect:rotated:untrimmedSize,rotated預設為No,utrimmedSize預設為rect.size

void setScale9Enabled (bool enabled)
開啟scale9渲染模式

bool isScale9Enabled () const
查詢是否開啟了scale9渲染

void setCapInsets (const Rect &capInsets)
設定ImageView的capInsets 只會影響開啟了scale9渲染模式的ImageView setScale9Enabled(true)

const Rect & getCapInsets () const
擷取ImageView的capInsets尺寸

virtual void ignoreContentAdaptWithSize (bool ignore) override
設定是否忽略使用者定義通過setContentSize設定的控制項尺寸。 設定為true將忽略使用者定義的尺寸,意味著控制項尺寸永遠等於getVirtualRendererSize的傳回值。

執行個體:

// Create the imageviewImageView* imageView = ImageView::create("cocosui/ccicon.png");imageView->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));_uiLayer->addChild(imageView);
        // Create the imageview        ImageView* imageView = ImageView::create("cocosui/ccicon.png");        imageView->ignoreContentAdaptWithSize(false);        imageView->setScale9Enabled(true);        imageView->setContentSize(Size(100, 100));        imageView->setCapInsets(Rect(20,20,20,20));        imageView->setPosition(Vec2(widgetSize.width / 2.0f,            widgetSize.height / 2.0f));        imageView->setTouchEnabled(true);        imageView->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type){            if (type == Widget::TouchEventType::ENDED) {                if (imageView->isScale9Enabled())                {                    imageView->setScale9Enabled(false);                }                else                    imageView->setScale9Enabled(true);            }        });
        // Create the imageview        ImageView* imageView = ImageView::create("blocks9r.png", Widget::TextureResType::PLIST);        imageView->setScale9Enabled(true);        imageView->setContentSize(Size(250, 115));        imageView->setFlippedX(true);        imageView->setScale(0.5);        imageView->ignoreContentAdaptWithSize(false);        imageView->setPosition(Vec2(widgetSize.width / 2.0f,                                    widgetSize.height / 2.0f));

 

cocos代碼研究(19)Widget子類ImageView學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.