×
Namespaces

Variants
Actions

缩放位图

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 05 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000984


  • 开发伙伴平台:

S60 3rd Edition, MR


  • 详细描述

下列代码片段演示了如何使用CBitmapScale缩放一张位图。

CBitmapScaler是一个公开的API,可以调用BitmapTransforms库进行位图缩放。


在缩放位图之前你需要先将图片解码为CFbsBitmap对象,可以使用CImageDecorder来解码。


Contents

MMP文件

需要下列能力和链接库

CAPABILITY    None
LIBRARY bitmaptransforms.lib
LIBRARY fbscli.lib


头文件

#include <bitmaptransforms.h>
#include <e32base.h>
#include <fbs.h>
 
class MBitmapScalerObserver
{
public:
virtual void Completed(TInt aErr) = 0;
};
 
class CMyBitmapScaler : public CActive
{
public:
static CMyBitmapScaler* NewL(MBitmapScalerObserver* aObserver);
virtual ~CMyBitmapScaler();
 
public:
void Scale( TInt aPercent );
CFbsBitmap* GetBitmap();
 
protected: // From CActive
void DoCancel();
void RunL();
TInt RunError(TInt aError);
 
private:
CMyBitmapScaler(MBitmapScalerObserver* aObserver);
void ConstructL();
 
private:
CFbsBitmap* iBitmap;
CBitmapScaler* iScaler;
MBitmapScalerObserver* iObserver;
};


源文件

CMyBitmapScaler* CMyBitmapScaler::NewL( 
MBitmapScalerObserver* aObserver )
{
CMyBitmapScaler* self =
new(ELeave) CMyBitmapScaler( aObserver );
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
CMyBitmapScaler::CMyBitmapScaler(
MBitmapScalerObserver* aObserver ) :
CActive( EPriorityStandard ), iObserver( aObserver )
{
}
 
void CMyBitmapScaler::ConstructL()
{
iScaler = CBitmapScaler::NewL();
CActiveScheduler::Add(this);
}
 
CMyBitmapScaler::~CMyBitmapScaler()
{
Cancel();
delete iBitmap;
delete iScaler;
}
 
void CMyBitmapScaler::DoCancel()
{
iScaler->Cancel();
}
 
void CMyBitmapScaler::RunL()
{
iObserver->Completed(iStatus.Int());
}
 
TInt CMyBitmapScaler::RunError(TInt aError)
{
iObserver->Completed(aError);
return KErrNone;
}
 
void CMyBitmapScaler::Scale(TInt aPercent)
{
if (IsActive())
{
return;
}
 
TReal32 multiplier;
multiplier = aPercent/(TReal32)100.0;
 
TInt newWidth = (TInt)(iBitmap->SizeInPixels().iWidth * multiplier);
TInt newHeight = (TInt)(iBitmap->SizeInPixels().iHeight * multiplier);
 
// The maintain aspect ratio is by default set to true
iScaler->Scale( &iStatus, *iBitmap, TSize(newWidth,newHeight) );
 
SetActive();
}
 
CFbsBitmap* CMyBitmapScaler::GetBitmap()
{
return iBitmap;
}


如何使用

TInt KScaleUpFactor = 150;    // Scale up by 50%
TInt KScaleDownFactor = 50; // Scale down by 50%
 
iScaler = CMyBitmapScaler::NewL(this);
iScalser->Scale(KScaleUpFactor);
This page was last modified on 15 December 2011, at 09:32.
40 page views in the last 30 days.