些日子一直在纠结于android的东西,各种配环境、编译,终于告一小段落了。
用native code开发ndk提供的资源是远远不够的,要使用android源码这个超级sdk。具体方法就是下载好源码然后把自己写的代码和它的Android.mk文件放在源码根目录的一子目录里,然后在根目录make LOCAL_MODULE。
用native code显示图片由于网上很少有相关资料,所有费了一些劲。
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= test.cpp
LOCAL_MODULE:= testbmp
LOCAL_MODULE_TAGS := tests
LOCAL_SHARED_LIBRARIES :=
libskia
libutils
libsurfaceflinger_client
libui
$(info $(LOCAL_SHARED_LIBRARIES))
include $(BUILD_EXECUTABLE)
test.cpp:
#include <core/SkCanvas.h>
#include <core/SkDevice.h>
#include <surfaceflinger/SurfaceComposerClient.h>
using namespace android;
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 640
#define SCREEN_NUMBER 5
#define ORIGIN_X 0
#define ORIGIN_Y 0
#define BACKGROUND_LAYER 100000
#define FRONT_LAYER 120000
sp<SurfaceComposerClient> g_uiClient;
sp<SurfaceControl> g_bkSurfacecontrol;
sp<SurfaceControl> g_ftSurfacecontrol[SCREEN_NUMBER];
sp<Surface> g_bkSurface;
sp<Surface> g_ftSurface[SCREEN_NUMBER];
int p[10000] = {
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000,
0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000,
0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000, 0xff000000,
0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000,
0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000,
0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000,
0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000,
};
int testInit()
{
g_uiClient = new SurfaceComposerClient();
int i = 0;
g_ftSurfacecontrol[i] = g_uiClient->createSurface(getpid(), 0, SCREEN_WIDTH, SCREEN_HEIGHT, PIXEL_FORMAT_RGBA_8888);
g_ftSurface[i] = g_ftSurfacecontrol[i]->getSurface();
g_uiClient->openTransaction();
g_ftSurfacecontrol[i]->setLayer(FRONT_LAYER);
g_ftSurfacecontrol[i]->setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
g_ftSurfacecontrol[i]->setPosition(ORIGIN_X + i * SCREEN_WIDTH, ORIGIN_Y);
g_uiClient->closeTransaction();
g_bkSurfacecontrol = g_uiClient->createSurface(getpid(), 0, SCREEN_WIDTH * 1, SCREEN_HEIGHT, PIXEL_FORMAT_RGBA_8888);
g_uiClient->openTransaction();
g_bkSurfacecontrol->setLayer(BACKGROUND_LAYER);
g_bkSurfacecontrol->setSize(SCREEN_WIDTH * SCREEN_NUMBER, SCREEN_HEIGHT);
g_bkSurfacecontrol->setPosition(ORIGIN_X, ORIGIN_Y);
g_uiClient->closeTransaction();
g_bkSurface = g_bkSurfacecontrol->getSurface();
return 0;
}
void BkFlushBufferDraw(int *p)
{
Surface::SurfaceInfo info;
g_bkSurface->lock(&info);
SkBitmap bitmap;
ssize_t bpr = info.s * bytesPerPixel(info.format);
bitmap.setConfig(SkBitmap::kARGB_8888_Config, info.w, info.h, (int)bpr);
bitmap.setPixels(info.bits);
SkCanvas* canvas = new SkCanvas(new SkDevice(bitmap));
canvas->setViewport(bitmap.width(), bitmap.height());
SkBitmap bitmap2;
bitmap2.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
bitmap2.setPixels(p);
canvas->drawBitmap(bitmap2, (SkScalar)30.0,(SkScalar)30.0);
g_bkSurface->unlockAndPost();
sleep(100000000);
}
int main()
{
testInit();
BkFlushBufferDraw(p);
return 0;
}
然后make testbmp就可以了。
其中int p[]是图像的数组,每像素一字节,从低到高分别是RGBA。