SimpleCropView
介绍:
一个图片裁减库,可以自定义UI,非常不错。
运行效果:
使用说明:
依赖:
repositories {
jcenter()
}
dependencies {
compile 'com.isseiaoki:simplecropview:1.0.7'
}
添加com.isseiaoki.simplecropview.CropImageView 到布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.isseiaoki.simplecropview.CropImageView
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"
/>
<Button
android:id="@+id/crop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="16dp"
android:text="CROP"
/>
<ImageView
android:id="@+id/croppedImageView"
android:layout_margin="16dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
设置图片,获取裁减后的图片
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
final ImageView croppedImageView = (ImageView)findViewById(R.id.croppedImageView);
// Set image for cropping
cropImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.sample5));
Button cropButton = (Button)findViewById(R.id.crop_button);
cropButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get cropped image, and show result.
croppedImageView.setImageBitmap(cropImageView.getCroppedBitmap());
}
});
}
}
以上介绍的是基本用法,下面是更多自定义用法。
自定义
CropMode
裁减范围的宽高比选项
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setCropMode(CropImageView.CropMode.RATIO_16_9);
CropMode 的值:
RATIO_4_3, RATIO_3_4, RATIO_1_1, RATIO_16_9, RATIO_9_16, RATIO_FIT_IMAGE, RATIO_FREE
RATIO_FREE: 没有固定的宽高比模式
RATIO_X_Y: 固定的宽高比
RATIO_FIT_IMAGE: 固定的宽高比,但是比例和原始图像一致。
如果你需要其他宽高比,使用setCustomRatio(int ratioX, int ratioY);
MinimumFrameSize
裁减区域的最小大小 单位dp
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);
颜色
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setBackgroundColor(0xFFFFFFFB);
cropImageView.setOverlayColor(0xAA1C1C1C);
cropImageView.setFrameColor(getResources().getColor(R.color.frame));
cropImageView.setHandleColor(getResources().getColor(R.color.handle));
cropImageView.setGuideColor(getResources().getColor(R.color.guide));
线条与拖块的粗细
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setFrameStrokeWeightInDp(1);
cropImageView.setGuideStrokeWeightInDp(1);
cropImageView.setHandleSizeInDp(getResources().getDimension(R.dimen.handle_size));
触摸区域
裁减拖块的额外触摸区域。
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setTouchPadding(16);
拖块与辅助线条的显示模式
CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setHandleShowMode(CropImageView.ShowMode.SHOW_ALWAYS);
cropImageView.setGuideShowMode(CropImageView.ShowMode.SHOW_ON_TOUCH);
ShowMode 值:
SHOW_ALWAYS, NOT_SHOW, SHOW_ON_TOUCH
拖块显示模式 | 辅助线条显示模式 | 外观 |
---|---|---|
SHOW_ALWAYS | SHOW_ALWAYS | |
NOT_SHOW | NOT_SHOW | |
SHOW_ALWAYS | NOT_SHOW | |
SHOW_ALWAYS | SHOW_ON_TOUCH | |
SHOW_ON_TOUCH | NOT_SHOW |
XML 属性
xml的例子:
<com.isseiaoki.simplecropview.CropImageView
android:id="@+id/cropImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp"
custom:imgSrc="@mipmap/ic_launcher"
custom:cropMode="ratio_fit_image"
custom:minFrameSize="50dp"
custom:backgroundColor="@color/background_material_dark"
custom:overlayColor="#66000000"
custom:frameColor="@android:color/white"
custom:handleColor="@android:color/white"
custom:guideColor="#BBFFFFFF"
custom:frameStrokeWeight="3dp"
custom:guideStrokeWeight="1dp"
custom:handleSize="32dp"
custom:touchPadding="0dp"
custom:guideShowMode="not_show"
custom:handleShowMode="show_always"
custom:cropEnabled="true"
/>
| XML Attribute
(custom:) | Related Method | Description |
---|---|---|
imgSrc | setImageResource(int resId) | Set source image. |
cropMode | setCropMode(CropImageView.CropMode mode) | Set crop mode. |
minFrameSize | setMinFrameSizeInDp(int minDp) | Set the image cropping frame minimum size in density-independent pixels. |
backgroundColor | setBackgroundColor(int bgColor) | Set view background color. |
overlayColor | setOverlayColor(int overlayColor) | Set image overlay color. |
frameColor | setFrameColor(int frameColor) | Set the image cropping frame color. |
handleColor | setHandleColor(int frameColor) | Set the handle color. |
guideColor | setGuideColor(int frameColor) | Set the guide color. |
handleSize | setHandleSizeInDp(int handleDp) | Set handle radius in density-independent pixels. |
touchPadding | setTouchPaddingInDp(int paddingDp) | Set the image cropping frame handle touch padding(touch area) in density-independent pixels. |
frameStrokeWeight | setFrameStrokeWeightInDp(int weightDp) | Set frame stroke weight in density-independent pixels. |
guideStrokeWeight | setGuideStrokeWeightInDp(int weightDp) | Set guideline stroke weight in density-independent pixels. |
guideShowMode | setGuideShowMode(CropImageView.ShowMode mode) | Set guideline show mode. |
handleShowMode | setHandleShowMode(CropImageView.ShowMode mode) | Set handle show mode. |
cropEnabled | setCropEnabled(boolean enabled) | Set whether to show the image cropping frame. |
已下载
0