BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #4同步于 2010/3/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖

【范文二】OPhone OS C++编程入门 ——Skia c++ API的使用 (转

haibara
2010/3/13镜像同步1 回复
【 以下文字转载自 Board_Apply 讨论区 】 发信人: zzsanduo (出太阳出太阳), 信区: Board_Apply 标 题: 【范文二】OPhone OS C++编程入门 ——Skia c++ API的使用 发信站: 北邮人论坛 (Wed Mar 10 23:17:50 2010), 站内 OPhone OS C++编程入门 ——Skia c++ API的使用 首先我们先来介绍一下skia的由来 2005 年8月 17 日,收购美国 Android 公司,业务是手机软件开发,这当然就是现在开放源码 Android 计划的前身, 2005 年11月,收购美国 Skia 公司,业务是向量绘图软件, Skia是一个非常严谨的向量显示引擎,能在低端设备比如手机、电视及其它手持设备之上,呈现高质量的 2D 图形 后来Skia便成为了OPhone OS平台的图形引擎. 下面我们来看一个比较简单的例子 #include "SkBitmap.h" #include "SkDevice.h" #include "SkPaint.h" #include "SkRect" #include "SkImageEncoder.h" int main() { // Declare a raster bitmap, which has an integer width and height, // and a format (config), and a pointer to the actual pixels. // Bitmaps can be drawn into a SkCanvas, but they are also used to // specify the target of a SkCanvas' drawing operations. SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200); bitmap.allocPixels(); // A Canvas encapsulates all of the state about drawing into a // device (bitmap). This includes a reference to the device itself, // and a stack of matrix/clip values. For any given draw call (e.g. // drawRect), the geometry of the object being drawn is transformed // by the concatenation of all the matrices in the stack. The // transformed geometry is clipped by the intersection of all of the // clips in the stack SkCanvas canvas(new SkDevice(bitmap)); // SkPaint class holds the style and color information about how to // draw geometries, text and bitmaps. SkPaint paint; // SkIRect holds four 32 bit integer coordinates for a rectangle. SkRect r; paint.setARGB(255, 255, 0, 0); r.set(25, 25, 145, 145); canvas.drawRect(r, paint); /** Draw the specified rectangle using the specified paint. The rectangle will be filled or stroked based on the Style in the paint. */ paint.setARGB(255, 0, 255, 0); r.offset(20, 20); canvas.drawRect(r, paint); paint.setARGB(255, 0, 0, 255); r.offset(20, 20); canvas.drawRect(r, paint); // SkImageEncoder is the base class for encoding compressed images // from a specific SkBitmap. SkImageEncoder::EncodeFile("snapshot.png", bitmap,SkImageEncoder::kPNG_Type,/* Quality ranges from 0..100 */ 100); return 0; } 1.定义一个位图32位象素并初始化 SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200); 其中setConfig为设置位图的格式,原型为void setConfig(Config, int width, int height, int rowBytes = 0) Config为一个数据结构 enum Config { kNo_Config, //不确定的位图格式 kA1_Config, //1位(黑,白)位图 kA8_Config, //8位(黑,白)位图 kIndex8_Config, //类似windows下的颜色索引表,具体请查看SkColorTable类结构 kRGB_565_Config, //16位象素565格式位图,详情请查看SkColorPriv.h文件 kARGB_4444_Config, //16位象素4444格式位图,详情请查看SkColorPriv.h文件 kARGB_8888_Config, //32位象素8888格式位图,详情请查看SkColorPriv.h文件 kRLE_Index8_Config, kConfigCount }; 2.分配位图所占的空间bitmap.allocPixels() 其实allocPixels为重载函数,原型为bool allocPixels(SkColorTable* ctable = NULL) 参数ctable为颜色索引表,一般情况下为NULL,除非bitmap.setConfig(SkBitmap::kIndex8_Config, 200, 200)明确指明位图为颜色索引格式 3.指定输出设备 SkCanvas canvas(new SkDevice(bitmap)); 其中canvas为一个多构造函数,原型为explicit SkCanvas(const SkBitmap& bitmap),explicit SkCanvas(SkDevice* device = NULL) explicit关健字的意思为:不允许类型转换 输出设备可以为一个上下文DC,也可以指定为一张位图 4.设备绘制的风格 Paint paint; SkRect r; paint.setARGB(255, 255, 0, 0); r.set(25, 25, 145, 145); canvas.drawRect(r, paint); paint可以指定绘图的颜色,文本的大小及对齐方式,编码格式等等,因为以前位图的格式设置为kARGB_8888_Config,所以这里要设置绘制的颜色setARGB(255, 255, 0, 0),第一位参数为透明颜色通道,其它三位分别为RGB r设置要绘制的范围,最后通过drawRect绘制出指定区域的一个方形 5.将png转换成位图格式,并将数据放到bitmap变量中 SkImageEncoder::EncodeFile("snapshot.png", bitmap,SkImageEncoder::kPNG_Type,/* Quality ranges from 0..100 */ 100); 第一位参数为png文件路径,第二位为指定的输出位图,第三位为文件的类型,支持png,jpeg,第四位参数指定了输出位图的质量,范围为 0..100,默认为80 好了,利用Skia c++ API进行简单的图形操作就介绍到这里吧,希望对大家有所帮助,本文重点在于介绍了怎么将一个位图邦定到指定的输出设备(屏幕)上,并将png文件转换成位图,然后刷新到设备上。 大家可以根据自己的需要,实现各种图形图像的特效处理的并形成native(c++)组件,让java层通过JNI调用。丰富自己软件的功能并实现高效率运算。
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
FJ机器人#1 · 2010/3/15
up