您的位置:首页 > 移动开发 > Android开发

【Android】简单实用的图片裁剪库 - SimpleCropView (非常适合MD风格)

2017-03-06 12:00 811 查看
因为项目已转为 Material Design 风格,再加上以前的裁剪库已经很老了,跟 MD 风格又不是很搭,所以需求来了之后就找到了这个库。用了之后发现功能丰富实用,也简单,就推荐给大家。

库名:SimpleCropView

GitHub:https://github.com/IsseiAoki/SimpleCropView

下面来简单的说说怎么使用。

首先需要在 build.gradle 里面加入依赖:

dependencies {

    ...

 'com.isseiaoki:simplecropview:1.1.4'

...
}


接着在布局 XML 里面跟普通的控件一样去使用:

<com.isseiaoki.simplecropview.CropImageView
android:id="@+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center"
custom:scv_handle_size="10dp"
custom:scv_touch_padding="10dp"
custom:scv_background_color="#000000"
custom:scv_handle_color="#FFFFFF"
custom:scv_guide_color="#FFFFFF"
custom:scv_overlay_color="#aa1c1c1c"
custom:scv_frame_color="#FFFFFF"
custom:scv_handle_show_mode="show_always"
custom:scv_guide_show_mode="show_on_touch" />


注意加上(xmlns:custom="http://schemas.android.com/apk/res-auto")。

其 xml 属性以及对应的方法:

序号XML Attribute

(custom:)
Related MethodDescription
1scv_img_srcsetImageResource(int resId)Set source image.
2scv_crop_modesetCropMode(CropImageView.CropMode mode)Set crop mode.
3scv_background_colorsetBackgroundColor(int bgColor)Set view background color.
4scv_overlay_colorsetOverlayColor(int overlayColor)Set image overlay color.
5scv_frame_colorsetFrameColor(int frameColor)Set the image cropping frame color.
6scv_handle_colorsetHandleColor(int frameColor)Set the handle color.
7scv_guide_colorsetGuideColor(int frameColor)Set the guide color.
8scv_guide_show_modesetGuideShowMode(CropImageView.ShowMode mode)Set guideline show mode.
9scv_handle_show_modesetHandleShowMode(CropImageView.ShowMode mode)Set handle show mode.
10scv_handle_sizesetHandleSizeInDp(int handleDp)Set handle radius in density-independent pixels.
11scv_touch_paddingsetTouchPaddingInDp(int paddingDp)Set the image cropping frame handle touch padding(touch area) in density-independent pixels.
12scv_min_frame_sizesetMinFrameSizeInDp(int minDp)Set the image cropping frame minimum size in density-independent pixels.
13scv_frame_stroke_weightsetFrameStrokeWeightInDp(int weightDp)Set frame stroke weight in density-independent pixels.
14scv_guide_stroke_weightsetGuideStrokeWeightInDp(int weightDp)Set guideline stroke weight in density-independent pixels.
15scv_crop_enabledsetCropEnabled(boolean enabled)Set whether to show the image cropping frame.
16scv_initial_frame_scalesetInitialFrameScale(float initialScale)Set Set initial scale of the frame.(0.01 ~ 1.0)
17scv_animation_enabledsetAnimationEnabled(boolean enabled)Set whether to animate.
18scv_animation_durationsetAnimationDuration(int durationMillis)Set animation duration.
19scv_handle_shadow_enabledsetHandleShadowEnabled(boolean handleShadowEnabled)Set whether to show handle shadows.
各颜色设置对应名称:



下面介绍一些基本的常用的设置:

1、handle(拖块) 的大小和 padding,分别对应XML属性表的10,11

2、guide(辅助线条) 和 frame (边框)的粗细,分别对应XML属性表的14,13

3、handle(拖块)和 guide(辅助线条)显示模式,有显示(SHOW_ALWAYS,默认),不显示(NOT_SHOW),触摸时才显示(SHOW_ON_TOUCH),分别对应XML属性表的9,8

4、裁剪框的初始大小,通过 setInitialFrameScale(float initialScale); 设置,其参数范围:0.01~1.0(default),分别对应效果:



5、裁剪框最小尺寸,通过 setMinFrameSizeInDp(int Mindp); 去设置,默认是50。

6、裁剪框模式,比如比例,形状(方,圆)。通过 setCropMode(CropImageView.CropMode.RATIO_16_9); 其参数属性有:

*FIT_IMAGE, 固定纵横比,与原始图片相同的比例

*RATIO_4_3,  固定纵横比,宽高比4:3

*RATIO_3_4,  固定纵横比,宽高比3:4

*SQUARE(default),  固定纵横比,1:1正方形

*RATIO_16_9,  固定纵横比,宽高比16:9

*RATIO_9_16,  固定纵横比,宽高比9:16

*FREE,  非固定纵横比

*CUSTOM, 定制的

*CIRCLE,  圆形

*CIRCLE_SQUARE, 圆形,但裁剪出来的图是方形

PS:当然也可以自己定制宽高比(固定纵横比),通过 setCustomRatio(int ratioX, int ratioY); 进行设置。


大概常用的属性就这些,还有一些其它的属性,需要的可以去 GitHub 看下该库的详细方法。

接下来到加载图片和裁剪图片:

首先是加载图片,有两种方法:

1、setImageXXX();

2、startLoad(Uri sourceUri, LoadCallback callback);

其中第一种是同步,第二种则是异步。

第一种如果 bitmap 过大会出现异常,第一种可以加载 bitmap, Resource, Drawable, Uri;第二种直接加载 Uri 最有效的 bitmap ,无需理会文件路径和图片尺寸,callback 中包含两个方法,一个 onSuccess(),一个 onError()。

其次是裁剪图片,也有两种方法:

1、getCroppedBitmap();

2、startCrop(Uri saveUri, CropCallback cropCallback, SaveCallback saveCallback);

其中第一种是同步,第二种则是异步。

第一种用于setImageXXX(); 不保存裁剪后的 bitmap;第二种裁剪的是 Uri 最大尺度的 bitmap ,裁剪后保存在 saveUri,两个callback 中都包含两个方法,一个 onSuccess(),一个 onError(),第一个 callback 中的 onSuccess(Bitmap cropped) 可以获得裁剪后的 bitmap;第二个 callback 中的 onSuccess(Uri outputUri) 可以获得裁剪后的 Uri。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐