cropiwa
1.0.0
该库是一个高度可配置的图像裁剪小部件。
将其添加到您的依赖项块中。
compile 'com.steelkiwi:cropiwa:1.0.3'
请参阅示例应用程序以获取库使用示例。
该库具有模块化架构,这使得它具有高度可配置性。有关如何配置CropIwaView
的信息,请参阅以下部分。
其中一个有用的功能是您不必等待结果 - 裁剪请求完成后,只需切换到另一个屏幕并以广播的形式等待结果。
将 CropIwa 添加到您的 xml 中:
< com .steelkiwi.cropiwa.CropIwaView
android : id = " @+id/crop_view "
android : layout_width = " match_parent "
android : layout_height = " match_parent " />
cropView . crop ( new CropIwaSaveConfig . Builder ( destinationUri )
. setCompressFormat ( Bitmap . CompressFormat . PNG )
. setSize ( outWidth , outHeight ) //Optional. If not specified, SRC dimensions will be used
. setQuality ( 100 ) //Hint for lossy compression formats
. build ());
裁剪区域保存回调。当裁剪请求完成时,会发送广播。您可以使用 CropIwaView 实例来收听它
cropView . setCropSaveCompleteListener ( bitmapUri -> {
//Do something
});
cropView . setErrorListener ( error -> {
//Do something
});
或直接使用广播接收器。优点是它可以从应用程序的任何部分使用,只要您可以访问Context
。
CropIwaResultReceiver resultReceiver = new CropIwaResultReceiver ();
resultReceiver . setListener ( resultListener );
resultReceiver . register ( context );
//Don't forget to unregister it when you are done
resultReceiver . unregister ( context );
您可以订阅CropIwaView
配置中的更改。每当调用.apply()
时,侦听器都会收到通知。
cropIwaView . configureOverlay (). addConfigChangeListener ( listener );
cropIwaView . configureImage (). addConfigChangeListener ( listener )
app : ci_dynamic_aspect_ratio = "true|false"
cropView . configureOverlay ()
. setDynamicCrop ( enabled )
. apply ();
app : ci_draw_grid = "true|false"
cropView . configureOverlay ()
. setShouldDrawGrid ( draw )
. apply ();
app : ci_aspect_ratio_w = "16"
app : ci_aspect_ratio_h = "9"
cropView . configureOverlay ()
. setAspectRatio ( new AspectRatio ( 16 , 9 ))
. setAspectRatio ( AspectRatio . IMG_SRC ) //If you want crop area to be equal to the dimensions of an image
. apply ();
app : ci_initial_position = "centerCrop|centerInside"
cropView . configureImage ()
. setImageInitialPosition ( position )
. apply ();
//Value is a float from 0.01f to 1
cropIwaView . configureImage ()
. setScale ( scale )
. apply ();
app : ci_scale_enabled = "true|false"
cropView . configureImage ()
. setImageScaleEnabled ( enabled )
. apply ();
app : ci_translation_enabled = "true|false"
cropView . configureImage ()
. setImageTranslationEnabled ( enabled )
. apply ();
app : ci_crop_shape = "rectangle|oval"
cropView . configureOverlay ()
. setCropShape ( new CropIwaRectShape ( cropView . configureOverlay ()))
. setCropShape ( new CropIwaOvalShape ( cropView . configureOverlay ()))
. apply ();
app : ci_max_scale = "1f"
cropView . configureImage ()
. setMinScale ( minScale )
. setMaxScale ( maxScale )
. apply ();
app : ci_min_crop_width = "40dp"
app : ci_min_crop_height = "40dp"
cropView . configureOverlay ()
. setMinWidth ( dps )
. setMinHeight ( dps )
. apply ();
app : ci_border_width = "1dp"
app : ci_corner_width = "1dp"
app : ci_grid_width = "1dp"
cropView . configureOverlay ()
. setBorderStrokeWidth ( dps )
. setCornerStrokeWidth ( dps )
. setGridStrokeWidth ( dps )
. apply ();
app : ci_border_color = "#fff"
app : ci_corner_color = "#fff"
app : ci_grid_color = "#fff"
app : ci_overlay_color = "#fff"
cropView . configureOverlay ()
. setBorderColor ( Color . WHITE )
. setCornerColor ( Color . WHITE )
. setGridColor ( Color . WHITE )
. setOverlayColor ( Color . WHITE )
. apply ();
您可以直接使用Paint
对象。例如,这使您能够绘制具有虚线效果的网格。
Paint gridPaint = cropView . configureOverlay ()
. getCropShape ()
. getGridPaint ();
gridPaint . setPathEffect ( new DashPathEffect ( new float [] { interval , interval }, 0 ));
用同样的方法可以获取其他Paint
。
CropIwaOverlayConfig config = cropView . configureOverlay ();
CropIwaShape shape = config . getCropShape ();
shape . getGridPaint ();
shape . getBorderPaint ();
shape . getCornerPaint ();
您还可以创建自定义裁剪区域形状。只需扩展CropIwaShape
(有关示例,请参阅 CropIwaOvalShape)并使用以下命令设置类的实例:
cropView . configureOverlay ()
. setCropShape ( new MyAwesomeShape ())
. apply ();
Copyright © 2017 SteelKiwi, http://steelkiwi.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.