Write a basic commonly used drawing Applet
The write is messy, just to practice
Directly Add code
package mars.com;import android.app.Activity;import android.os.Bundle;public class Demo_Activity extends Activity {private MyGraphics myGraphics = null;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.myGraphics = new MyGraphics(this);setContentView(myGraphics);}}
Drawing java files
Package mars.com; 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. graphics. typeface; import android. graphics. drawable. bitmapdrawable; import android. view. view; public class mygraphics extends view implements runnable {PR Ivate paint = NULL; Public mygraphics (context) {super (context); paint = new paint (); // build object new thread (this ). start (); // thread start} // override the ondraw method protected void ondraw (canvas) {super. ondraw (canvas); paint. setcolor (color. red); // set the paint color for the paint brush. setalpha (100); // sets the transparency of the paint brush. The value range is 0-255canvas.drawcolor (color. white); // set the canvas color. drawline (50, 50,450, 50, paint); // draw a straight canvas. drawrect (100,100, 2 00,600, paint); // draw a rectangle painting. setalpha (50); // you can specify the paint transparency. setstyle (style. stroke); // set the paint brush to a hollow paint. setstrokewidth (3); // you can specify the canvas width. drawrect (300,100,400,600, paint); // draw a rectangle painting. settypeface (typeface. sans_serif); // set the font style paint. settextsize (20); // set the size of the paint brush font canvas. drawtext ("Wang yuchao", 10,350, paint); // you can specify the font size of the paint brush. moveTo (50,100); // connect to the next path. lineto (50,300); Path. li Neto (20, 60); Path. lineto (60, 80); Path. lineto (66,300); canvas. drawpath (path, paint); // draw any polygon // draw the image Bitmap bitmap = NULL; bitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. ic_launcher )). getbitmap (); canvas. drawbitmap (bitmap, 50,150, null);} // overload the run method public void run () {While (! Thread. currentthread (). isinterrupted () {try {thread. sleep (1000);} catch (exception e) {thread. currentthread (). interrupt () ;}postinvalidate (); // update interface }}}
Running result
In addition to these simple operations, Android also has many operations, but I think the most important thing is paint and canvas. Only by mastering these two classes can we draw other images better.
For example, you can draw multiple straight lines, crop, rotate the canvas, lock the canvas, draw points, set the lower arc, and set the inclination. For more information, see the android API. I will not list them here.