打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android skia简单应用
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://vaero.blog.51cto.com/4350852/790602
Android skia简单应用
         很简单的Skia 2D图形库的调用。
 

一、Skia 2D图形库

         Skia是Google一个底层的图形、图像、动画、SVG、文本等多方面的图形库,它是Android中图形系统的引擎。
         Skia的系统库为libskia.so、libskiagl.so(不同版本名称可能有出入)。而libjnigraphics.so图形库由于和其密切相关,一般会一同调用。
 
二、开始Skia工程
1)Java外壳
 
  1. public class SkiaView extends View { 
  2.  
  3.     /** TAG标识 */ 
  4.     private static final String TAG = "SkiaView"
  5.  
  6.     /** 载入动态库 */ 
  7.     static { 
  8.         try { 
  9.             System.loadLibrary("SkiaJni"); 
  10.         } catch(UnsatisfiedLinkError e) { 
  11.             Log.e(TAG, "Couldn't load native libs"); 
  12.             e.printStackTrace(); 
  13.         } 
  14.     } 
  15.  
  16.     public SkiaView(Context context) { 
  17.         super(context); 
  18.     } 
  19.  
  20.     @Override 
  21.     protected void onDraw(Canvas canvas) { 
  22.         super.onDraw(canvas); 
  23.         Log.i(TAG, "==draw start=="); 
  24.         // 调用本地方法 
  25.         native_renderCanvas(canvas); 
  26.         Log.i(TAG, "==draw end=="); 
  27.     } 
  28.  
  29.     /** 本地渲染画布方法 */ 
  30.     private native void native_renderCanvas(Canvas canvas); 
  31.  
 
2)C/C++封装
2.1)我的环境
         XP+Eclipse+Cygwin。并需要准备源码,这里是2.3.3_r1。
 
         准备源码,可参见《ubuntu 11.10下载和编译Android源码》。
 
2.2)建立工程

         1. 工程地址:AndroidSkia工程根目录jni文件夹。

         2. Build command:bash --login -c "cd $WORKSPACE/AndroidSkia && $NDKROOT/ndk-build"

         $WORKSPACE、$NDKROOT为工作空间、NDK路径。在Cygwin根目录\home\[your name]\ .bash_profile文件内配置。

         3.includes jni、skia等需要的头文件。当前如下:

         详细方式,参见《Android NDK基础样例》。
 
2.3)Android.mk
 
  1. LOCAL_PATH := $(call my-dir) 
  2. include $(CLEAR_VARS) 
  3.  
  4. MY_ANDROID_SOURCE = F:/01.软件/01.开发/05.android/android_sys_src/2.3.3_r1 
  5. MY_ANDROID_SYSLIB = $(MY_ANDROID_SOURCE)/out/target/product/generic/system/lib 
  6. #也可以在cygwin\home\Join\.bash_profile文件内配置,如下: 
  7. #   export MY_ANDROID_SOURCE="/cygdrive/f/..." 
  8. #   export MY_ANDROID_SYSLIB="/cygdrive/f/..." 
  9.  
  10. LOCAL_MODULE :libSkiaJni 
  11. LOCAL_SRC_FILES := \ 
  12.     jniLoad.cpp \ 
  13.     org_join_skia_SkiaView.cpp 
  14.  
  15. LOCAL_C_INCLUDES := \ 
  16.     $(MY_ANDROID_SOURCE)/dalvik/libnativehelper/include/nativehelper \ 
  17.     $(MY_ANDROID_SOURCE)/frameworks/base/include \ 
  18.     $(MY_ANDROID_SOURCE)/system/core/include \ 
  19.     $(MY_ANDROID_SOURCE)/frameworks/base/native/include \ 
  20.     $(MY_ANDROID_SOURCE)/frameworks/base/core/jni/android/graphics \ 
  21.     $(MY_ANDROID_SOURCE)/external/skia/include/core \ 
  22.     $(MY_ANDROID_SOURCE)/external/skia/include/config \ 
  23.     $(MY_ANDROID_SOURCE)/external/skia/include/images 
  24. #同时在工程Properties->C/C++ General->Paths and Symbols属性内include相应文件目录 
  25. #否则编程时找不到.h文件,不便写代码,但不会影响编译。而LOCAL_LDLIBS/LOCAL_LDFLAGS必需添加。 
  26.  
  27. #LOCAL_LDLIBS := -L$(MY_ANDROID_SYSLIB) -llog -ljnigraphics  -lskia -landroid_runtime 
  28. #也可以用以下方式指定so库 
  29. LOCAL_LDFLAGS := \ 
  30.     $(MY_ANDROID_SYSLIB)/liblog.so \ 
  31.     $(MY_ANDROID_SYSLIB)/libjnigraphics.so \ 
  32.     $(MY_ANDROID_SYSLIB)/libskia.so \ 
  33.     $(MY_ANDROID_SYSLIB)/libskiagl.so \ 
  34.     $(MY_ANDROID_SYSLIB)/libandroid_runtime.so 
  35.  
  36. include $(BUILD_SHARED_LIBRARY) 
 
         LOCAL_C_INCLUDES的头文件路径,第一个是jni的,最后三是lskia的,倒数四是ljnigraphics的,其他为基础的(如llog,除了某一是landroid_runtime的,忘了哪个==)。
 
         MY_ANDROID_SYSLIB也可从模拟器导出。
 
2.4)org_join_skia_SkiaView.cpp
 
  1. #include "jniLoad.h" 
  2.  
  3. #include <GraphicsJNI.h> 
  4. #include <SkCanvas.h> 
  5. #include <SkPaint.h> 
  6. #include <SkRect.h> 
  7. #include <SkColor.h> 
  8. #include <SkTypes.h> 
  9. #include <SkGraphics.h> 
  10.  
  11. static void drawFlag(SkCanvas* canv); 
  12.  
  13. static void native_renderCanvas(JNIEnv* env, jobject obj, jobject canvas) { 
  14.     MY_LOGI("==c method start=="); 
  15.  
  16.     SkCanvas* canv = GraphicsJNI::getNativeCanvas(env, canvas); 
  17.     if (!canv) { 
  18.         MY_LOGE("==canv is NULL=="); 
  19.         return
  20.     } 
  21.  
  22.     canv->save(); 
  23.     canv->translate(100, 100); 
  24.     drawFlag(canv); 
  25.     canv->restore(); 
  26.  
  27.     MY_LOGI("==c method end=="); 
  28.  
  29. /** 画旗帜 */ 
  30. static void drawFlag(SkCanvas* canv) { 
  31.     SkPaint* paint = new SkPaint(); 
  32.     paint->setFlags(paint->kAntiAlias_Flag); 
  33.  
  34.     SkRect* rect = new SkRect(); 
  35.     rect->set(0, 0, 200, 100); 
  36.     paint->setColor(SK_ColorRED); 
  37.     canv->drawRect(*rect, *paint); 
  38.  
  39.     paint->setColor(SK_ColorGRAY); 
  40.     paint->setStrokeWidth(10); 
  41.     canv->drawLine(5, 100, 5, 300, *paint); 
  42.  
  43.     paint->setTextSize(30); 
  44.     paint->setColor(SK_ColorBLUE); 
  45.     paint->setTextAlign(paint->kCenter_Align); 
  46.     const char* text = "Hello World"
  47.     canv->drawText(text, strlen(text), 100, 60, *paint); 
  48.  
  49. /** 
  50.  * JNI registration. 
  51.  */ 
  52. static JNINativeMethod methods[] = { { "native_renderCanvas"
  53.         "(Landroid/graphics/Canvas;)V", (void*) native_renderCanvas } }; 
  54.  
  55. int register_org_join_skia_SkiaView(JNIEnv *env) { 
  56.     return jniRegisterNativeMethods(env, "org/join/skia/SkiaView", methods, 
  57.             sizeof(methods) / sizeof(methods[0])); 
 
2.5)jniLoad.h
 
  1. #ifndef JNILOAD_H_ 
  2. #define JNILOAD_H_ 
  3.  
  4. #include <jni.h> 
  5. #include <utils/Log.h> 
  6.  
  7. #define MY_LOG_TAG "JNI_LOG" 
  8. #define MY_LOGI(...) __android_log_print(ANDROID_LOG_INFO, MY_LOG_TAG, __VA_ARGS__) 
  9. #define MY_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, MY_LOG_TAG, __VA_ARGS__) 
  10.  
  11. #ifdef __cplusplus 
  12. extern "C" { 
  13. #endif 
  14.  
  15. int jniRegisterNativeMethods(JNIEnv* env, const char* className, 
  16.         const JNINativeMethod* gMethods, int numMethods); 
  17.  
  18. #ifdef __cplusplus 
  19. #endif 
  20.  
  21. #endif /* JNILOAD_H_ */ 
 
2.6)jniLoad.cpp
 
  1. #include "jniLoad.h" 
  2.  
  3. #include <stdlib.h> 
  4.  
  5. int register_org_join_skia_SkiaView(JNIEnv *env); 
  6.  
  7. int jniRegisterNativeMethods(JNIEnv* env, const char* className, 
  8.         const JNINativeMethod* gMethods, int numMethods) { 
  9.  
  10.     jclass clazz; 
  11.     MY_LOGI("Registering %s natives\n", className); 
  12.     clazz = env->FindClass(className); 
  13.  
  14.     if (clazz == NULL) { 
  15.         MY_LOGE("Native registration unable to find class '%s'\n", className); 
  16.         return JNI_ERR; 
  17.     } 
  18.     if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { 
  19.         MY_LOGE("RegisterNatives failed for '%s'\n", className); 
  20.         return JNI_ERR; 
  21.     } 
  22.     return JNI_OK; 
  23.  
  24. jint JNI_OnLoad(JavaVM* vm, void* reserved) { 
  25.     JNIEnv* env = NULL; 
  26.     jint result = JNI_ERR; 
  27.  
  28.     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 
  29.         MY_LOGE("GetEnv failed!"); 
  30.         return result; 
  31.     } 
  32.  
  33.     MY_LOGI("loading . . ."); 
  34.  
  35.     if(register_org_join_skia_SkiaView(env) != JNI_OK) { 
  36.         MY_LOGE("can't load org_join_skia_SkiaView"); 
  37.         goto end; 
  38.     } 
  39.     /** 
  40.      * register others 
  41.      */ 
  42.  
  43.     MY_LOGI("loaded"); 
  44.  
  45.     result = JNI_VERSION_1_4; 
  46. end: 
  47.     return result; 
 
3)运行效果

 
三、Cygwin问题
         使用Cygwin时可能遇到的问题,之前都没提到过,现在补上==。
 

1)make 3.81 bug - error: multiple target patterns. Stop.

         下载http://www.cmake.org/files/cygwin/make.exe替换原来的make.exe
 
2)添加当前工程下的头文件和库文件

         添加include路径:project->properties->c/c++ build->settings->cygwin c compiler->includes->include paths->"${workspace_loc:/${ProjName}}"

         添加链接库:同上,在cygwin c linker->libraries下添加。

3)cygwin warning: MS-DOS style path detected:...

         添加环境变量CYGWIN=nodosfilewarning,可取消报警。
 
四、后记
         附件工程!
 

 

本文出自 “-_--___---_-” 博客,请务必保留此出处http://vaero.blog.51cto.com/4350852/790602



附件下载:
  

Android skia简单应用.zip

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于V8 JavaScript Engine的使用方法研究(三)
android中的surface
Android?图形系统剖析?(转)
Android中编译和使用LuaJIT开发应用
【Android】【转】JavaVM和JNIEnv
Android之JNI ERROR (app bug): accessed stale global reference 0xb39533f2 (index 19708 in a table of s
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服