现在的位置: 首页 > 综合 > 正文

[转载]symbian Grid图像动态转换加载图像(jpg,bmp)

2012年11月29日 ⁄ 综合 ⁄ 共 12818字 ⁄ 字号 评论关闭

/** 
*  
* @brief Definition of CSimpleGridGamesGrid 

* Copyright (c) EMCC Software Ltd 2003 
* @version 1.0 
*/ 
// INCLUDE FILES  
// Class include  
#include "SimpleGridGamesGrid.h"  
// System includes  
#include <akniconarray.h> // CAknIconArray  
#include <aknlists.h> // AknListBoxLayouts  
#include <barsread.h> // TResourceReader  
//#include <simplegrid.mbg> // contains icon enumeration  
#include <stringloader.h> // StringLoader  
#include <gulicon.h>  
#include "ImageConverterEngine.h"  
#include "PublicUtility.h"  
// CONSTANTS  
const TInt KNumberOfIcons(5);  
const TInt KTextColor = 0;//215;  
const TInt KGridRectTLY = 0 ;  
const TInt KGridRectBRY = 640;  
const TInt KGridRectBRX = 360 ;  
const TInt NumOfItemsWidth = 3 ;  
const TInt NumOfItemsHeight = 5 ;  
// ================= MEMBER FUNCTIONS =======================  
/** 
* Symbian OS 2nd phase constructor.  Constructs the grd from aGridResource 
* sets up the icons container in the file in aIconFileResource and draws itself. 
* @param aGridResource The GRID resource 
* @param aIconFileResource The resource defining the icon file name 
*/        
void CSimpleGridGamesGrid::ConstructL(TInt aGridResource, TInt aIconFileResource)  
    {  
    // Construct the grid from resource  
    TResourceReader reader;  
    CEikonEnv::Static()->CreateResourceReaderLC(reader, aGridResource);  
    ConstructFromResourceL(reader);  
    CleanupStack::PopAndDestroy(); // reader  
    SetCellSizes();  
    // Set up the icons  
    SetupGridIconsL (aIconFileResource);  
    // Draw the grid  
    SizeChanged();  
    }  
/** 
*    
* Sets up the grid's icons, which are contained in a file. 

* @param aIconFileResource the file name resource 
*/ 
void CSimpleGridGamesGrid::SetupGridIconsL(TInt aIconFileResource)  
    {  
    // Get the name of the file containing the icons      
//  HBufC* iconFileName;  
//  iconFileName = StringLoader::LoadLC(aIconFileResource); // Pushes iconFileName onto the Cleanup Stack.  
    // Create an array of icons, reading them from the file  
    CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(KNumberOfIcons);  
    CleanupStack::PushL(icons);  
    GetIconByFileName(_L("latest.jpg"),icons);  
    GetIconByFileName(_L("top.jpg"),icons);  
    GetIconByFileName(_L("category.jpg"),icons);  
    GetIconByFileName(_L("Bookshelf.jpg"),icons);  
    GetIconByFileName(_L("Search.jpg"),icons);  
    CleanupStack::Pop(icons);  
    ItemDrawer()->FormattedCellData()->SetIconArray(icons); // passing ownership of icons  
    }  
void CSimpleGridGamesGrid::GetIconByFileName(const TDesC& aFileName,CArrayPtr<CGulIcon>* icons)  
    {  
    TFileName iFileName;  
#if !defined(__WINS__) && !defined(__WINSCW__)   
    // Device  
    iFileName = aFileName;  
      
    HBufC* resoucePath = HBufC::NewLC(KMaxFileName);  
    TPtr resoucePtr = resoucePath->Des();  
      
    PublicUtility::GetExePath(resoucePtr);  
    iFileName.Insert(0, *resoucePath);  
    CleanupStack::PopAndDestroy(resoucePath);      
#else  
    // Emulator  
    PublicUtility::GetDataPathL(iFileName);   
    iFileName.Append(aFileName);  
#endif  
    CImageConverterEngine* iImageConverter = CImageConverterEngine::NewL(this);  
    iImageConverter->StartToDecodeL(iFileName, ETrue);  
    CFbsBitmap* iBitmap = iImageConverter->GetBitmap();  
    CGulIcon * icon = CGulIcon::NewL(iBitmap);  
    icons->AppendL(icon);   
    }  
/** 
*    
* Sets up colors, and subcells for the grid. 

*/ 
void CSimpleGridGamesGrid::SetupGrid()  
    {  
    this->SetLayoutL(EFalse, ETrue, ETrue, 3, 5, iCellSize);  
    this->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOff);  
      
    // Setup text foreground and background colours to default  
    AknListBoxLayouts::SetupStandardGrid(*this);  
    // Get local copies of data we will need      
    CFormattedCellListBoxItemDrawer* itemDrawer = this->ItemDrawer();  
    TInt cellWidth = iCellSize.iWidth;  
    TInt cellHeight = iCellSize.iHeight;  
          
    // Set up graphics subcells  
    AknListBoxLayouts::SetupFormGfxCell(*this,  //the grid  
                                        itemDrawer, // the grid's drawer  
                                        0, // index of the graphic within item strings  
                                        0, // left position  
                                        0, // top position  
                                        0, // right - unused  
                                        0, // bottom - unused  
                                        cellWidth, // width of graphic  
                                        cellHeight, // height of graphic  
                                        TPoint (0, 0), // start position  
                                        TPoint (cellWidth , cellHeight)); // end position  
    // Set up text subcells  
    const CFont* KFont = CEikonEnv::Static()->AnnotationFont();  
    TInt baseline = cellHeight - KFont->DescentInPixels() - 1;  
    AknListBoxLayouts::SetupFormTextCell(*this, // the grid  
                                         itemDrawer, // the grid's drawer  
                                         1, // index of text within item strings  
                                         KFont, // the font for the text  
                                         KTextColor, // the color of the text - N.B. although commented out in the header file, this is still used!  
                                         0, // left margin  
                                         0, // right margin - unused  
                                         baseline, // Baseline  
                                         cellWidth, // text width (would need to take margin into account if set, i.e. - lm)  
                                         CGraphicsContext::ECenter, // Alignment  
                                         TPoint (0, cellHeight), // start position  
                                         TPoint(cellWidth, cellHeight)); // end position  
    }  
void CSimpleGridGamesGrid::SetCellSizes()  
    {  
    TRect MyRect(Rect());  
      
    MyRect.iTl.iY = KGridRectTLY;  
    MyRect.iBr.iY = KGridRectBRY;  
    MyRect.iBr.iX = KGridRectBRX;  
    // Determinate icon size  
    iCellSize.iWidth =  MyRect.iBr.iX / NumOfItemsWidth;  
    iCellSize.iHeight = MyRect.iBr.iY / NumOfItemsHeight;  
    }  
/** 
*    
* Called by framework when the view size is changed and by ConstructL to intialise the grid.   

*/ 
void CSimpleGridGamesGrid::SizeChanged()  
    {  
    CAknGrid::SizeChanged();  
    SetupGrid();  
    }  
void CSimpleGridGamesGrid::NotifyCompletionL(TInt aErr, const TDesC& aMsg)  
{  
 //LOG(_L("CComicViewerSplashContainer::NotifyCompletionL()"));  
    if (aErr == KErrNone)  
    {         
    }  
    else 
    {  
        //PublicUtility::ShowMessageL(aMsg);  
    }  
    if (aErr && aErr != KErrNotReady)  
    {  
        //PublicUtility::ShowMessageL(aMsg);  
    }  
}  
// End of File   
/**
*
* @brief Definition of CSimpleGridGamesGrid
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDE FILES
// Class include
#include "SimpleGridGamesGrid.h"
// System includes
#include <akniconarray.h> // CAknIconArray
#include <aknlists.h> // AknListBoxLayouts
#include <barsread.h> // TResourceReader
//#include <simplegrid.mbg> // contains icon enumeration
#include <stringloader.h> // StringLoader
#include <gulicon.h>
#include "ImageConverterEngine.h"
#include "PublicUtility.h"
// CONSTANTS
const TInt KNumberOfIcons(5);
const TInt KTextColor = 0;//215;
const TInt KGridRectTLY = 0 ;
const TInt KGridRectBRY = 640;
const TInt KGridRectBRX = 360 ;
const TInt NumOfItemsWidth = 3 ;
const TInt NumOfItemsHeight = 5 ;
// ================= MEMBER FUNCTIONS =======================
/**
* Symbian OS 2nd phase constructor.  Constructs the grd from aGridResource
* sets up the icons container in the file in aIconFileResource and draws itself.
* @param aGridResource The GRID resource
* @param aIconFileResource The resource defining the icon file name
*/  
void CSimpleGridGamesGrid::ConstructL(TInt aGridResource, TInt aIconFileResource)
 {
 // Construct the grid from resource
 TResourceReader reader;
 CEikonEnv::Static()->CreateResourceReaderLC(reader, aGridResource);
 ConstructFromResourceL(reader);
 CleanupStack::PopAndDestroy(); // reader
 SetCellSizes();
 // Set up the icons
 SetupGridIconsL (aIconFileResource);
 // Draw the grid
 SizeChanged();
 }
/**

* Sets up the grid's icons, which are contained in a file.
*
* @param aIconFileResource the file name resource
*/
void CSimpleGridGamesGrid::SetupGridIconsL(TInt aIconFileResource)
 {
 // Get the name of the file containing the icons 
// HBufC* iconFileName;
// iconFileName = StringLoader::LoadLC(aIconFileResource); // Pushes iconFileName onto the Cleanup Stack.
 // Create an array of icons, reading them from the file
 CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(KNumberOfIcons);
 CleanupStack::PushL(icons);
 GetIconByFileName(_L("latest.jpg"),icons);
 GetIconByFileName(_L("top.jpg"),icons);
 GetIconByFileName(_L("category.jpg"),icons);
 GetIconByFileName(_L("Bookshelf.jpg"),icons);
 GetIconByFileName(_L("Search.jpg"),icons);
 CleanupStack::Pop(icons);
 ItemDrawer()->FormattedCellData()->SetIconArray(icons); // passing ownership of icons
 }
void CSimpleGridGamesGrid::GetIconByFileName(const TDesC& aFileName,CArrayPtr<CGulIcon>* icons)
 {
 TFileName iFileName;
#if !defined(__WINS__) && !defined(__WINSCW__)
    // Device
 iFileName = aFileName;
 
    HBufC* resoucePath = HBufC::NewLC(KMaxFileName);
    TPtr resoucePtr = resoucePath->Des();
   
    PublicUtility::GetExePath(resoucePtr);
    iFileName.Insert(0, *resoucePath);
    CleanupStack::PopAndDestroy(resoucePath);   
#else
    // Emulator
 PublicUtility::GetDataPathL(iFileName);
 iFileName.Append(aFileName);
#endif
 CImageConverterEngine* iImageConverter = CImageConverterEngine::NewL(this);
 iImageConverter->StartToDecodeL(iFileName, ETrue);
 CFbsBitmap* iBitmap = iImageConverter->GetBitmap();
 CGulIcon * icon = CGulIcon::NewL(iBitmap);
 icons->AppendL(icon);
 }
/**

* Sets up colors, and subcells for the grid.
*
*/
void CSimpleGridGamesGrid::SetupGrid()
 {
 this->SetLayoutL(EFalse, ETrue, ETrue, 3, 5, iCellSize);
 this->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOff);
 
 // Setup text foreground and background colours to default
 AknListBoxLayouts::SetupStandardGrid(*this);
 // Get local copies of data we will need 
 CFormattedCellListBoxItemDrawer* itemDrawer = this->ItemDrawer();
 TInt cellWidth = iCellSize.iWidth;
 TInt cellHeight = iCellSize.iHeight;
  
 // Set up graphics subcells
 AknListBoxLayouts::SetupFormGfxCell(*this,  //the grid
          itemDrawer, // the grid's drawer
          0, // index of the graphic within item strings
          0, // left position
          0, // top position
          0, // right - unused
          0, // bottom - unused
          cellWidth, // width of graphic
          cellHeight, // height of graphic
          TPoint (0, 0), // start position
          TPoint (cellWidth , cellHeight)); // end position
 // Set up text subcells
 const CFont* KFont = CEikonEnv::Static()->AnnotationFont();
 TInt baseline = cellHeight - KFont->DescentInPixels() - 1;
 AknListBoxLayouts::SetupFormTextCell(*this, // the grid
           itemDrawer, // the grid's drawer
           1, // index of text within item strings
           KFont, // the font for the text
           KTextColor, // the color of the text - N.B. although commented out in the header file, this is still used!
           0, // left margin
           0, // right margin - unused
           baseline, // Baseline
           cellWidth, // text width (would need to take margin into account if set, i.e. - lm)
           CGraphicsContext::ECenter, // Alignment
           TPoint (0, cellHeight), // start position
           TPoint(cellWidth, cellHeight)); // end position
 }
void CSimpleGridGamesGrid::SetCellSizes()
 {
    TRect MyRect(Rect());
   
    MyRect.iTl.iY = KGridRectTLY;
    MyRect.iBr.iY = KGridRectBRY;
    MyRect.iBr.iX = KGridRectBRX;
    // Determinate icon size
    iCellSize.iWidth =  MyRect.iBr.iX / NumOfItemsWidth;
    iCellSize.iHeight = MyRect.iBr.iY / NumOfItemsHeight;
 }
/**

* Called by framework when the view size is changed and by ConstructL to intialise the grid. 
*
*/
void CSimpleGridGamesGrid::SizeChanged()
 {
 CAknGrid::SizeChanged();
 SetupGrid();
 }
void CSimpleGridGamesGrid::NotifyCompletionL(TInt aErr, const TDesC& aMsg)
{
 //LOG(_L("CComicViewerSplashContainer::NotifyCompletionL()"));
    if (aErr == KErrNone)
    {     
    }
    else
    {
        //PublicUtility::ShowMessageL(aMsg);
    }
    if (aErr && aErr != KErrNotReady)
    {
     //PublicUtility::ShowMessageL(aMsg);
    }
}
// End of File 
 

网上找了一圈动态创建Grid的范例,终于在一个论坛中找到点代码。这里图像时动态转换后,生成图标,然后再创建Grid.

其中的CImageConverterEngine,用symbian wiki上的imageConverter范例的CImageConverterEngine,要注意转换的时候用同步操作。

view plaincopy to clipboardprint?
TBool CImageConverterEngine::StartToDecodeL(const TDesC& aFileName , TBool aAsync /*= EFalse*/ )  
{  
 //LOG(_L("CImageConverterEngine::StartToDecodeL"));  
    if (IsActive())  
        Cancel();  
    if(iImageDecoder)  
    {  
        delete iImageDecoder;   
        iImageDecoder = NULL;  
    }  
    if(!PublicUtility::CheckFileCanbeReadOrWriteL(aFileName))  
        return EFalse;  
    SetFilePathL(aFileName);  
      
    // create the decoder  
    iImageDecoder = CImageDecoder::FileNewL( iFs, *iFileName );  
    // create the destination bitmap  
    if(iBitmap)  
    {  
        delete iBitmap;  
        iBitmap = NULL;  
    }  
    iBitmap = new (ELeave) CFbsBitmap();  
    TRAPD(error, iBitmap->Create( iImageDecoder->FrameInfo().iOverallSizeInPixels, iImageDecoder->FrameInfo().iFrameDisplayMode ));  
    if(error)  
        return EFalse;  
    // start conversion to bitmap  
    iState = EDecoding;  
    iImageDecoder->Convert( &iStatus, *iBitmap );  
    SetActive();  
    if(aAsync)  
        StartWait();  
   //LOG(_L("CImageConverterEngine::StartToDecodeL--end"));  
    return ETrue;  

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linzhiji/archive/2010/01/26/5259214.aspx

抱歉!评论已关闭.