-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new feature - start crop using ucrop fragment
- Loading branch information
1 parent
4c41d26
commit 6b16bc5
Showing
13 changed files
with
954 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
sample/src/main/java/com/yalantis/ucrop/sample/ActivityWithFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package com.yalantis.ucrop.sample; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Intent; | ||
import android.graphics.PorterDuff; | ||
import android.graphics.drawable.Animatable; | ||
import android.graphics.drawable.Drawable; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.support.annotation.ColorInt; | ||
import android.support.annotation.DrawableRes; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.Toolbar; | ||
import android.util.Log; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.Window; | ||
import android.view.WindowManager; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.yalantis.ucrop.UCrop; | ||
import com.yalantis.ucrop.UCropFragment; | ||
import com.yalantis.ucrop.UCropFragmentCallback; | ||
|
||
public class ActivityWithFragment extends AppCompatActivity implements UCropFragmentCallback { | ||
|
||
private String mToolbarTitle; | ||
@DrawableRes | ||
private int mToolbarCancelDrawable; | ||
@DrawableRes | ||
private int mToolbarCropDrawable; | ||
// Enables dynamic coloring | ||
private int mToolbarColor; | ||
private int mStatusBarColor; | ||
private int mToolbarWidgetColor; | ||
private boolean mShowLoader; | ||
private UCropFragment fragment; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_with_fragment); | ||
|
||
Bundle bundles = getIntent().getExtras(); | ||
if (bundles != null) { | ||
Uri inputUri = bundles.getParcelable(UCrop.EXTRA_INPUT_URI); | ||
Uri outputUri = bundles.getParcelable(UCrop.EXTRA_OUTPUT_URI); | ||
if (inputUri != null && outputUri != null) { | ||
UCrop uCrop = UCrop.of(inputUri, outputUri); | ||
setupViews(bundles); | ||
|
||
fragment = uCrop.getFragment(bundles, this); | ||
getSupportFragmentManager().beginTransaction() | ||
.add(R.id.fragment_container, fragment, UCropFragment.TAG) | ||
.commit(); | ||
} else { | ||
finish(); | ||
} | ||
} | ||
} | ||
|
||
public void setupViews(Bundle args) { | ||
mStatusBarColor = args.getInt(UCrop.Options.EXTRA_STATUS_BAR_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_statusbar)); | ||
mToolbarColor = args.getInt(UCrop.Options.EXTRA_TOOL_BAR_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar)); | ||
mToolbarCancelDrawable = args.getInt(UCrop.Options.EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE, R.drawable.ucrop_ic_cross); | ||
mToolbarCropDrawable = args.getInt(UCrop.Options.EXTRA_UCROP_WIDGET_CROP_DRAWABLE, R.drawable.ucrop_ic_done); | ||
mToolbarWidgetColor = args.getInt(UCrop.Options.EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar_widget)); | ||
mToolbarTitle = args.getString(UCrop.Options.EXTRA_UCROP_TITLE_TEXT_TOOLBAR); | ||
mToolbarTitle = mToolbarTitle != null ? mToolbarTitle : getResources().getString(R.string.ucrop_label_edit_photo); | ||
|
||
setupAppBar(); | ||
} | ||
|
||
/** | ||
* Configures and styles both status bar and toolbar. | ||
*/ | ||
private void setupAppBar() { | ||
setStatusBarColor(mStatusBarColor); | ||
|
||
final Toolbar toolbar = findViewById(R.id.toolbar); | ||
|
||
// Set all of the Toolbar coloring | ||
toolbar.setBackgroundColor(mToolbarColor); | ||
toolbar.setTitleTextColor(mToolbarWidgetColor); | ||
|
||
final TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title); | ||
toolbarTitle.setTextColor(mToolbarWidgetColor); | ||
toolbarTitle.setText(mToolbarTitle); | ||
|
||
// Color buttons inside the Toolbar | ||
Drawable stateButtonDrawable = ContextCompat.getDrawable(getBaseContext(), mToolbarCancelDrawable); | ||
stateButtonDrawable.mutate(); | ||
stateButtonDrawable.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP); | ||
toolbar.setNavigationIcon(stateButtonDrawable); | ||
|
||
setSupportActionBar(toolbar); | ||
final ActionBar actionBar = getSupportActionBar(); | ||
if (actionBar != null) { | ||
actionBar.setDisplayShowTitleEnabled(false); | ||
} | ||
} | ||
|
||
/** | ||
* Sets status-bar color for L devices. | ||
* | ||
* @param color - status-bar color | ||
*/ | ||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
private void setStatusBarColor(@ColorInt int color) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
final Window window = getWindow(); | ||
if (window != null) { | ||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | ||
window.setStatusBarColor(color); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(final Menu menu) { | ||
getMenuInflater().inflate(R.menu.ucrop_menu_activity, menu); | ||
|
||
// Change crop & loader menu icons color to match the rest of the UI colors | ||
|
||
MenuItem menuItemLoader = menu.findItem(R.id.menu_loader); | ||
Drawable menuItemLoaderIcon = menuItemLoader.getIcon(); | ||
if (menuItemLoaderIcon != null) { | ||
try { | ||
menuItemLoaderIcon.mutate(); | ||
menuItemLoaderIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP); | ||
menuItemLoader.setIcon(menuItemLoaderIcon); | ||
} catch (IllegalStateException e) { | ||
Log.i(this.getClass().getName(), String.format("%s - %s", e.getMessage(), getString(R.string.ucrop_mutate_exception_hint))); | ||
} | ||
((Animatable) menuItemLoader.getIcon()).start(); | ||
} | ||
|
||
MenuItem menuItemCrop = menu.findItem(R.id.menu_crop); | ||
Drawable menuItemCropIcon = ContextCompat.getDrawable(this, mToolbarCropDrawable); | ||
if (menuItemCropIcon != null) { | ||
menuItemCropIcon.mutate(); | ||
menuItemCropIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP); | ||
menuItemCrop.setIcon(menuItemCropIcon); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onPrepareOptionsMenu(Menu menu) { | ||
menu.findItem(R.id.menu_crop).setVisible(!mShowLoader); | ||
menu.findItem(R.id.menu_loader).setVisible(mShowLoader); | ||
return super.onPrepareOptionsMenu(menu); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
if (item.getItemId() == R.id.menu_crop) { | ||
if (fragment.isAdded()) | ||
fragment.cropAndSaveImage(); | ||
} else if (item.getItemId() == android.R.id.home) { | ||
onBackPressed(); | ||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
@Override | ||
public void onCropFinish(UCropFragment.UCropResult result) { | ||
switch (result.mResultCode) { | ||
case RESULT_OK: | ||
handleCropResult(result.mResultData); | ||
break; | ||
case UCrop.RESULT_ERROR: | ||
handleCropError(result.mResultData); | ||
break; | ||
|
||
} | ||
} | ||
|
||
private void handleCropResult(@NonNull Intent result) { | ||
final Uri resultUri = UCrop.getOutput(result); | ||
if (resultUri != null) { | ||
ResultActivity.startWithUri(this, resultUri); | ||
} else { | ||
Toast.makeText(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) { | ||
Log.e(this.getClass().getName(), "handleCropError: ", cropError); | ||
Toast.makeText(this, cropError.getMessage(), Toast.LENGTH_LONG).show(); | ||
} else { | ||
Toast.makeText(this, R.string.toast_unexpected_error, Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void loadingProgress(boolean showLoader) { | ||
mShowLoader = showLoader; | ||
supportInvalidateOptionsMenu(); | ||
} | ||
} |
Oops, something went wrong.