Image Painting Board for Android,

Source: Internet
Author: User
Tags gety

Image Painting Board for Android,

This article focuses on the implementation of image drawing board for Android

Design Project Layout:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <ImageView android: layout_above =" @ + id/bt "android: id =" @ + id/iv "android: layout_width =" fill_parent "android: layout_height = "fill_parent"/> <Button android: id = "@ + id/bt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: onClick = "save" android: text = "save image"/> </RelativeLayout>

First, implement the drawing function:

Public class MainActivity extends Activity {private ImageView iv; private Bitmap baseBitmap; private Canvas canvas; private Paint paint; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv = (ImageView) findViewById (R. id. iv); paint = new Paint (); paint. setStrokeWidth (5); paint. setColor (Color. GREEN); // create a modifiable bitmap baseBitmap = Bitmap. createBitmap (320,360, Bitmap. config. ARGB_8888); System. out. println ("Image Width:" + iv. getWidth (); System. out. println ("Image Height:" + iv. getHeight (); canvas = new Canvas (baseBitmap); canvas. drawColor (Color. WHITE); // knows the trajectory of the user's finger moving on the screen iv. setOnTouchListener (new OnTouchListener () {// set the coordinate int startX; int startY; @ Override public boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // the first time the finger contacts the screen startX = (int) event. getX (); startY = (int) event. getY (); break; case MotionEvent. ACTION_MOVE: // slide int newX = (int) event with your finger on the screen. getX (); int newY = (int) event. getY (); canvas. drawLine (startX, startY, newX, newY, paint); // re-update the start position of the paint brush startX = (int) event. getX (); startY = (int) event. getY (); iv. setImageBitmap (baseBitmap); break; case MotionEvent. ACTION_UP: // finger exit screen break; default: break;} return true ;}});} public void save (View view ){}}

The following describes how to save an image:

Public void save (View view) {try {File file = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". jpg "); FileOutputStream stream = new FileOutputStream (file); baseBitmap. compress (CompressFormat. JPEG, 100, stream); stream. close (); Toast. makeText (this, "saved image succeeded", 1 ). show (); // simulate a Message notification system where the SD card is remounted with Intent intent = new Intent (); intent. setAction (intent. ACTION_MEDIA_MOUNTED); intent. setData (Uri. fromFile (Environment. getExternalStorageDirectory (); sendBroadcast (intent);} catch (Exception e) {Toast. makeText (this, "failed to save the image", 1 ). show (); e. printStackTrace ();}}

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.