Simple picture of Android

Source: Internet
Author: User
Tags gety

Layout

<LinearLayoutxmlns: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:orientation= "vertical"Tools:context= "Com.example.paint.MainActivity" >    <ButtonAndroid:onclick= "Save"Android:id= "@+id/btn"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Save" />    <ButtonAndroid:onclick= "Selectimage"Android:id= "@+id/select"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Select a picture" />    <ImageViewAndroid:layout_above= "@id/select"Android:id= "@+id/iv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        /></LinearLayout>

Main_activity

 PackageCom.example.paint;ImportJava.io.File;ImportJava.io.FileOutputStream;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.CompressFormat;ImportAndroid.graphics.Bitmap.Config;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.ActionBarActivity;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {PrivateImageView IV; PrivateBitmap Basebitmap; PrivateCanvas canvas;//Canvas        PrivatePaint paint;//Brushes@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Paint=NewPaint (); //Brush WidthPaint.setstrokewidth (5); //Brush ColorPaint.setcolor (Color.green); //Create a bitmap that can be modifiedBasebitmap = Bitmap.createbitmap (320, 350, config.argb_8888); //Create a canvasCanvas =NewCanvas (BASEBITMAP); //Set Background colorCanvas.drawcolor (Color.White); IV=(ImageView) Findviewbyid (R.ID.IV); //know the trajectory of the user's finger moving on the flat AmericaIv.setontouchlistener (NewOntouchlistener () {//gets the coordinates of the start position of the finger            intStartX; intStarty; @Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down://Finger touch screen for the first timeStartX= (int) Event.getx (); Starty= (int) event.gety ();  Break;  CaseMotionevent.action_move://finger moves on the screen                                                intNewx = (int) Event.getx (); intNewy = (int) event.gety (); //Draw Linecanvas.drawline (StartX, Starty, Newy, newy, paint); //re-update brush start positionStartX = (int) Event.getx (); Starty= (int) event.gety ();                        Iv.setimagebitmap (BASEBITMAP);  Break;  CaseMOTIONEVENT.ACTION_UP://the movement of the finger off the screen                                                 Break; }                                //return True indicates that the brush has been touched                return true;    }                    }); }                 Public voidselectimage (view view) {Intent Intent=NewIntent ();        Intent.setaction (Intent.action_pick); Intent.settype ("Image/*"); Startactivityforresult (Intent,0); } @Overrideprotected voidOnactivityresult (intARG0,intarg1, Intent data) {        if(data!=NULL) {URI Uri=Data.getdata (); Bitmap Bitmap= Data.getparcelableextra ("Data"); Basebitmap=bitmap;                    Iv.setimageuri (URI); }        Super. Onactivityresult (arg0, arg1, data); }             Public voidSave (View view) {Try{File File=NewFile (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". jpg"); FileOutputStream Fos=Newfileoutputstream (file); //Save PictureBasebitmap.compress (Compressformat.jpeg, 100, FOS); Toast.maketext ( This, "Save picture succeeded", 0). Show (); //simulate a message notification System SD card is re-mountedIntent Intent =NewIntent ();            Intent.setaction (intent.action_media_mounted);            Intent.setdata (Uri.fromfile (Environment.getexternalstoragedirectory ()));                    Sendbroadcast (Intent); } Catch(Exception e) {Toast.maketext ( This, "Save picture Failed", 0). Show ();        E.printstacktrace (); }            }  }

Simple picture of Android

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.