Cocos2d-x 3.x使用第三方庫播放gif圖


Cocos2d-x 3.x使用第三方庫播放gif圖

 

效果圖:

testGif (1).gif

由於kd的項目中需要用到gif,而Cocos2d-x本身是不支持gif播放的。百度 + google 了很久,最終都指向:https://github.com/opentalking/gif-for-cocos2dx

這里,可惜一開始看到的時候是基於2.2的,由於對底層代碼不熟悉,根本沒辦法下手。於是聯系原作者,他一個周末就搞出個3.x的版本,大家可以去膜拜下:
地址:https://github.com/opentalking/gif-for-cocos2dx-3.x.git

 

在這里真心要為大神的無私分享精神點個贊~

 

使用方法:

pull下來后,把gif文件夾導入到項目中來,然后就可以像使用Sprite那樣使用gif~~~

1
2
3
4
5
6
7
8
9
10
std::string name =  "g2.gif" ;
name = FileUtils::getInstance() -> fullPathForFilename(name.c_str());
GifBase *gif = InstantGif::create(name.c_str());
gif->setPosition(Point(visibleSize.width * 0.5, visibleSize.height * 0.5));
gif -> setScale(2);
this ->addChild(gif);
gif2 = CacheGif::create(name.c_str());
gif2->setPosition(Point(500,0));
gif2->setScale(2);
this ->addChild(gif2);

用到的類主要是InstantGif 和 CacheGif,兩者的使用方法一樣,前者是一邊播放一邊從數據中解析幀,后者是一次性解析完,並放到緩存中,這種方式耗時長,占用內存大(圖片幀數太多的話可能會掛),但是相對第一種方式會流暢很多~

Cocos引擎中文官網現面向廣大Cocos引擎相關開發者征集優秀教程,歡迎給位童鞋踴躍投稿!來稿請發送至:support@cocos.org。

 

來源網址:http://helkyle.tk/2014/12/11/cocos2dxgif/

 

 

  1 .cpp完整代碼
  2 
  3 #include "HelloWorldScene.h"
  4 #include "Gif/GIFMovie.h"
  5 #include "Gif/CacheGif.h"
  6 #include "Gif/InstantGif.h"
  7 
  8 USING_NS_CC;
  9 
 10 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
 11 #define FILE_FORMAT ("/mnt/sdcard/g%d.gif") //On the Android platform, the resources are compressed in the asset directory. Therefore, the resources must be files on the sd card
 12 #else
 13 #define FILE_FORMAT ("g%d.gif")
 14 #endif
 15 
 16 CCScene* HelloWorld::scene()
 17 {
 18     // 'scene' is an autorelease object
 19     CCScene *scene = CCScene::create();
 20     
 21     // 'layer' is an autorelease object
 22     HelloWorld *layer = HelloWorld::create();
 23 
 24     // add layer as a child to scene
 25     scene->addChild(layer);
 26 
 27     // return the scene
 28     return scene;
 29 }
 30 
 31 // on "init" you need to initialize your instance
 32 bool HelloWorld::init()
 33 {
 34     //////////////////////////////
 35     // 1. super init first
 36     if ( !CCLayer::init() )
 37     {
 38         return false;
 39     }
 40       
 41     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 42     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
 43 
 44     /////////////////////////////
 45     // 2. add a menu item with "X" image, which is clicked to quit the program
 46     //    you may modify it.
 47 
 48     // add a "close" icon to exit the progress. it's an autorelease object
 49     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
 50         "CloseNormal.png",
 51         "CloseSelected.png",
 52         this,
 53         menu_selector(HelloWorld::menuCloseCallback));
 54 
 55     pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
 56         origin.y + pCloseItem->getContentSize().height/2));
 57 
 58     // create menu, it's an autorelease object
 59     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
 60     pMenu->setPosition(CCPointZero);
 61     this->addChild(pMenu, 1);
 62 
 63     return true;
 64 }
 65 
 66 int count = 1;
 67 void HelloWorld::update(float delta)
 68 {
 69     count++ ;
 70     if(count > 240)
 71     {
 72         this->removeAllChildren();
 73     }
 74 }
 75 
 76 void HelloWorld::menuCloseCallback(CCObject* pSender)
 77 {
 78     count++;
 79     while(this->getChildByTag(1000))
 80     {
 81         this->removeChildByTag(1000);
 82     }
 83     CCLOG("%s","------after remove gif-----------");
 84     CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();
 85     if(count % 2 != 0)
 86     {
 87         return ;
 88     }
 89     std::string name = CCString::createWithFormat(FILE_FORMAT,count/2)->getCString();
 90     name = CCFileUtils::sharedFileUtils()->fullPathForFilename(name.c_str());
 91     
 92     GifBase *gif = InstantGif::create(name.c_str());
 93     if(gif == NULL)
 94     {
 95         CCLOG("%s","create gif failed");
 96         return ;
 97     }
 98     gif->setAnchorPoint(ccp(0,0));
 99     this->addChild(gif);
100     gif->setPosition(ccp(0,0));
101     gif->setTag(1000);
102 
103 
104     gif = CacheGif::create(name.c_str());
105     gif->setAnchorPoint(ccp(0,0));
106     this->addChild(gif);
107     gif->setPosition(ccp(500,0));
108     gif->setScale(2);
109     gif->setTag(1000);
110     CCLOG("%s","------after add gif-----------");
111     CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();
112 
113     return ;
114 }

 

在class類中還有Gif文件夾,里面有需要的類

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM