Android Development Practice: Write your own picture tailoring application (1)

Source: Internet
Author: User

The recent use of a week or so of spare time, finally completed an Android image clipping library, the core function is based on their own understanding of the implementation, part of the code reference to the Android source image clipping application. Now open the code on GitHub for everyone to learn and use, Address: Https://github.com/Jhuster/ImageCropper, the effect is as follows:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/73/wKioL1SxLJCh__uNAAEOLNPt7hk690.jpg "title=" Imagecropper_ copy. jpg "alt=" wkiol1sxljch__unaaeolnpt7hk690.jpg "/>


My general plan is to first introduce the use of this library, and then write a few articles about some of the principles and key technologies, hoping to help the Android developers.


Characteristics


    1. Support for moving and zooming the trim window through gestures

    2. Supports fixed clipping window size, fixed window length-to-width ratio

    3. Supports setting the maximum window length and width

    4. Supports the rotation of cropped pictures

    5. Easy to integrate and use


"How to use"


    1. Modify the Androidmanifest.xml file


You need to add an activity tag:


<activity android:name= "Com.ticktick.imagecropper.CropImageActivity"/>


Permission to add write SDcard


<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>


2. How to start the picture clipping interface


The first method uses the encapsulated cropintent in the library to build the intent object:


Private void startcropimage ()  {    // create a cropintent     cropintent intent = new cropintent ();          // Set the source image filepath/URL and  output filepath/url  (Required)     intent.setimagepath ("/sdcard/source.jpg");     intent.setoutputpath ("/sdcard/cropped.jpg");         // Set a fixed crop window size  (Optional)       intent.setoutputsize (640,480);    // set the max crop  window size  (Optional)      intent.setmaxoutputsize (800,600);     // set a fixed crop window ' s width/height aspect  (Optional )      intent. Setaspect (3,2);         // start imagecropper activity  with certain request code and listen for result     startactivityforresult (Intent.getintent (this),  request_code_crop_picture);}


The second method, the custom intent object:


Private void startcropimage ()  {    // create explicit intent     intent intent = new intent (This, cropimageactivity.class);             // Set the source  image filepath/url and output filepath/url  (Required)      Intent.setdata (Uri.fromfile (New file ("/sdcard/source.jpg"));     intent.putextra ( Mediastore.extra_output,uri.fromfile (New file ("/sdcard/cropped.jpg"));         // Set a fixed crop window size  (Optional)       intent.putextra ("Outputx", 640);     intent.putextra ("OutputY", 480);     // Set the max crop window size  (Optional)       intent.putextra ("Maxoutputx ","     intent.putextra "(" Maxoutputy ");     // set  a fixed crop window ' s width/height aspect  (Optional)       intent.putextra ("Aspectx", 3);     intent.putextra ("Aspecty", 2);         // Start ImageCropper activity with certain  Request code and listen for result    startactivityforresult ( Intent, request_code_crop_picture);}


3. Get clipping results


After clipping, if the user clicks on the "Save" button, you can get the URL of the saved picture via Mediastore.extra_output, and if the user clicks "Cancel", the activity's return value will be set to Result_ CANCEL


Protected void onactivityresult (int requestcode, int resultcode, intent  Data)  {    if  (RESULTCODE&NBSP;!=&NBSP;RESULT_OK)  {         return;    }    if  (requestcode ==  REQUEST_CODE_CROP_PICTURE )  {        Uri  Croppeduri = data.getextras (). getparcelable (Mediastore.extra_output);         InputStream in = null;try {             in = getcontentresolver (). Openinputstream (CroppedUri);             Bitmap b =  Bitmapfactory.decodestream (in);             Mimageview.setimagebitmap (b);         } catch  (filenotfoundexception e)  {             e.printstacktrace ();         }          }    super.onactivityresult ( Requestcode, resultcode, data);}


Summary


This library first introduced here, have any questions welcome message or letter [email protected] exchange, also welcome comments and suggestions, or for the open source code to make its own contribution, thank you.


This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1601702

Android Development Practice: Write your own picture tailoring application (1)

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.