Package com. Android. image. Demo;
Import java. Io. inputstream;
Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. lineargradient;
Import Android. Graphics. paint;
Import Android. Graphics. path;
Import Android. Graphics. porterduduxfermode;
Import Android. Graphics. shader;
Import Android. Graphics. typeface;
Import Android. OS. Bundle;
Import Android. View. view;
Public class alphabitmap extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (New sampleview (this ));
}
Private Static class sampleview extends view {
Private bitmap mbitmap;
Private bitmap mbitmap2;
Private bitmap mbitmap3;
Private shader mshader;
Private Static void drawintobitmap (Bitmap BM ){
Float x = BM. getwidth ();
Float y = BM. getheight ();
Canvas c = new canvas (BM );
Paint P = new paint ();
/* A method of painting class for smooth edges. True indicates smooth edges */
P. setantialias (true );
P. setalpha (0x80); // set the color transparency to hexadecimal 80 (translucent), 0x00 transparent, 0xff opaque
/* Draw a tangent circle in the Bitmap matrix area */
C. drawcircle (X/2, Y/2, X/2, P );
P. setalpha (0x30 );
/* Use the specified porterduff model to create xformode and porterduff. mode. SRC
* Indicates that the text to be drawn below should be on the upper layer of the circle drawn above
*/
P. setxfermode (New porterduxfermode (Android. Graphics. porterduff. mode. SRC ));
P. settextsize (60 );
/* Paint. Align is an enumeration class for text alignment.
* Center indicates text Center
* Left indicates alignment.
* Right indicates the right alignment.
*/
P. settextalign (paint. Align. center );
/* Fontmetrics is a class of font metrics that describes various font metrics for a given text size.
* Ascent indicates the distance above the baseline.
* Bottom indicates the maximum distance to the baseline. It is the lowest font type.
* Descent indicates the distance to the baseline.
* Leading: the distance from the space character to the baseline. The value is 0.
**/
Paint. fontmetrics fm = P. getfontmetrics ();
C. drawtext ("Alpha", X/2, (y-fm.ascent)/2, P );
}
Public sampleview (context ){
Super (context );
Setfocusable (true );
/* Obtain the input stream of the resource file */
Inputstream is = context. getresources ()
. Openrawresource (R. drawable. qq );
/* Bitmapfactory is a factory class of Bitmap.
* Create bitmap objects from various bitmap objects, including files, streams, and byte arrays.
**/
Mbitmap = bitmapfactory. decodestream (is );
/* This method of extractalpha () bitmap is extracted
* Creates a new Bitmap Based on the transparent channel value of the original bitmap */
Mbitmap2 = mbitmap. extractalpha ();
/* Create a bitmap by configuring the bitmap width and height.
* Bitmap. config is an internal enumeration class that indicates the color configuration of Bitmap.
* Its color settings include alpha_8, argb_4444, argb_8888, and rgb_565.
**/
Mbitmap3 = bitmap. createbitmap (200,200, bitmap. config. alpha_8 );
Drawintobitmap (mbitmap3 );
/* The lineargradient class is a subclass of shader, which implements a linear gradient change.
* The color gradient variation line of the straight-line type from (0, 0) to (, 70 ).
* This gradient variation varies evenly between red, green, and blue.
* Shader. tilemode is the color change mode that exceeds the gradient line.
* When the clamp is fixed, the color of the part that exceeds the original boundary (gradient line) is drawn by the boundary color.
* Repeat uses the colorant color in both the horizontal and vertical directions, but the boundary is distinct.
* Mirror repeats the color of the shadow in both the horizontal and vertical directions, and the exchanged image color makes the adjacent
* The near colors are always the same; the colors are about the gradient line image.
**/
Mshader = new lineargradient (0, 0,100, 70,
New int [] {color. Red, color. Green, color. Blue },
Null, shader. tilemode. Mirror );
}
@ Override
Protected void ondraw (canvas ){
Canvas. drawcolor (color. White );
Paint P = new paint ();
Float y = 10;
/* Set the paint brush color to red */
P. setcolor (color. Red );
/* Call the drawbitmap method of the canvas to draw the specified bitmap with the specified paint brush at the specified position */
Canvas. drawbitmap (mbitmap, 10, Y, P );
/* Set the Y coordinate value of the next bitmap */
Y + = mbitmap. getheight () + 10;
Canvas. drawbitmap (mbitmap2, 10, Y, P );
Y + = mbitmap2.getheight () + 10;
/* Set the color of the paint brush */
P. setshader (mshader );
Canvas. drawbitmap (mbitmap3, 10, Y, P );
/* This class mainly loads geometric paths such as linear curves. */
Path = New Path ();
/* Gradient line on the painting */
Path. moveTo (0, 0 );
Path. lineto (100,70 );
P. setcolor (color. Red );
/* Enumeration class of paint. Style painter styles
* Only stroke shapes are drawn by stroke.
* Fill filling
* Fill_and_stroke: paint and fill with paint brushes
**/
P. setstyle (paint. style. Stroke );
/* Use the specified path and the path required by the specified image painter */
Canvas. drawpath (path, P );
}
}
}