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.