Drawing Board implementation is actually very simple. We only need to use the Canvas class provided by android to implement simple painting.
Read the Code directly and write the comments clearly.
Public class MainActivity extends Activity {private ImageView iv; private Bitmap baseBitmap; private Canvas canvas; private Paint paint; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); this. iv = (ImageView) this. findViewById (R. id. iv); // create a blank image baseBitmap = Bitmap. createBitmap (480,640, Bitmap. config. ARGB_8888); // create a canvas Canvas = new canvas (baseBitmap); // The canvas background is a gray Canvas. drawColor (Color. GRAY); // create paint Paint = new paint (); // The paint color is red Paint. setColor (Color. RED); // The width of the painting is 5 pixels. setStrokeWidth (5); // first, draw the gray background on the canvas. drawBitmap (baseBitmap, new Matrix (), paint); iv. setImageBitmap (baseBitmap); iv. setOnTouchListener (new OnTouchListener () {int startX; int startY; @ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // get the coordinate startX = (int) event when the hand is pressed. getX (); startY = (int) event. getY (); break; case MotionEvent. ACTION_MOVE: // get the coordinate int stopX = (int) event after the mobile phone moves. getX (); int stopY = (int) event. getY (); // draw a line canvas between the start and end coordinates. drawLine (startX, startY, stopX, stopY, paint); // real-time update start coordinate startX = (int) event. getX (); startY = (int) event. getY (); iv. setImageBitmap (baseBitmap); break;} return true ;}});} public void save (View view) {try {File file = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". jpg "); OutputStream stream = new FileOutputStream (file); baseBitmap. compress (CompressFormat. JPEG, 100, stream); stream. close (); // simulate a broadcast to notify the system that the sdcard is mounted with Intent intent = new Intent (); intent. setAction (Intent. ACTION_MEDIA_MOUNTED); intent. setData (Uri. fromFile (Environment. getExternalStorageDirectory (); sendBroadcast (intent); Toast. makeText (this, "saved image succeeded", 0 ). show ();} catch (Exception e) {Toast. makeText (this, "failed to save the image", 0 ). show (); e. printStackTrace ();}}}
Simple layout File
Next permission
The last two pictures I drew on the high-end atmosphere ....