Android skia instance

Source: Internet
Author: User
Tags cairo skia

# Include "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

 

       // 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

       // Device (Bitmap ).This operation des 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.

       // Transformed geometry is clipped by the intersection of all of

 

       // Clips in the stack.

       SkCanvas canvas (new SkDevice (bitmap ));

 

       // SkPaint class holds the style and color information about how

       // 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 );

 

       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,

              100 );

       Return 0;

}

Compilation Method:

G ++ \

       -I./include \

       -I./include/core \

       -I./include/images \                                                             

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

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

       -Lpng-lpthread-g

I made a brief comment to know the functions of these Apis starting with Sk, and the above sample program requires Skia to configure the canvas (SkCanvas) at the beginning ), then, a SkRect object r is used to specify the ARGB description so that it has different colors. Then, the displacement of the vector object is adjusted and drawn. As mentioned above, Skia is only a drawing engine and does not widely correspond to bottom-layer drawing devices such as PDF, X11, and GDI. Therefore, to facilitate the observation of drawing results, we use the built-in image codec of Skia to output the PNG image file.
"Test-skia", you should get a document: (there is no external frame and background color, but to be clear about the article presentation, add an additional drawing software)

 

Three overlapping rectangle objects of different colors are called through the following API:

       Paint. setARGB (255, 0,255, 0 );

       R. offset (20, 20 );

       Canvas. drawrect (R, paint );

Because skia and Cairo have a high configuration, you can refer to [Cairo: Documentation] to create the required background knowledge.

Reprinted,

Address: http://blog.sina.com.cn/s/blog_4a0a39c30100cog4.html

Another example:

# Include "SkBitmap. h "# include" SkDevice. h "# include" SkPaint. h "# include" SkRect. h "# include <cairo. h> int main () {SkBitmap bitmap; bitmap. setConfig (SkBitmap: kARGB_8888_Config, 100,100); bitmap. allocPixels (); SkDevice device (bitmap); SkCanvas canvas (& device); SkPaint paint; SkRect r; paint. setARGB (255,255,255,255); r. set (10, 10, 20, 20); canvas. drawRect (r, paint); paint. setARGB (255,255, 0, 0); r. offset (5, 5); canvas. drawRect (r, paint); paint. setARGB (255, 0, 0,255); r. offset (5, 5); canvas. drawRect (r, paint); {SkAutoLockPixels image_lock (bitmap); cairo_surface_t * surface = cairo_image_surface_create_for_data (unsigned char *) bitmap. getPixels (), CAIRO_FORMAT_ARGB32, bitmap. width (), bitmap. height (), bitmap. rowBytes (); cairo_surface_write_to_png (surface, "snapshot.png"); cairo_surface_destroy (surface);} return 0;} original address: http://www.linuxgraphics.cn/android/skia.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.