Simple Example of canvas and paint in Android

Source: Internet
Author: User

 

Package com. Canvas. paint;

Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. lineargradient;
Import Android. Graphics. paint;
Import Android. Graphics. path;
Import Android. Graphics. rectf;
Import Android. Graphics. shader;
Import Android. OS. Bundle;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;

Public class mainactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (New myview (this ));
}
Class myview extends surfaceview implements surfaceholder. Callback {
Private surfaceholder mholder;
Private canvas;
Public myview (context ){
Super (context );
Mholder = getholder ();
Mholder. addcallback (this); // sets the surface lifecycle callback.
}
@ Override
Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){
}
@ Override
Public void surfacecreated (surfaceholder holder ){
Canvas = mholder. lockcanvas (); // obtain the canvas
Canvas. drawcolor (color. White); // set the canvas background to white.
Paint mpaint = new paint ();
// Draw a rectangle
Mpaint. setcolor (color. Yellow );
Canvas. drawrect (100,200,200,300, mpaint );
Canvas. Save (); // every time a picture is drawn, it is stored as a memo to ensure that it does not affect other images.
// Rotate
Canvas. Rotate (45); // Rotation Angle 45 degrees
Mpaint. setcolor (color. Green );
Canvas. drawrect (150, 10,200, 60, mpaint); // draw a rectangle
Canvas. Save ();
// Draw a gradient circle
Shader mshader = new lineargradient (0, 0, 100,100,
New int [] {color. Red, color. Green, color. Blue, color. Yellow },
Null, shader. tilemode. Repeat); // use the shader
Mpaint. setshader (mshader );
Canvas. drawcircle (500,150, 80, mpaint );
/*
Canvas. Save ();
// Draw a triangle
Mpaint. setstyle (paint. style. Stroke); // you can specify
Mpaint. setstrokewidth (3); // you can specify the width of the P outer frame.
Path = New Path ();
Path. moveTo (10,330 );
Path. lineto (70,330 );
Path. lineto (40,270 );
Path. Close ();
Canvas. drawpath (path, mpaint );
Canvas. Save ();
// Draw a trapezoid
Mpaint. setantialias (true); // remove the edges.
Mpaint. setcolor (color. Blue );
Mpaint. setstyle (paint. style. Fill); // set solid
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, mpaint );
Canvas. Save ();
// Draw an elliptical shape
Mpaint. setantialias (true); // remove the edges.
Mpaint. setcolor (color. Red );
Rectf Re = new rectf (10,220, 70,250 );
Canvas. drawoval (Re, mpaint );
*/
Canvas. Restore ();
Mholder. unlockcanvasandpost (canvas); // unlock and submit the canvas view after drawing.
}
@ Override
Public void surfacedestroyed (surfaceholder holder ){
// Todo auto-generated method stub
}
}
}

 

Related Article

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.