In fact, drawing board this thing, a lot of places are used to, for example: in QQ there is a scribble drawing board, computer graphics tools, PS, and so on, these are used to draw the board, today I realized a small drawing board, share to everyone, hope to help you.
1. Principle Analysis: (very simple)
1. When the user touches the screen, start drawing
2. When the user moves, the starting position and the end position of the drawing are connected by a line
3. When the user hands off the screen, it is finished painting
2. First:
3. Sample source code
Package Com.zengtao.demo;import Java.io.file;import Java.io.fileoutputstream;import java.io.outputstream;import Android.annotation.suppresslint;import Android.graphics.bitmap;import Android.graphics.Bitmap.CompressFormat; Import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.os.bundle;import Android.os.environment;import Android.support.v7.app.actionbaractivity;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Actionbaractivity {private Button bt_save;private ImageView IV; Private Bitmap basebitmap;private Canvas canvas;private Paint paint;private int startx;private int starty;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); iv = (ImageView) fiNdviewbyid (R.ID.IV); bt_save = (Button) Findviewbyid (R.id.bt_save); Initpaint (); Initialize Brush Basebitmap = Bitmap.createbitmap (480, 640, Bitmap.Config.ARGB_8888); Create a Bitmapcanvas = new Canvas (BASEBITMAP) that can be modified; Create a canvas canvas.drawcolor (color.white); Iv.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motionevent.action_down://Gesture point under System.out.println ("Hand pointing");/* * Basebitmap = Bitmap.createbitmap (Iv.getwidth (), * iv.getheight (), Bitmap.Config.ARGB_8888); * Create a bitmap canvas = new Canvas (BASEBITMAP) that can be modified; * Create a canvas canvas.drawcolor (color.white); *///gets the starting position of the landing point startx = (int) event.getx (); starty = (int) event.gety (); Break;case motionevent.action_move:// Gestures move when System.out.println ("Finger moves"), int stopx = (int) event.getx (), int stopy = (int) event.gety (); Canvas.drawline (StartX , Starty, Stopx, stopy, paint); Iv.setimagebitmap (basebitmap);//regain the start position of the brush startx = (int) event.getx (); starty = (int) eve Nt.gety ();Break;case motionevent.action_up://Break when you sign up;} return true;}}); Bt_save.setonclicklistener (New Onclicklistener () {@SuppressLint ("showtoast") @Overridepublic void OnClick (View v) { try {File File = new file (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". jpg"); outputstream stream = new FileOutputStream (file); basebitmap.compress (Compressformat.jpeg, N, Stream); Stream.Close (); Toast.maketext (Mainactivity.this, "Save picture succeeded", 0). Show (); catch (Exception e) {toast.maketext (Mainactivity.this, "Save picture Failed", 0). Show ();}});} private void Initpaint () {paint = new paint ();p aint.setstrokewidth (5);p Aint.setcolor (Color.green);}}
4. Remember to add permissions and write permissions to the SDK
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
5. The above has completed a simple drawing board, we also try it, hope to help you.
2015 first: Android drawing board