Draw a line above the android Screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.