Simple canvas for android

Source: Internet
Author: User
Tags gety

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 ....






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.