android 图片剪切组件,android开发游记:自定义图片裁剪组件

由于各种原因,我放弃了在项目中使用系统的裁剪图片功能组件,转而使用自定义裁剪组件,话不多说,先上图

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

这里使用了github上的开源库,感谢Issei Aoki的分享

https://github.com/IsseiAoki/SimpleCropView

下面说下如何在项目中使用

首先下载项目,导入工程作为library管理到自己的工程

工程是android studio的环境,使用eclipse的朋友请参考我的第一篇博客

http://www.voidcn.com/article/p-fjjwsfkb-gh.html

实例中使用了toolbar,没有最新的5.0兼容包support_v7的朋友会出现错误:提示找不到Toolbar,只有把布局中的toolbar换成LinearLayout就OK了:

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="64dp"

android:background="@null">

......

换成

......

然后在自己工程的布局文件中加入自定义View组件CropImageView:

android:id="@+id/cropImageView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingBottom="64dp"

android:paddingLeft="16dp"

android:paddingRight="16dp"

android:paddingTop="32dp"

custom:backgroundColor="@color/base"

custom:cropMode="ratio_fit_image"

custom:frameColor="@color/frame"

custom:frameStrokeWeight="1dp"

custom:guideColor="@color/guide"

custom:guideShowMode="show_on_touch"

custom:guideStrokeWeight="1dp"

custom:handleColor="@color/handle"

custom:handleShowMode="show_always"

custom:handleSize="8dp"

custom:minFrameSize="50dp"

custom:overlayColor="@color/overlay"

custom:touchPadding="8dp" />

然后在自己的activity中初始化View控件

mCropView = (CropImageView) findViewById(R.id.cropImageView);

mCropView.setImageBitmap(bitmap);

mCropView.setCropMode(CropImageView.CropMode.RATIO_FREE);

/** * bitmap是你设置的要进行裁剪的图片 */

就可以了,很简单吧

这样就可以运行来看看了

注意原工程有个BUG,打开library的自定义View组件 CropImageView.java,找到getCroppedBitmap()方法,

/** * Get cropped image bitmap *@return cropped image bitmap */

public Bitmap getCroppedBitmap() {

int x = 0, y = 0, w = 0, h = 0;

if (mBitmap != null) {

int l = (int) (mFrameRect.left / mScale);

int t = (int) (mFrameRect.top / mScale);

int r = (int) (mFrameRect.right / mScale);

int b = (int) (mFrameRect.bottom / mScale);

x = l - (int) (mImageRect.left / mScale);

y = t - (int) (mImageRect.top / mScale);

w = r - l;

h = b - t;

}

Bitmap cropped = Bitmap.createBitmap(mBitmap, x, y, w, h, null, false);

if(mCropMode != CropMode.CIRCLE) return cropped;

return getCircularBitmap(cropped);

}

这个方法是从原图片中将生成裁剪后的图片

作者这里使用了强制类型转换,把float转换为int,由于强制类型转换是默认向下取整的所以,可能会造成产生1个像素的误差,因为这1个像素就会导致生成图片的高宽超过原图,从而导致异常,这个异常时偶发型的,困扰了我好久,后来才发现是裁图组件的bug导致的。把上面的方法改成这样:

int l = (int) Math.ceil((mFrameRect.left / mScale));

int t = (int) Math.ceil((mFrameRect.top / mScale));

int r = (int) Math.floor((mFrameRect.right / mScale));

int b = (int) Math.floor((mFrameRect.bottom / mScale));

x = l - (int) Math.ceil((mImageRect.left / mScale));

y = t - (int) Math.ceil((mImageRect.top / mScale));

就OK了

这个BUG已经反馈给作者,无奈英语不好,被当成来找茬的 #-_-

裁剪组件可以设置裁剪框是固定比例或者自由调整,矩形圆形等,只需要修改CropMode参数就可以了

可以在布局中使用custom:cropMode="ratio_1_1"属性,如下:

android:id="@+id/cropImageView"

xmlns:custom="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="250dp"

android:padding="16dp"

custom:cropMode="ratio_1_1"

/>

也可以在java代码中运行时动态改变:

mCropView.setCropMode(CropImageView.CropMode.RATIO_1_1);

CropMode的值如下,含义显而易见:

RATIO_FIT_IMAGE(0),

RATIO_4_3(1),

RATIO_3_4(2),

RATIO_1_1(3),

RATIO_16_9(4),

RATIO_9_16(5),

RATIO_FREE(6),

RATIO_CUSTOM(7);

获取裁剪后的图片就调用上面的方法

Bitmap croppedBitmap=mCropView.getCroppedBitmap();

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值