×
Namespaces

Variants
Actions

双缓冲防止闪屏

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

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

CS001049


设备, 软件 版本: S60 3rd Edition, MR


Contents

详细描述

当图片直接绘制到屏幕时,产生动画时。屏幕可能会有抖动闪屏。常用的一个方法是将图片绘制在off-screen缓冲上,然后在绘制完成后拷贝它的内容到屏幕上。下列示例显示了如果使用这个技术,我们需要两个缓冲,组成双缓冲。off-screen缓冲和原尺寸一样。


MMP文件

需要下列能力和链接库

CAPABILITY        NONE
LIBRARY fbscli.lib
LIBRARY bitgdi.lib


头文件

这里演示了允许双缓冲的CCoeControl组件的类定义

#include <coecntrl.h>
#include <bitstd.h>
#include <bitdev.h>
 
class CMyContainer : public CCoeControl
{
public:
void NewL(const TRect& aRect)
void ConstructL(const TRect& aRect);
virtual ~CImageConverterContainer();
private:
void CMyContainer();
void SizeChanged();
void HandleResourceChange(TInt aType);
TInt CountComponentControls() const;
CCoeControl* ComponentControl(TInt aIndex) const;
void Draw(const TRect& aRect) const;
 
// for Double buffering
public:
void DrawToBackBuffer();
// for Double buffering
private:
void CreateBackBufferL();
void ReleaseBackBuffer();
 
// for Double buffering
private:
CFbsBitmap* iBackBuffer;
CFbsBitmapDevice* iBackBufferDevice;
CFbsBitGc* iBackBufferContext;
TSize iBackBufferSize;
 
// other member variables of the class if needed
private:
};


源文件

下列代码只是和双缓冲有关的部分

void CMyContainer::SizeChanged()
{
// Delete back buffer and create a new one with new size
ReleaseBackBuffer();
CreateBackBufferL();
DrawToBackBuffer();
}
 
void CMyContainer::CreateBackBufferL()
{
// Create back buffer bitmap
iBackBuffer = new (ELeave) CFbsBitmap;
 
User::LeaveIfError( iBackBuffer->Create(Size(),
iEikonEnv->DefaultDisplayMode()));
 
// Create back buffer graphics context
iBackBufferDevice = CFbsBitmapDevice::NewL(iBackBuffer);
User::LeaveIfError(iBackBufferDevice->CreateContext(iBackBufferContext));
iBackBufferContext->SetPenStyle(CGraphicsContext::ESolidPen);
 
iBackBufferSize = iBackBuffer->SizeInPixels();
}
 
void CMyContainer::ReleaseBackBuffer()
{
// Release double buffering classes
if (iBackBufferContext)
{
delete iBackBufferContext;
iBackBufferContext = NULL;
}
if (iBackBufferDevice)
{
delete iBackBufferDevice;
iBackBufferDevice = NULL;
}
if (iBackBuffer)
{
delete iBackBuffer;
iBackBuffer = NULL;
}
iBackBufferSize = TSize(0, 0);
}
 
void CMyContainer::DrawToBackBuffer()
{
if (!iBackBufferContext)
{
return;
}
 
iBackBufferContext->Clear();
 
// TODO: do your drawing there.
// Remember to draw to iBackBufferContext buffered graphic contex
 
// Example of drawing bitmap into iBackBufferContext
//if(iBitmap)
// {
// iBackBufferContext->BitBlt( iPicturePoint, iBitmap );
// }
}
 
void CMyContainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
 
// Copy backbuffer to the screen
gc.BitBlt(TPoint(0, 0), iBackBuffer);
 
// TODO: Remove your all drawing from there to DrawToBackBuffer
}
 
CMyContainer::~CMyContainer()
{
ReleaseBackBuffer();
}

如何使用

老的使用方式:

iAppContainer->DrawNow();


现在用双缓冲的方式:

iAppContainer->DrawToBackBuffer();
iAppContainer->DrawNow();


后继条件

屏幕将不会抖动闪屏

This page was last modified on 15 December 2011, at 08:55.
53 page views in the last 30 days.
×