Example
"How do I draw 2D images on a computer ?" This is a problem that is often mentioned in many android game development. In the android SDK, Java graphics2d functions are not available, but classes under Android. graphics are used to draw 2D vector diagrams. This package provides many classes and methods for Drawing Images on mobile phones, including canvas, while painting (Android. graphics. paint) class is like a color pencil. Different coincidences can be used to draw vector images of different colors and effects.
This example uses different paint object setting values to draw a variety of geometric shapes of hollow, practical, and gradient on the CAVAS canvas.
If
Sample program
The main program inherits from Android. view. in the myview class of the view, override the ondraw () method of the myview and start the painting. In ondraw (), draw the Ry on the canvas with the paint, and use the paint. setcolor () is used to change the image color and paint the image. setstyle () settings to control whether the drawn image is hollow or solid. The last section of the program is to write text directly on the canvas. With the attribute settings in the paint object, there will be different appearance modes.
Package com. graphices;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. rectf;
Import Android. Graphics. path;
Import Android. Graphics. shader;
Import Android. Graphics. lineargradient;
Public class graphicesactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
/* Set contentview to custom myview */
Myview = new myview (this );
Setcontentview (myview );
}
/* Customize the myview that inherits the view */
Private class myview extends View
{
Public myview (context ){
Super (context );
}
/* Override ondraw ()*/
@ Override
Protected void ondraw (canvas)
{
Super. ondraw (canvas );
/* Set the background to white */
Canvas. drawcolor (color. White );
Paint paint = new paint ();
/* De-sawtooth */
Paint. setantialias (true );
/* Set the paint color */
Paint. setcolor (color. Red );
/* Set the paint style to stroke: hollow */
Paint. setstyle (paint. style. Stroke );
/* Set the width of the paint outer frame */
Paint. setstrokewidth (3 );
/* Draw a hollow circle */
Canvas. drawcircle (40, 40, 30, paint );
/* Draw a hollow square */
Canvas. drawrect (10, 90, 70,150, paint );
/* Draw a hollow rectangle */
Canvas. drawrect (10,170, 70,200, paint );
/* Draw a hollow elliptical shape */
Rectf Re = new rectf (10,220, 70,250 );
Canvas. drawoval (Re, paint );
/* Draw a hollow triangle */
Path = New Path ();
Path. moveTo (10,330 );
Path. lineto (70,330 );
Path. lineto (40,270 );
Path. Close ();
Canvas. drawpath (path, paint );
/* Draw a hollow trapezoid */
Path path1 = New Path ();
Path1.moveto (10,410 );
Path1.lineto (70,410 );
Path1.lineto (55,350 );
Path1.lineto (25,350 );
Path1.close ();
Canvas. drawpath (path1, paint );
/* Set the style of paint to fill: solid */
Paint. setstyle (paint. style. Fill );
/* Set the paint color */
Paint. setcolor (color. Blue );
/* Draw a solid circle */
Canvas. drawcircle (, 40, 30, paint );
/* Draw a solid square */
Canvas. drawrect (90, 90,150,150, paint );
/* Draw a solid rectangle */
Canvas. drawrect (90,170,150,200, paint );
/* Draw a solid ellipse */
Rectf re2 = new rectf (90,220,150,250 );
Canvas. drawoval (re2, paint );
/* Draw a solid triangle */
Path path2 = New Path ();
Path2.moveto (90,330 );
Path2.lineto (150,330 );
Path2.lineto (120,270 );
Path2.close ();
Canvas. drawpath (path2, paint );
/* Draw a solid trapezoid */
Path path3 = New Path ();
Path3.moveto (90,410 );
Path3.lineto (150,410 );
Path3.lineto (135,350 );
Path3.lineto (105,350 );
Path3.close ();
Canvas. drawpath (path3, paint );
/* Set gradient */
Shader mshader = new lineargradient (0, 0, 100,100,
New int [] {color. Red, color. Green, color. Blue, color. Yellow },
Null, shader. tilemode. Repeat );
Paint. setshader (mshader );
/* Draw a gradient circle */
Canvas. drawcircle (, 30, paint );
/* Draw a gradient square */
Canvas. drawrect (170, 90,230,150, paint );
/* Draw a gradient rectangle */
Canvas. drawrect (170,170,230,200, paint );
/* Draw a gradient Oval */
Rectf RE3 = new rectf (170,220,230,250 );
Canvas. drawoval (RE3, paint );
/* Draw a gradient triangle */
Path path4 = New Path ();
Path4.moveto (170,330 );
Path4.lineto (230,330 );
Path4.lineto (200,270 );
Path4.close ();
Canvas. drawpath (path4, paint );
/* Draw a gradient trapezoid */
Path path5 = New Path ();
Path5.moveto (170,410 );
Path5.lineto (230,410 );
Path5.lineto (215,350 );
Path5.lineto (185,350 );
Path5.close ();
Canvas. drawpath (path5, paint );
/* Write */
Paint. settextsize (24 );
Canvas. drawtext (getresources (). getstring (R. String. str_text1), 240, 50, paint );
Canvas. drawtext (getresources (). getstring (R. String. str_text2), 240,120, paint );
Canvas. drawtext (getresources (). getstring (R. String. str_text3), 240,190, paint );
Canvas. drawtext (getresources (). getstring (R. String. str_text4), 240,250, paint );
Canvas. drawtext (getresources (). getstring (R. String. str_text5), 240,320, paint );
Canvas. drawtext (getresources (). getstring (R. String. str_text6), 240,390, paint );
}
}
}