版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lamphogani/article/details/78810192
效果图
重写ucrop布局,更改UCropActivity代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ucrop_photobox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF1A1A1A">
<com.yalantis.ucrop.view.UCropView
android:id="@+id/ucrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tv_rotate_angel"
android:layout_marginBottom="10dp" />
<TextView
android:id="@+id/tv_rotate_angel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rotate_scroll_wheel"
android:layout_centerHorizontal="true"
android:text=""
android:textColor="@android:color/white"
android:textSize="14sp" />
<com.yalantis.ucrop.view.widget.HorizontalProgressWheelView
android:id="@+id/rotate_scroll_wheel"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="@+id/layout_aspect_ratio"
android:layout_marginBottom="5dp" />
<LinearLayout
android:id="@+id/layout_aspect_ratio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_bottom"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:background="@android:color/black"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_weight="1"
android:text="取消"
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/iv_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="图片旋转90度"
android:src="@drawable/icon_new_recycle" />
<TextView
android:id="@+id/tv_free"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="自由裁剪"
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/iv_rotate_reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="旋转重置"
android:src="@drawable/icon_new_hy" />
<TextView
android:id="@+id/tv_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:gravity="end"
android:text="完成"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_SELECT_PICTURE) {//图片选择结果
final Uri selectedUri = data.getData();
if (selectedUri != null) {
startCropActivity(data.getData());
} else {
Toast.makeText(MainActivity.this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
}
} else if (requestCode == UCrop.REQUEST_CROP) {//图片裁剪结果
handleCropResult(data);
}
}
if (resultCode == UCrop.RESULT_ERROR) {
handleCropError(data);
}
}
private void handleCropResult(@NonNull Intent result) {
final Uri resultUri = UCrop.getOutput(result);
if (resultUri != null) {
ResultActivity.startWithUri(MainActivity.this, resultUri);
//可以在这里直接将裁剪后的图片进行保存
//saveCroppedImage(resultUri);
} else {
Toast.makeText(MainActivity.this, R.string.toast_cannot_retrieve_cropped_image, Toast.LENGTH_SHORT).show();
}
}
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
private void handleCropError(@NonNull Intent result) {
final Throwable cropError = UCrop.getError(result);
if (cropError != null) {
Toast.makeText(MainActivity.this, cropError.getMessage(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, R.string.toast_unexpected_error, Toast.LENGTH_SHORT).show();
}
}