最近利用一周左右的业余时间,终于完成了一个Android图片剪裁库,核心功能是根据自己的理解实现的,部分代码参考了Android源码的图片剪裁应用。现在将该代码开源在Github上以供大家学习和使用,地址:https://github.com/Jhuster/ImageCropper,效果如下所示:

我的大致计划是首先介绍一下这个库的用法,然后再写几篇文章介绍一下其中的一些原理和关键技术,希望对Android开发新手有所帮助。

【特性】

  1. 支持通过手势移动和缩放剪裁窗口

  2. 支持固定剪裁窗口大小、固定窗口的长宽比率

  3. 支持设置最大的窗口长和宽

  4. 支持剪裁图片的旋转

  5. 易于集成和使用

【使用方法】

  1. 修改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 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(,); // Set the max crop window size (Optional)
intent.setMaxOutputSize(,); // Set a fixed crop window's width/height aspect (Optional)
intent.setAspect(,); // Start ImageCropper activity with certain request code and listen for result
startActivityForResult(intent.getIntent(this), REQUEST_CODE_CROP_PICTURE);
}

第二种方法,自定义Intent对象:

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",);
intent.putExtra("outputY",); // 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",);
intent.putExtra("aspectY",); // 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图片剪裁库的更多相关文章

  1. Android 图片裁剪库 uCrop

    引语 晚上好,我是猫咪,我的公众号「程序媛猫咪」会推荐 GitHub 上好玩的项目,挖掘开源的价值,欢迎关注我. 现在 Android 开发,离不开图片,必然也需要图片裁剪功能,这个实现可以调用系统的 ...

  2. [RN] React Native 使用开源库 react-native-image-crop-picker 实现图片选择、图片剪裁

    React Native 使用开源库 react-native-image-crop-picker 实现图片选择.图片剪裁 该库可以实现启动本地相册和照相机来采集图片,并且提供多选.图片裁剪等功能,支 ...

  3. Android图片加载库的理解

    前言     这是“基础自测”系列的第三篇文章,以Android开发需要熟悉的20个技术点为切入点,本篇重点讲讲Android中的ImageLoader这个库的一些理解,在Android上最让人头疼是 ...

  4. picasso-强大的Android图片下载缓存库

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! pica ...

  5. fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)

    [Android开发经验]FaceBook推出的Android图片加载库-Fresco   欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...

  6. picasso_强大的Android图片下载缓存库

    tag: android pic skill date: 2016/07/09 title: picasso-强大的Android图片下载缓存库 [本文转载自:泡在网上的日子 参考:http://bl ...

  7. 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选

    毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...

  8. android图片加载库Glide

    什么是Glide? Glide是一个加载图片的库,作者是bumptech,它是在泰国举行的google 开发者论坛上google为我们介绍的,这个库被广泛的运用在google的开源项目中. Glide ...

  9. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

随机推荐

  1. BZOJ 4195: [Noi2015]程序自动分析 并查集 + 离散化 + 水题

    TM 读错题了...... 我还以为是要动态询问呢,结果是统一处理完了再询问...... 幼儿园题,不解释. Code: #include<bits/stdc++.h> #define m ...

  2. eoLinker AMS 专业版V3.3发布:分享项目可以测试并选择分享内容等

    eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台.目前eoLinker AMS已经为来自全球的超过两万家企业托管超 ...

  3. Day3 分支结构

    if语句的使用 在Python中,要构造分支结构可以使用if.elif和else关键字.所谓关键字就是有特殊含义的单词,像if和else就是专门用于构造分支结构的关键字,很显然你不能够使用它作为变量名 ...

  4. js中call、apply、bind的区别

    var Person = { name : 'alice', say : function(txt1,txt2) { console.info(txt1+txt2); console.info(thi ...

  5. 一键安装LNMP(适合centos7)

    1.准备工作,下载源码包 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar ...

  6. python-if判断

    1. python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何 ...

  7. BeanUtils.copyProperties()错误使用,给自己挖了坑

    场景:需要对某个集合中的所有元素拷贝到另一个集合中,想着BeanUtils.copyProperties()可以深拷贝对象,误以为也可以拷贝集合,于是乎写下了如下代码 List<CostRule ...

  8. 【 Codeforces Round #519 by Botan Investments B】Lost Array

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举k 不难根据a得到x[0..k-1] 然后再根据a[k+1..n]来验证一下得到的x是否正确就好. [代码] #include <bits ...

  9. Spring中获取Session的方法汇总

    Spring: web.xml <listener> <listener-class>org.springframework.web.context.request.Reque ...

  10. iOS开发——定制圆形头像与照相机图库的使用

    如今的App都很流行圆形的头像,比方QQ右上角的头像,今日头条的头像等等.这已经成为App设计的趋势了.今天我们就来简单实现一下这个功能,我还会把从手机拍照中或者图库中取出作为头像的照片存储到应用程序 ...