Android ApiDemos example (51): Graphics-& gt; AlphaBitmap

Source: Internet
Author: User
Tags drawtext

We will introduce the graphic-related examples in ApiDemos from today. The Android platform provides powerful 2D, 3D Graphics, and animation support. If you have a development background in Windows GDI +, you can quickly get started with the relationship between Android 2D and GDI. If you still like the GDI style, the Web tracking software also provides an efficient two-dimensional Graphics library that is independent of the free platform. Before you begin to learn about Android Graphics development, let's briefly introduce the most important classes in Android 2D Graphics:

Canvas: used to define a Canvas. All drawing operations occur on this Canvas object. It mainly provides:

Draw basic ry, such as Shape and Path.
Draw image bitmaps.
Draw text.
Crop images.
Coordinate Transformation.
Paint: it can be seen as all the Color palette during painting. It defines the Paint Brush, Paint Brush, Color, Font, and other painting attributes corresponding to GDI, such as Brush, Color, Pen, and Font.

Bitmap: Each Canvas corresponds to a Bitmap, which is used to store the drawing results (pixel values) of the Canvas ). Android also provides methods to directly operate pixels in Bitmap.

Therefore, to draw a thing on Android, four parts are required: a Bitmap is used to store the pixel value, a Canvas is used to define the Drawing operation, and a Paint is used to define the color, Paint brush, Paint brush, and other attributes of the drawing, the last one is the drawing itself (such as a rectangle or line segment ).

In general, if you need to draw a graph on the screen, you can derive a subclass from View and reload the onDraw (Canvas canvas) method of view. OnDraw is executed every time the screen is refreshed. In onDraw, you can use the input canvas object to draw the required image.

In this example, AlphaBitmap uses Bitmap, Shader, Canvas, and Paint classes to demonstrate the image Alpha channel, which is the image transparency.

[Java]
MBitmap2 = mBitmap. extractAlpha ();
MBitmap2 = mBitmap. extractAlpha ();
MBitmap2 uses extractAlpha to retrieve only the Alpha channel of mBitmap. For an image, mBitmap2 can be understood as the Mask part of the image, indicating that this Part has image content. In this example, the red part is the Mask of the ApiDemos icon, which is the outline of the ApiDemos icon.

Shader can be understood as the painting brush class in GDI. This example defines a linear gradient painting brush:

[Java]
MShader = new LinearGradient (0, 0,100, 70, new int [] {
Color. RED, Color. GREEN, Color. BLUE },
Null, Shader. TileMode. MIRROR );
MShader = new LinearGradient (0, 0,100, 70, new int [] {
Color. RED, Color. GREEN, Color. BLUE },
Null, Shader. TileMode. MIRROR );
In the area (0, 0)-(, 70) with three colors of red, green, blue uniform linear gradient.

Let's take a look at how mBitmap3 is drawn.

[Java]
MBitmap3 = Bitmap. createBitmap (200,200,
Bitmap. Config. ALPHA_8 );
DrawIntoBitmap (mBitmap3 );
...
Private static void drawIntoBitmap (Bitmap bm ){
Float x = bm. getWidth ();
Float y = bm. getHeight ();
Canvas c = new Canvas (bm );
Paint p = new Paint ();
P. setAntiAlias (true );

P. setAlpha (0x80 );
C. drawCircle (x/2, y/2, x/2, p );

P. setAlpha (0x30 );
P. setXfermode (new porterduxfermode (PorterDuff. Mode. SRC ));
P. setTextSize (60 );
P. setTextAlign (Paint. Align. CENTER );
Paint. FontMetrics fm = p. getFontMetrics ();
C. drawText ("Alpha", x/2, (y-fm.ascent)/2, p );
}
MBitmap3 = Bitmap. createBitmap (200,200,
Bitmap. Config. ALPHA_8 );
DrawIntoBitmap (mBitmap3 );
...
Private static void drawIntoBitmap (Bitmap bm ){
Float x = bm. getWidth ();
Float y = bm. getHeight ();
Canvas c = new Canvas (bm );
Paint p = new Paint ();
P. setAntiAlias (true );
 
P. setAlpha (0x80 );
C. drawCircle (x/2, y/2, x/2, p );
 
P. setAlpha (0x30 );
P. setXfermode (new porterduxfermode (PorterDuff. Mode. SRC ));
P. setTextSize (60 );
P. setTextAlign (Paint. Align. CENTER );
Paint. FontMetrics fm = p. getFontMetrics ();
C. drawText ("Alpha", x/2, (y-fm.ascent)/2, p );
}
MBitmap3 is generated by code instead of reading from the resource file. Bitmap. Config. ALPHA_8 is stored as a separate Alpha channel, and is usually used as a Mask Bitmap of other images.

Canvas c = new Canvas (bm); you can create a Canvas object for Bitmap. All the images drawn on this Canvas will be stored in the corresponding Bitmap, generally, you can use setAlpha to set the Alpha (transparency) value of the current Paint in Double Buffer or to draw a graph in the background. 0 indicates full transparency, and 255 indicates opacity. The greater the Alpha value, the lower the transparency.

In addition to setting opacity, you can also set attributes such as font size and Shader.

Finally, let's take a look at the onDraw (Canvas canvas) code:

[Java]
Canvas. drawColor (Color. WHITE );

Paint p = new Paint ();
Float y = 10;

P. setColor (Color. RED );
Canvas. drawBitmap (mBitmap, 10, y, p );
Y + = mBitmap. getHeight () + 10;
Canvas. drawBitmap (mBitmap2, 10, y, p );
Y + = mBitmap2.getHeight () + 10;
P. setShader (mShader );
Canvas. drawBitmap (mBitmap3, 10, y, p );
Canvas. drawColor (Color. WHITE );
 
Paint p = new Paint ();
Float y = 10;
 
P. setColor (Color. RED );
Canvas. drawBitmap (mBitmap, 10, y, p );
Y + = mBitmap. getHeight () + 10;
Canvas. drawBitmap (mBitmap2, 10, y, p );
Y + = mBitmap2.getHeight () + 10;
P. setShader (mShader );
Canvas. drawBitmap (mBitmap3, 10, y, p );


The mBitmap icon of ApiDemo is displayed in the upper left corner. MBitmap2 stores the Alpha channel value in mBitmap. If you call Canvas. drawBitmap (mBitmap2), The mBitmap2 color will display the value passed in by the Paint (in this example, It is red), you can change to its desired color, the mBitmap2 color will also change. For mBitmap3, only one circle and one line of text are drawn in drawIntoBitmap. However, because ALPHA_8 is used during creation, similar to mBitmap2, it also uses a color defined by Paint, however, the color defined by Paint is linear gradient defined by Shader, so there is a colorful background.


 

Author: mapdigit
 

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.