手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >安卓开发之SkBitmap的内存管理
安卓开发之SkBitmap的内存管理
摘要:SkBitmap是skia中很重要的一个类,很多画图动作涉及到SkBitmap,它封装了与位图相关的一系列操作,了解它的内存管理策略有助于我...

SkBitmap是skia中很重要的一个类,很多画图动作涉及到SkBitmap,它封装了与位图相关的一系列操作,了解它的内存管理策略有助于我们更好的使用它,了解它的初衷是要想实现对skia中的blitter进行硬件加速。

1. SkBitmap的类结构:

安卓开发之SkBitmap的内存管理1

2. SkBitmap的内嵌类Allocator

Allocator是SkBitmap的内嵌类,其实只有一个成员函数:allocPixelRef(),所以把它理解为一个接口更合适,SkBitmap使用Allocator的派生类–HeapAllocator作为它的默认分配器。其实现如下:

bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,

SkColorTable* ctable) {

Sk64 size = dst->getSize64();

if (size.isNeg() || !size.is32()) {

return false;

}

void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure

if (NULL == addr) {

return false;

}

dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();

// since we're already allocated, we lockPixels right away

dst->lockPixels();

return true;

}

当然,也可以自己定义一个Allocator,使用SkBitmap的成员函数allocPixels(Allocator* allocator, SkColorTable* ctable) ,传入自定义的Allocator即可,如果传入NULL,则使用默认的HeapAllocator。

3. SkPixelRef类

SkPixelRef和Allocator密切相关,Allocator分配的内存由SkPixelRef来处理引用计数,每个Allocator对应一个SkPixelRef,通常在分配内存成功后,由Allocator调用setPixelRef来进行绑定。默认的情况下,SkBitmap使用 SkMallocPixelRef和HeapAllocator进行配对。所以如果你要派生Allocator类,通常也需要派生一个 SkPixelRef类与之对应。

4. 使用例子

以下是一段简短的代码,示意如何动态分配一个SkBitmap:

SkBitmap bitmap;

bitmap.setConfig(hasAlpha ? SkBitmap::kARGB_8888_Config :

SkBitmap::kRGB_565_Config, width, height);

if (!bitmap.allocPixels()) {

return;

}

//...... // 对bitmap进行画图操作

//......

// 画到Canvas上

canvas->drawBitmap(bitmap, SkFloatToScalar(x), SkFloatToScalar(y), paint);

【安卓开发之SkBitmap的内存管理】相关文章:

android开发之方形圆角listview代码分享

安卓开发教程之判断出屏的方法

andorid编程中内存管理优化

点九图片的显示内容区域应作何理解

Android开发笔记之:在ImageView上绘制圆环的实现方法

Android开发之XML文件解析的使用

android 开发 文件读写应用案例分析

Android开发之OpenGL ES 画多边形

安卓开发之音效代码

安卓开发之调试实例

精品推荐
分类导航