[Android] Picture drawing board

Source: Internet
Author: User
Tags gety

Page layout, relative layout

The button is located below the parent form using android:layout_alignparentbottom="true"

The picture is above the button, fills the parent form, uses android:layout_above="@+id/xxxxxid"

Get to ImageView Object

Call the bitmap.createbitmap (width,height,config) method to create a Bitmap object that can be modified

Parameters: Widthheight wide height write dead,config is Bitmap.Config.ARGB_8888

Get the canvas object, via new canvas (bitmap)

Call the Drawcolor () method of the Canvas Object , initialize the background color, parameters:color.white White

Call the Setontouchlistener () method of the ImageView object , Parameter:ontouchlistener object, this class is an interface type and therefore directly New it creates an anonymous inner class implementation method OnTouch ()

Inside the OnTouch (View v,motionevent Event) method

Parameters: TheView object is a touch object, andthemotionevent object is a touch event object

Call the getaction () method of the motionevent object to get the touch event

Switch to judge the event

The event for Motionevent.action_down is the first time the finger touches the screen

The event for Motionevent.action_move is that the finger moves on the screen

The event for motionevent.action_up is the finger leaving the screen

When the finger touches the screen

Defines the coordinates of the start position of the finger StartX starty

Call the GetX () and GetY () methods of the motionevent object to get the coordinates

When the fingers move, we want to get new coordinates NEWX and newy, the same way as above

Call the Canvas object's drawLine () method, draw a line, parameter:startX,starty , newx , Newy , Paint

Gets the paint Brush object, coming out through new

Call the settrokewidth () method of the paint object to set the brush weight, parameters: pixels

Call the SetColor () method of the paint object , set the brush color, parameters:color.green

Note To update the position of the brush from the new

Call the setimagebitmap (bitmap) method of the ImageView object to display the bitmap in the control

The OnTouch () method must return truein order to be continuously executed

Click the Save button to save the image to the SD card

Call the Bitmap object's compress () method, Parameters: Picture format compressformat.jpeg, picture quality the output stream

Gets the file object, via new file (Environment.getexternalstoragedirectory (), file name ) , the file name is a timestamp

Get fileoutputstream Object

Requires permission Android.permission.WRITE_EXTERNAL_STORGE

View pictures using System Gallery

You will not see it at this time, because the library is only scanned when the SD card is mounted.

Get Intent object, come out with new

Call the setaction () method of the Intent object , Parameter:intent.action_media_mounted

Call the setData () method of the Intent object,

Parameters: File directory object uri.fromfile (Environment.getexternalstoragedirectory ())

Call the sendbroadcast () method to send the broadcast

Code:

 PackageCom.tsh.tshpaint;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.CompressFormat;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.view.InputDevice.MotionRange;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateImageView iv_img; PrivateBitmap Basebitmap; PrivateCanvas Canvas; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Iv_img=(ImageView) Findviewbyid (r.id.iv_img); //Create an empty bitmapBasebitmap = Bitmap.createbitmap (320, 359, Bitmap.Config.ARGB_8888); Canvas=NewCanvas (BASEBITMAP);        Canvas.drawcolor (Color.White); //set finger swipe eventsIv_img.setontouchlistener (NewOntouchlistener () {intStartX; intStarty; @Override Public BooleanOnTouch (View V, motionevent event) {intAction =event.getaction (); //BrushesPaint paint=NewPaint (); Paint.setstrokewidth (5);                Paint.setcolor (Color.green); Switch(action) {//Finger Press                 CaseMotionEvent.ACTION_DOWN:startX=(int) Event.getx (); Starty=(int) event.gety ();  Break; //Finger Movement                 CaseMotionevent.action_move:intNewx= (int) Event.getx (); intNewy= (int) event.gety (); //Dashcanvas.drawline (StartX, Starty, newx, newy, paint); StartX=(int) Event.getx (); Starty=(int) event.gety ();                    Iv_img.setimagebitmap (BASEBITMAP);  Break; //Finger away                 Casemotionevent.action_up: Break; }                return true;    }        }); }    //Save Picture     Public voidSave (View v) {File file=NewFile (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". jpg");        FileOutputStream stream; Try{Stream=Newfileoutputstream (file); Basebitmap.compress (Compressformat.jpeg,100, stream);            Stream.Close (); //send SD card mount notificationIntent intent=NewIntent ();            Intent.setaction (intent.action_media_mounted);            Intent.setdata (Uri.fromfile (Environment.getexternalstoragedirectory ()));                        Sendbroadcast (Intent); Toast.maketext ( This, "Save picture succeeded", 0). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "Save picture Failed", 0). Show (); }            }}

[Android] Picture drawing board

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.