【移动应用开发技术】我的开源项目:Android图片剪裁库_第1页
【移动应用开发技术】我的开源项目:Android图片剪裁库_第2页
【移动应用开发技术】我的开源项目:Android图片剪裁库_第3页
【移动应用开发技术】我的开源项目:Android图片剪裁库_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】我的开源项目:Android图片剪裁库

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

我的大致计划是首先介绍一下这个库的用法,然后再写几篇文章介绍一下其中的一些原理和关键技术,希望对Android开发新手有所帮助。【特性】支持通过手势移动和缩放剪裁窗口支持固定剪裁窗口大小、固定窗口的长宽比率支持设置最大的窗口长和宽支持剪裁图片的旋转易于集成和使用【使用方法】修改AndroidManifest.xml文件需要添加一个Activity标签:<activity

android:name="com.ticktick.p_w_picpathcropper.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

p_w_picpath

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);

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

void

startCropImage()

{

//

Create

explicit

intent

Intent

intent

=

new

Intent(this,

CropImageActivity.class);

//

Set

the

source

p_w_picpath

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_CANCELprotected

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();

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论