基於ucrop實現圖片裁剪需求
阿新 • • 發佈:2018-11-27
效果圖
重寫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(); } }