Android Picture Clipping Gallery

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:

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

<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 voidStartcropimage () {//Create a cropintentCropintent Intent =Newcropintent (); //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 ( -, -); //Set a fixed crop window ' s width/height aspect (Optional)Intent.setaspect (3,2); //Start Imagecropper activity with certain request code and listen for resultStartactivityforresult (Intent.getintent ( This), request_code_crop_picture);}

The second method, the custom intent object:

Private voidStartcropimage () {//Create Explicit IntentIntent Intent =NewIntent ( This, Cropimageactivity.class); //Set The source image Filepath/url and Output Filepath/url (Required)Intent.setdata (Uri.fromfile (NewFile ("/sdcard/source.jpg"))); Intent.putextra (Mediastore.extra_output,uri.fromfile (NewFile ("/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 resultStartactivityforresult (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 voidOnactivityresult (intRequestcode,intResultCode, Intent data) {     if(ResultCode! =RESULT_OK) {        return; }     if(Requestcode = =request_code_crop_picture) {Uri Croppeduri=Data.getextras (). getparcelable (Mediastore.extra_output); InputStreaminch=NULL; Try {            inch=getcontentresolver (). Openinputstream (Croppeduri); Bitmap b= Bitmapfactory.decodestream (inch);        Mimageview.setimagebitmap (b); }     Catch(FileNotFoundException e) {e.printstacktrace (); }} super.onactivityresult (Requestcode, ResultCode, data);}

Android Picture Clipping Gallery

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.