Android Binder處理序間通訊---註冊Service組件---Server處理BC_TRANSACTION

來源:互聯網
上載者:User

標籤:cocos2d-x3.0   不規則   按鈕   button   

這是我參照之前在iOS項目中用過的一個不規則形狀按鈕的第三方Button,這裡用Cocos2d-x實現一個相似功能的按鈕。

原文地址:http://blog.csdn.net/qqmcy/article/details/26161339

代碼下載:http://download.csdn.net/detail/qqmcy/7365843

使用方法:

.h

////  TestScene.h//  maptest////  Created by 杜甲 on 14-5-18.////#ifndef __maptest__TestScene__#define __maptest__TestScene__#include "cocos2d.h"#include "ui/CocosGUI.h"#include "DJShapeButton.h"USING_NS_CC;class TestScene :public Layer,public DJShapeButtonDelegate{    public:    static Scene* createScene();    virtual bool init();        CREATE_FUNC(TestScene);    void buttonCallBack(DJShapeButton* sender);    };#endif /* defined(__maptest__TestScene__) */



        DJShapeButton* djShapeBtn  = DJShapeButton::create("res/red/1300_r.png");        djShapeBtn->setDelegate(this);        //設定tag        djShapeBtn->setShapeBtnTag(1000);        djShapeBtn->setPosition(Point(0, 40));        addChild(djShapeBtn);


回呼函數
void TestScene::buttonCallBack(DJShapeButton *sender){    log("%d",sender->getTag());}



DJShapeButton.h不規則形狀Button類

////  DJShapeButton.h//  maptest////  Created by 杜甲 on 14-5-18.////#ifndef __maptest__DJShapeButton__#define __maptest__DJShapeButton__class DJShapeButton;class DJShapeButtonDelegate {    public:    virtual void buttonCallBack(DJShapeButton* sender) = 0;        };#include "cocos2d.h"#include "ui/CocosGUI.h"USING_NS_CC;class DJShapeButton :public ui::Layout{    public:    static DJShapeButton* create(const std::string& normalImage);        CC_SYNTHESIZE(DJShapeButtonDelegate*, _delegateBtn, Delegate);                virtual bool init(const std::string& normalImage);        CC_SYNTHESIZE_RETAIN(Sprite*,  shapeBtn, ShapeBtn);        void setShapeBtnTag(int tag);private:    bool isSwallow ;            };#endif /* defined(__maptest__DJShapeButton__) */

DJShapeButton.cpp

////  DJShapeButton.cpp//  maptest////  Created by 杜甲 on 14-5-18.////#include "DJShapeButton.h"DJShapeButton* DJShapeButton::create(const std::string &normalImage){    DJShapeButton *pRet = new DJShapeButton();    if (pRet && pRet->init(normalImage))    {        pRet->autorelease();        return pRet;    }    else    {        delete pRet;        pRet = NULL;        return NULL;    }}bool DJShapeButton::init(const std::string &normalImage){    bool bRet = false;    do {        CC_BREAK_IF(!ui::Layout::init());                                Image* myImg = new Image();        myImg->initWithImageFile(normalImage);                                        Texture2D* temp = new Texture2D();        temp->initWithImage(myImg);                shapeBtn =Sprite::createWithTexture(temp);                log("%f,%f",shapeBtn->getAnchorPoint().x ,shapeBtn->getAnchorPoint().y);        log("width = %f,height = %f",shapeBtn->getContentSize().width , shapeBtn->getContentSize().height);                addChild(shapeBtn);                                this->setSize(shapeBtn->getContentSize());        shapeBtn->setPosition(Point(getSize().width / 2, getSize().height / 2));                auto listener1 = EventListenerTouchOneByOne::create();                listener1->onTouchBegan = [=](Touch* touch, Event* event)        {            auto target = static_cast<Sprite*>(event->getCurrentTarget());            Point locationInNode = target->convertToNodeSpace(touch->getLocation());            Color4B c = {0,0,0,0};                        Point pt = Point(locationInNode.x, target->getContentSize().height - locationInNode.y);                                                Size s = target->getContentSize();            Rect rect = Rect(0, 0, s.width, s.height);                        if (rect.containsPoint(locationInNode)) {                log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);                                                                unsigned int x = pt.x, y = pt.y;                unsigned char* data = myImg->getData();                unsigned int* pixel = (unsigned int*)data;                pixel = pixel + (y * 116) + x;                c.r = *pixel & 0xff;                c.g = (*pixel >> 8 ) & 0xff;                c.b = (*pixel >> 16) & 0xff;                c.a = (*pixel >> 24) & 0xff;                log("8888888888%d",c.a);                if (c.a <= 4) {                    isSwallow = false;                }else                {                    isSwallow = true;                                    }                            }else{                isSwallow = false;            }                        listener1->setSwallowTouches(isSwallow);                        return isSwallow;        };                listener1->onTouchEnded =  [=](Touch* touch, Event* event)        {            if (_delegateBtn != nullptr) {                _delegateBtn->buttonCallBack(this);            }        };                                _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, shapeBtn);                                                bRet = true;    } while (0);    return bRet;}void DJShapeButton::setShapeBtnTag(int tag){    shapeBtn->setTag(tag);    setTag(tag);    }



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.