This article describes how to use the android graphics technology to randomly draw a color solid circle. Effect 1 is shown.
Figure 1
the core of the Program is the canvas, that is, the canvas object. To draw a graph on canvas , canvas On View . Therefore, you must first create a layout class, which is View . When the canvas is refreshed, the ondraw method is called to re-draw the canvas, from ondraw method parameters to obtain the canvas object.
Because all content is cleared during canvas re-painting, you need to use oneListThe variable saves the information about the previously drawn solid circle (Center Coordinate, radius, paint brush color), so that when the next solid circle is drawn, all the solid circles previously drawn are re-painted. If notListThe object retains the painting history. Only the last solid circle can be drawn. Next let's take a look at the layoutCode.
Package Mobile. Android. ch02.first;
ImportJava. util. arraylist;
ImportJava. util. List;
ImportAndroid. content. context;
ImportAndroid. Graphics. Canvas;
ImportAndroid. Graphics. paint;
ImportAndroid. View. view;
Public Class Circlecanvas Extends View
{
// Save draw history
Public List < Circleinfo > Mcircleinfos = New Arraylist < Circlecanvas. circleinfo > ();
// Class for saving information about solid circles
Public Static Class Circleinfo
{
Private Float X; // Horizontal coordinate of Center
Private Float Y; // Center ordinate
Private Float Radius; // Radius
Private Int Color; // Paint Brush color
Public Float Getx ()
{
Return X;
}
Public Void Setx ( Float X)
{
This . X = X;
}
Public Float Gety ()
{
Return Y;
}
Public Void Sety ( Float Y)
{
This . Y = Y;
}
Public Float Getradius ()
{
Return Radius;
}
Public Void Setradius ( Float Radius)
{
This . Radius = Radius;
}
Public Int Getcolor ()
{
Return Color;
}
Public Void Setcolor ( Int Color)
{
This . Color = Color;
}
}
Public Circlecanvas (context)
{
Super (Context );
}
// This method is called when the canvas is re-painted. Canvas indicates the canvas object. You can draw basic images on this object.
@ Override
Protected Void Ondraw (canvas)
{
Super . Ondraw (canvas );
// Redraw all solid circles based on the saved drawing history
For (Circleinfo: mcircleinfos)
{
Paint paint = New Paint ();
// Set paint color
Paint. setcolor (circleinfo. getcolor ());
// Draw a solid circle
Canvas. drawcircle (circleinfo. getx (), circleinfo. Gety (), circleinfo. getradius (), paint );
}
}
}
Next we will write the main program. When creating a project, you must enter a"Create activity", Because we entered"Main". Therefore, the generated main class isMain. Java. OpenMain. JavaFile, enter the following code.
package mobile. android. ch02.first;
ImportJava. util. Random;
ImportMobile. Android. First. circlecanvas. circleinfo;
ImportAndroid. App. activity;
ImportAndroid. Graphics. color;
ImportAndroid. OS. Bundle;
ImportAndroid. View. view;
ImportAndroid. View. viewgroup;
ImportAndroid. View. viewgroup. layoutparams;
Public Class Main Extends Activity
{
Private Circlecanvas mcirclecanvas; // Define a layout class
@ Override
Public Void Oncreate (bundle savedinstancestate)
{
Super . Oncreate (savedinstancestate );
// Load the layout file (the main. xml file configured in section 2.2.3)
Viewgroup = (Viewgroup) getlayoutinflater (). Inflate (R. layout. Main, Null );
Mcirclecanvas = New Circlecanvas ( This ); // Create a circlecanvas object
// Add the circlecanvas object to the view on the current interface (below the two buttons)
Viewgroup. addview (mcirclecanvas, New Layoutparams (layoutparams. fill_parent, 350 ));
Setcontentview (viewgroup );
}
// Start to draw a circle randomly (click the event of the first button)
Public Void Onclick_drawrandomcircle (view)
{
Random random = New Random ();
Float Randomx = ( Float )( 100 + Random. nextint ( 100 )); // Generate random center abscissa (100 to 200)
Float Randomy = ( Float )( 100 + Random. nextint ( 100 )); // Randomly generate center ordinate (100 to 200)
Float Randomradius = ( Float )( 20 + Random. nextint ( 40 )); // Random circle radius (20 to 60)
Int Randomcolor = 0 ;
// Generates a random number ranging from 0 to 100. If the random number is greater than 50, the paint brush color is blue.
If (Random. nextint ( 100 ) > 50 )
{
Randomcolor = Color. blue;
}
Else
{
// Generates a random number ranging from 0 to 100. If the random number is greater than 50, the paint brush color is red.
If (Random. nextint ( 100 ) > 50 )
Randomcolor = Color. Red;
// Otherwise, the paint brush color is green.
Else
Randomcolor = Color. Green;
}
Circleinfo = New Circleinfo ();
Circleinfo. setx (randomx );
Circleinfo. sety (randomy );
Circleinfo. setradius (randomradius );
Circleinfo. setcolor (randomcolor );
Mcirclecanvas. mcircleinfos. Add (circleinfo ); // Add the currently drawn solid circle information to the list object
Mcirclecanvas. invalidate (); // Re-paint the canvas
}
// Clear the canvas (click the event of the second button)
Public Void Onclick_clear (view)
{
Mcirclecanvas. mcircleinfos. Clear (); // Clear draw history
Mcirclecanvas. invalidate (); // Re-paint the canvas
}
}
This article is selected from the android Development Authority guide. If you need to reprint it, please indicate the source.