Android图片剪裁库
生活随笔
收集整理的這篇文章主要介紹了
Android图片剪裁库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近利用一周左右的業余時間,終于完成了一個Android圖片剪裁庫,核心功能是根據自己的理解實現的,部分代碼參考了Android源碼的圖片剪裁應用。現在將該代碼開源在Github上以供大家學習和使用,地址:https://github.com/Jhuster/ImageCropper,效果如下所示:
?
? ??
我的大致計劃是首先介紹一下這個庫的用法,然后再寫幾篇文章介紹一下其中的一些原理和關鍵技術,希望對Android開發新手有所幫助。
【特性】
支持通過手勢移動和縮放剪裁窗口
支持固定剪裁窗口大小、固定窗口的長寬比率
支持設置最大的窗口長和寬
支持剪裁圖片的旋轉
易于集成和使用
【使用方法】
修改AndroidManifest.xml文件
需要添加寫SDcard的權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />2. 啟動圖片剪裁界面的方法
第一種方法,使用庫中封裝的CropIntent來構建Intent對象:
private void startCropImage() {// Create a CropIntentCropIntent 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 resultstartActivityForResult(intent.getIntent(this), REQUEST_CODE_CROP_PICTURE); }第二種方法,自定義Intent對象:
private void startCropImage() {// Create explicit intentIntent 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",800);intent.putExtra("maxOutputY",600);// 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. 獲取剪裁結果
剪裁結束后,如果用戶點擊了“Save”按鈕,則可以通過MediaStore.EXTRA_OUTPUT得到保存的圖片的URL地址;如果用戶點擊了“Cancel”,則Activity的返回值會被設置為 RESULT_CANCEL
protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode != 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); }?
總結
以上是生活随笔為你收集整理的Android图片剪裁库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GO语言基础map与函数
- 下一篇: 转换函数