Simple android 2D skia library Application

Source: Internet
Author: User
Tags drawtext transparent color xml parser skia

1 skia plotting Overview

 

Skia is a graphic library of Google's underlying graphics, text, images, animations, and other aspects. It is the engine of the Image System in Android. Skia is used as a third-party software in the external Directory: external/skia /. The source files and some header files of skia are in the src directory, and the exported header files are in the include directory.

The following classes are mainly used for Drawing Images Using skia APIs:

Skbitmap, skcanvas, skpaint, and skrect. skbitmap is used to set pixels, skcanvas is written to bitmap, skpaint is used to set colors and styles, and skrect is used to draw rectangles. The implementation code is mainly in the src/Core directory.

 

2. Steps for drawing with skia

 

A) define a 32-Bit Bitmap and initialize it.
Skbitmap bitmap;

Bitmap. setconfig (skbitmap: kargb_8888_config, 200,200 );

Setconfig is used to set the bitmap format. The prototype is void setconfig (config, int width, int height, int rowbytes = 0)

Config is a Data Structure
Enum config {
Kno_config, // uncertain bitmap format
Ka1_config, // 1-bit (black, white) bitmap
Ka8_config, // 8-bit (black, white) bitmap
KIndex8_Config, // similar to the color index table in windows. For details, see the SkColorTable class structure.
KRGB_565_Config, // a 16-bit 565-Bit Bitmap. For more information, see the SkColorPriv. h file.
KARGB_4444_Config, // a 16-bit 4444-Bit Bitmap. For more information, see the SkColorPriv. h file.
KARGB_8888_Config, // 32-bit pixel 8888 format bitmap. For more information, see the SkColorPriv. h file.
KRLE_Index8_Config,
KConfigCount
};

B) Allocate space occupied by bitmap

Bitmap. allocPixels ()
In fact, allocPixels is an overloaded function, and its prototype is bool allocPixels (SkColorTable * ctable = NULL)
The ctable parameter is a color index table, which is generally NULL.

C) Specify the output device

SkCanvas canvas (new SkDevice (bitmap ));
Canvas is a multi-constructor. Its prototype is

Explicit SkCanvas (const SkBitmap & bitmap ),

Explicit SkCanvas (SkDevice * device = NULL)
The explicit it parameter indicates that type conversion is not allowed.
The output Device can be a context Device or a bitmap.

D) style of device painting
Paint paint;
SkRect r;
Paint. setARGB (255,255, 0, 0 );
R. set (25, 25,145,145 );
Canvas. drawRect (r, paint );
Paint can specify the color of the drawing, the size and alignment of the text, the encoding format, and so on. Because the previous bitmap format is set to kARGB_8888_Config, set the color setARGB (255,255, 0, 0). The first parameter is the transparent color channel. The other three parameters are R, G, and B. R sets the range to be drawn, and draws a square of the specified area through drawRect.

In this way, a red rectangle is successfully drawn.

SkCanvas mainly provides three painting functions:

A Basic Graph Drawing (such as the drawARGB and drawLine functions)

B. Drawing image files (drawBitmap function)

C text rendering (drawText function)

Related APIs include:

Canvas. drawRect (rect, paint );
Canvas. drawOval (oval, paint );
Canvas. drawCircle (x, y, radius, paint );
Canvas. drawRoundRect (rect, rx, ry, paint );
Canvas. drawPath (path, paint );
Canvas. drawBitmap (bitmap, x, y, & paint );
Canvas. drawBitmapRect (bitmap, & srcRect, dstRect, & paint );
Canvas. drawBitmapMatrix (bitmap, matrix, & paint );
Canvas. drawText (text, length, x, y, paint );
Canvas. drawPosText (text, length, pos [], paint );
Canvas. drawTextOnPath (text, length, path, paint );

E)

Routine

 

I) point, line, circle, and text

# Include "SkBitmap. h"

# Include "SkDevice. h"

# Include "SkPaint. h"

# Include "SkRect. h"

# Include "SkImageEncoder. h"

# Include "SkTypeface. h"

 

Using namespace std;

 

Int main ()

{

SkBitmap bitmap;

Bitmap. setConfig (SkBitmap: kARGB_8888_Config, 320,240 );

Bitmap. allocPixels ();

SkCanvas canvas (new SkDevice (bitmap ));

SkPaint paint;

// Draw points with red.

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

Paint. setStrokeWidth (4 );

Canvas. drawPoint (40, 30, paint );

Canvas. drawPoint (80, 60, paint );

Canvas. drawPoint (, paint );

// Draw a line with green.

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

Paint. setStrokeWidth (4 );

Canvas. drawLine (320,110, paint );

// Draw a circle with Bule.

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

Canvas. drawcircle (80,180, 50, paint );

// Draw text with red

Sktypeface * font = sktypeface: createfromfile ("simkai. TTF ");

If (font)

{

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

Paint. settypeface (font );

Paint. settextsize (24 );

Canvas. drawtext ("Hello! :) ", 8,200,180, paint );

}

Skimageencoder: encodefile ("snapshot.png", bitmap, skimageencoder: kpng_type, 100 );

Return 0;

}

After the program is executed, the following output is displayed:

 

 

 

Ii) image coding/Decoding

This routine currently supports only. PNG images and. jpg images.

# Include "skbitmap. H"

# Include "skdevice. H"

# Include "skpaint. H"

 

# Include "skrect. H"

# Include "skimageencoder. H"

# Include "skimagedecoder. H"

# Include <iostream>

 

Using namespace STD;

 

Int main ()

{

Int ret =-1;

Skbitmap bitmap;

 

// Skimagedecoder

Ret = skimagedecoder: decodefile ("./old.png", & Bitmap );

Cout <"Get the decode type =" <bitmap. config () <Endl;

 

// Skimageencoder

Ret = skimageencoder: encodefile ("new1.png", bitmap, skimageencoder: kpng_type, 100 );

Cout <"encode data to PNG result =" <RET <Endl;

 

Return 0;

}

SkImageDecoder: DecodeFile ("./old.png", & bitmap );

Convert png to a bitmap format and place the data in the bitmap variable.
SkImageEncoder: EncodeFile ("snapshot.png", bitmap, SkImageEncoder: kPNG_Type,/* Quality ranges from 0 .. 100 */100 );

Output the data encoding in bitmap. png format. The first parameter is the png file path, the second is the specified output bitmap, the third is the file type, and the fourth parameter specifies the quality of the output bitmap. The value range is 0 .. 100. The default value is 80.

 

3. image effects

The src/effects directory files mainly implement some special effects on graphics and images, including masks, relief, blur, filters, gradient, discretization, transparency, and various PATH effects.

 

4. Animation

The src/animator directory file mainly implements the animation effect of Skia, which is not supported by Android.

 

5. UI Library

The src/view directory creates a set of UI libraries.
Widgets include Window, Menu, TextBox, ListView, ProgressBar, Widget, ScrollBar, TagList, and Image.

 

6 others

A) src/gl Directory: skia calls OpenGL or OpenGL ES to implement 3D effects.
If MAC is defined, OpenGL is used. If Android is defined, the esgl 3D graphics library on the embedded system is used.

B) src/images Directory: Mainly SkImageDecoder, SkImageEncoder, and SkMovie. Mainly used to process images. The types of images that can be processed include BMP,

JPEG/PVJPEG, PNG, ICO, and SkMovie are used to process gif animation.

C) src/opts Directory: Performance Optimization code.

D) src/pdf Directory: Processes PDF documents and uses a fpdfemb library.

E) src/ports Directory: This part is the implementation of some skia interfaces on different systems and platform-related code, such as fonts, threads, and time. It mainly includes the following parts: font, Event, File, Thread, Time, XMLParser

These and Skia interfaces must be implemented for different operating systems.

F) src/svg Directory: vector image, which is not supported by Android.
SkSVGPath, SkSVGPolyline, SkSVGRect, SkSVGText, SkSVGLine, SkSVGImage, SkSVGEllipse, and so on.

G) src/text Directory :???

H) src/utils Directory: it is a helper tool class.
SkCamera, SkColorMatrix, SkOSFile, SkProxyCanvas, SkInterpolator, and other files.

I) src/xml: this is the part for processing xml data. skia only wraps the xml Parser here, the specific implementation of the xml Parser needs to be implemented based on different operating systems and host programs.

J) Third-party library

In addition to all of its own files, skia also uses some third-party libraries and contains many linux header files.

By analyzing the skia source program, it is found that skia mainly uses the following third-party libraries:
Zlib for data compression and decompression
Jpeglib for processing jpeg image encoding and decoding
Pnglib, encoding and decoding of png Images
Giflib, processing gif images
Fpdfemb, processing pdf documents

Skia also needs some header files in linux/unix (more files may be needed ):
Stdint. h
Unistd. h
Features. h
Cdefs. h
Stubs. h
Posix_opt.h
Types. h
Wordsize. h
Typesizes. h
Confname. h
Getopt. h
Mman. h

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.