Create View:
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. rect;
Import Android. Graphics. rectf;
Import Android. View. view;
Public class myview extends view {
Private paint;
Public myview (context ){
Super (context );
// Todo auto-generated constructor stub
Paint = new paint ();
// Set the paint brush to be non-sawtooth (if not set, the effect will be poor)
Paint. setantialias (true );
// Set the background to always bright
This. setkeepscreenon (true );
// Set the paint brush color
Paint. setcolor (color. Red );
}
@ Override
Protected void ondraw (canvas ){
// Todo auto-generated method stub
Super. ondraw (canvas );
// Set the screen color
Canvas. drawcolor (color. White );
// The first two parameters mean the coordinates in the upper left corner of the rectangle.
// The last two parameters mean the coordinates at the bottom right corner of the rectangle.
Rect = new rect (30, 30, 50, 50 );
// Draw a rectangle
Canvas. drawrect (rect, paint );
// Rectf is only in the float format of a rectangle, but it is different from the rect precision.
Rectf = new rectf (70f, 30f, 90f, 90f );
// Draw an ellipse
Canvas. drawarc (rectf, 0,360, true, paint );
// Draw a circle. The third parameter is the radius.
Canvas. drawcircle (150, 30, 20, paint );
Float [] points = new float [] {200f, 10f, 200f, 40f, 300f, 30f, 400f, 70f };
Canvas. drawlines (points, paint );
// Select two points in the array of specific points to draw a straight line
// Canvas. drawlines (points, 1, 4, paint );
Canvas. drawtext ("Hello World", 230, 30, paint );
}
}
Use View:
Import Android. App. activity;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. OS. Bundle;
Import com. Main. myview;
Public class simplegraph extends activity {
Private Boolean iisstart = true;
Private myview;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Myview = new myview (this );
Setcontentview (myview );
// Refresh the view
New thread (New runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
While (iisstart)
{
Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println (E. tostring ());
}
// Use postinvalidate to directly update the interface in the thread
Myview. postinvalidate ();
}
}
}). Start ();
}
}