日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android图片剪裁库

發布時間:2025/7/14 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android图片剪裁库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近利用一周左右的業余時間,終于完成了一個Android圖片剪裁庫,核心功能是根據自己的理解實現的,部分代碼參考了Android源碼的圖片剪裁應用。現在將該代碼開源在Github上以供大家學習和使用,地址:https://github.com/Jhuster/ImageCropper,效果如下所示:

?

? ??

我的大致計劃是首先介紹一下這個庫的用法,然后再寫幾篇文章介紹一下其中的一些原理和關鍵技術,希望對Android開發新手有所幫助。

【特性】

  • 支持通過手勢移動和縮放剪裁窗口

  • 支持固定剪裁窗口大小、固定窗口的長寬比率

  • 支持設置最大的窗口長和寬

  • 支持剪裁圖片的旋轉

  • 易于集成和使用

  • 【使用方法】

  • 修改AndroidManifest.xml文件

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

    需要添加寫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图片剪裁库的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。