Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Showing GIF animations

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: symbianyucca (20 Mar 2007)
Last edited: hamishwillee (13 Sep 2012)

CGif_Reader shows a simple GIF animation reader class that can be used to show a GIF animation in CCoeControl based containers. To construct the CGif_Reader add the following lines to the ConstructL() method inside your container:

iGif_Reader = new(ELeave)CGif_Reader(*this);
iGif_Reader->ConstructL(KTxtFileName);

KTxtFileName is the GIF file name (with drive, full path, name, and extension). To draw the GIF animation in your container you implement the container's Draw() method:

void CMycontainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
 
if(iGif_Reader)
{
if(iGif_Reader->Bitmap())
{
if(iGif_Reader->Bitmap()->Handle())
{
gc.DrawBitmap(aRect, iGif_Reader->Bitmap());
}
}
}
}

Gif_Reader.cpp

#include "Gif_Reader.h"
 
_LIT8(KtxTypeImageGif_8 ,"image/gif");
 
const TInt KFrameTimeOut = 200000;// 0.2 sec frame timeout
 
 
CGif_Reader::CGif_Reader(CCoeControl& aParent)
:
CActive(0), iParent(aParent), iCurrImg(-1)
{
}
 
CGif_Reader::~CGif_Reader()
{
Cancel();
delete iExampleTimer;
delete iImageDecoder;
delete iFrame;
delete iFrameImg;
delete iFrameMsk;
 
delete iBitmapDevice;
delete iGraphicsContext;
}
 
 
void CGif_Reader::ConstructL(const TDesC& aFileName)
{
CActiveScheduler::Add(this);
iImageDecoder = CImageDecoder::FileNewL(CCoeEnv::Static()->FsSession(),
aFileName, KtxTypeImageGif_8);
iCurrCount = iImageDecoder->FrameCount();
 
if(iCurrCount > 0)
{
iFrame = new(ELeave)CFbsBitmap();
iFrame->Create(iImageDecoder->FrameInfo(0).iOverallSizeInPixels,
iImageDecoder->FrameInfo(0).iFrameDisplayMode);
 
iBitmapDevice = CFbsBitmapDevice::NewL(iFrame);
User::LeaveIfError(iBitmapDevice->CreateContext(iGraphicsContext));
}
//should call NextImageL to actually start showing something
NextImageL();
}
 
void CGif_Reader::TimerExpired(TAny* /*aTimer*/, TInt /*aError*/)
{
Cancel();
NextImageL();
}
 
 
void CGif_Reader::NextImageL(void)
{
iCurrImg++;
if(iCurrImg >= iCurrCount || iCurrImg < 0)
{
iCurrImg = 0;
}
 
delete iFrameImg;
iFrameImg = NULL;
iFrameImg = new(ELeave)CFbsBitmap();
iFrameImg->Create(iImageDecoder->FrameInfo(iCurrImg).iOverallSizeInPixels,
iImageDecoder->FrameInfo(iCurrImg).iFrameDisplayMode);
 
delete iFrameMsk;
iFrameMsk = NULL;
iFrameMsk = new(ELeave)CFbsBitmap();
iFrameMsk->Create(iImageDecoder->FrameInfo(iCurrImg).iOverallSizeInPixels,
EGray2);
 
iImageDecoder->Convert(&iStatus, *iFrameImg, *iFrameMsk, iCurrImg);
SetActive();
}
 
 
void CGif_Reader::DoCancel()
{
iImageDecoder->Cancel();
}
 
CFbsBitmap* CGif_Reader::Bitmap()
{
return iFrame;
}
 
 
void CGif_Reader::RunL()
{
if(iFrameMsk && iFrameImg && iFrame && iGraphicsContext)
{
if(iFrameMsk->Handle() && iFrameImg->Handle() && iFrame->Handle())
{
if(TFrameInfo::ERestoreToBackground & iImageDecoder->FrameInfo(iCurrImg).iFlags)
{
iGraphicsContext->DrawBitmap(
TRect(0, 0, iFrame->SizeInPixels().iWidth, iFrame->SizeInPixels().iHeight),
iFrameImg);
}
else
{
iGraphicsContext->DrawBitmapMasked(iImageDecoder->FrameInfo(iCurrImg).iFrameCoordsInPixels,
iFrameImg, TRect(0, 0, iFrameImg->SizeInPixels().iWidth, iFrameImg->SizeInPixels().iHeight),
iFrameMsk, EFalse);
}
}
}
 
iParent.DrawNow();
 
if(iStatus.Int() != KErrCancel)
{
if(!iExampleTimer)
iExampleTimer = CExampleTimer::NewL( *this );
 
// will be redrawing next frame after interval specified in Gif
TTimeIntervalMicroSeconds nextDelay = iImageDecoder->FrameInfo(iCurrImg).iDelay;
iExampleTimer->After(nextDelay.Int64()); //KFrameTimeOut);
}
}

Gif_Reader.h

#include <e32base.h>
#include <coecntrl.h>
#include <ImageConversion.h>
 
// CImageDecoder
// Link against: ImageConversion.lib
// Supported from 7.0s
//
 
#include "CExampleTimer.h"
 
 
 
class CGif_Reader : public CActive,MExampleTimerNotify
{
public:
CGif_Reader(CCoeControl& aParent);
void ConstructL(const TDesC& aFileName);
~CGif_Reader();
CFbsBitmap* Bitmap();
protected:
void TimerExpired(TAny* aTimer,TInt aError);
void DoCancel();
void RunL();
private:
void NextImageL(void);
void FinnishReadL(void);
private:
CCoeControl& iParent;
TInt iCurrImg,iCurrCount;
CImageDecoder* iImageDecoder;
CFbsBitmap* iFrame;
CFbsBitmap* iFrameMsk;
CFbsBitmap* iFrameImg;
CExampleTimer* iExampleTimer;
CFbsBitGc* iGraphicsContext;
CFbsBitmapDevice* iBitmapDevice;
};
This page was last modified on 13 September 2012, at 06:21.
94 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×