Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android圖形庫Skia(一)-基本測試生成PNG圖片

Android圖形庫Skia(一)-基本測試生成PNG圖片

編輯:關於Android編程

基於淺談 Google Skia 圖形處理引擎和Skia Demo Build。 Skia是一個跨平台的圖形庫,目前使用在Android中,同樣也有PC版本,這裡測試一下以了解其內涵。

1.PC版本測試:

# 1.下載 Skia

git clone [email protected]:google/skia.git

# 2.切換到老版本 參考ubuntu 移植SKIA的日期進行回退

git reset --hard 0e2810be95d3f1aa95c341521d3f514eb9e9ebde

# 3.查看編譯幫助

$ make help

Targets:

: out/libskia.a

bench: out/bench/bench

gm: out/gm/gm

skimage: out/tools/skimage

skhello: out/tools/skhello

tests: out/tests/tests

clean: removes entire out/ directory

help: this text

Options: (after make, or in bash shell)

SKIA_DEBUG=true for debug build

SKIA_SCALAR=fixed for fixed-point build

SKIA_BUILD_FOR=mac for mac build (e.g. CG for image decoding)

SKIA_PDF_SUPPORT=false to disable the pdf generation backend

# 3.進行編譯

$ make SKIA_BUILD_FOR=linux

# 4.將Skia Demo Build的例子保存為test-skia.c。

# 5.編譯測試程序

$ g++ \

-I./include \

-I./include/core \

-I./include/images \

-I./include/config \

-Wall -o test-skia test-skia.c \

out/src/images/SkImageDecoder_libpng.o out/libskia.a \

-lpng -lpthread -lfreetype -g

# 運行

$ ./test-skia

# 查看測試結果

$ eog snapshot.png

顯示的是一張圖片如下:

\ <喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+suLK1LPM0PKjujwvcD4KPHA+LyogU2ltcGxlIHZlY3RvciBncmFwaGljcyBkZW1vIHV0aWxpemluZyBTa2lhIHRvb2xraXQuPC9wPgo8cD4gKiBBdXRob3JlZCBieSBKaW0gSHVhbmcgPGpzZXJ2LnR3QGdtYWlsLmNvbT48L3A+CjxwPiAqLzwvcD4KPHA+IDwvcD4KPHA+I2luY2x1ZGUg"SkBitmap.h"

#include "SkDevice.h"

#include "SkPaint.h"

#include "SkRect.h"

#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(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;

}

2.在Android中測試

寫了一個Android.mk內容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := skia_test2

#Can't use 'LOCAL_SRC_FILES := $(call all-subdir-cpp-files)'

LOCAL_SRC_FILES := skia_test2.cpp

LOCAL_C_INCLUDES := \

external/skia/include/core/ \

external/skia/include/images/

LOCAL_SHARED_LIBRARIES := \

libskia

include $(BUILD_EXECUTABLE)

可以原封不動地將PC上的測試源碼編譯成適合Android的測試程序。將其放到/system/bin目錄下。如下執行:

$ cd sdcard

$ skia_test2

會在/sdcard目錄下同樣生成snapshot.png,傳到PC上查看和之前實驗效果一樣。以後會加入freetype和framebuffer。最終實現撇開Android系統來實現簡單圖形字體顯示。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved