Home | History | Annotate | Download | only in jni
      1 #include <math.h>
      2 #include <jni.h>
      3 #include <android/bitmap.h>
      4 
      5 #include "SkCanvas.h"
      6 #include "SkGraphics.h"
      7 #include "SkSurface.h"
      8 #include "SkString.h"
      9 #include "SkTime.h"
     10 
     11 
     12 /**
     13  * Draws something into the given bitmap
     14  * @param  env
     15  * @param  thiz
     16  * @param  dstBitmap   The bitmap to place the results of skia into
     17  * @param  elapsedTime The number of milliseconds since the app was started
     18  */
     19 extern "C"
     20 JNIEXPORT void JNICALL Java_com_example_HelloSkiaActivity_drawIntoBitmap(JNIEnv* env,
     21         jobject thiz, jobject dstBitmap, jlong elapsedTime)
     22 {
     23     // Grab the dst bitmap info and pixels
     24     AndroidBitmapInfo dstInfo;
     25     void* dstPixels;
     26     AndroidBitmap_getInfo(env, dstBitmap, &dstInfo);
     27     AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels);
     28 
     29     SkImageInfo info = SkImageInfo::MakeN32Premul(dstInfo.width, dstInfo.height);
     30 
     31     // Create a surface from the given bitmap
     32     SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterDirect(info, dstPixels, dstInfo.stride));
     33     SkCanvas* canvas = surface->getCanvas();
     34 
     35     // Draw something "interesting"
     36 
     37     // Clear the canvas with a white color
     38     canvas->drawColor(SK_ColorWHITE);
     39 
     40     // Setup a SkPaint for drawing our text
     41     SkPaint paint;
     42     paint.setColor(SK_ColorBLACK); // This is a solid black color for our text
     43     paint.setTextSize(SkIntToScalar(30)); // Sets the text size to 30 pixels
     44     paint.setAntiAlias(true); // We turn on anti-aliasing so that the text to looks good.
     45 
     46     // Draw some text
     47     SkString text("Skia is Best!");
     48     SkScalar fontHeight = paint.getFontSpacing();
     49     canvas->drawText(text.c_str(), text.size(), // text's data and length
     50                      10, fontHeight,            // X and Y coordinates to place the text
     51                      paint);                    // SkPaint to tell how to draw the text
     52 
     53     // Adapt the SkPaint for drawing blue lines
     54     paint.setAntiAlias(false); // Turning off anti-aliasing speeds up the line drawing
     55     paint.setColor(0xFF0000FF); // This is a solid blue color for our lines
     56     paint.setStrokeWidth(SkIntToScalar(2)); // This makes the lines have a thickness of 2 pixels
     57 
     58     // Draw some interesting lines using trig functions
     59     for (int i = 0; i < 100; i++)
     60     {
     61         float x = (float)i / 99.0f;
     62         float offset = elapsedTime / 1000.0f;
     63         canvas->drawLine(sin(x * M_PI + offset) * 800.0f, 0,   // first endpoint
     64                          cos(x * M_PI + offset) * 800.0f, 800, // second endpoint
     65                          paint);                               // SkPapint to tell how to draw the line
     66     }
     67 
     68     // Unlock the dst's pixels
     69     AndroidBitmap_unlockPixels(env, dstBitmap);
     70 }
     71