The job is as follows: draw any line 2011-12-1 on the android screen. You can see it on the Internet. For more information, see. modify it.
Package Feng. f121.drawline; // The package name created by myself. Each person has a different package.
Import java. Security. publickey;
Import Android. R. Integer;
Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. Paint. style;
Import Android. Graphics. path;
Import Android. util. attributeset;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. widget. Button;
// Create a class to inherit the view
Public class drawl extends view {
Private int mov_x; // declare the start Coordinate
Private int mov_y;
Private paint; // specifies the paint brush.
Private canvas; // canvas
Private Bitmap bitmap; // bitmap
Private int blcolor;
Public drawl (context ){
Super (context );
Paint = new paint (paint. dither_flag); // create a paint brush
Bitmap = bitmap. createbitmap (480,854, bitmap. config. argb_8888); // you can specify the width and height of a bitmap.
Canvas = new canvas ();
Canvas. setbitmap (Bitmap );
Paint. setstyle (style. Stroke); // you can specify a non-filled attribute.
Paint. setstrokewidth (5); // pen width 5 pixels
Paint. setcolor (color. Red); // set it to red
Paint. setantialias (true); // do not display the Sawtooth
}
// Draw a bitmap
@ Override
Protected void ondraw (canvas ){
// Super. ondraw (canvas );
Canvas. drawbitmap (bitmap, 0, 0, null );
}
// Touch event
@ Override
Public Boolean ontouchevent (motionevent event ){
If (event. getaction () = motionevent. action_move) {// if you drag
Canvas. drawline (mov_x, mov_y, event. getx (), event. Gety (), paint); // draw a line
Invalidate ();
}
If (event. getaction () = motionevent. action_down) {// if you click
Mov_x = (INT) event. getx ();
Mov_y = (INT) event. Gety ();
Canvas. drawpoint (mov_x, mov_y, paint); // painting point
Invalidate ();
}
Mov_x = (INT) event. getx ();
Mov_y = (INT) event. Gety ();
Return true;
}
}
In Activity
Public class drawline extends activity {
Private drawl bdrawl;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Bdrawl = new drawl (this );
Setcontentview (bdrawl); // display the view in activity.
}
Help: http://blog.csdn.net/huangjialiang1986/article/details/6219483