Skip to content

Commit

Permalink
added new feature - start crop using ucrop fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Legementarion committed Mar 21, 2018
1 parent 4c41d26 commit 6b16bc5
Show file tree
Hide file tree
Showing 13 changed files with 954 additions and 87 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:3.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ android {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
lintOptions {
abortOnError false
}

}

dependencies {
compile 'com.android.support:appcompat-v7:27.1.0'

compile project (':ucrop')
compile project(':ucrop')
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
29 changes: 13 additions & 16 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.yalantis.ucrop.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yalantis.ucrop.sample">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="@string/file_provider_authorities"
Expand All @@ -23,24 +22,22 @@
</provider>

<activity
android:name="com.yalantis.ucrop.sample.SampleActivity"
android:name=".SampleActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.yalantis.ucrop.sample.ResultActivity"
android:screenOrientation="portrait"/>

android:name=".ResultActivity"
android:screenOrientation="portrait" />
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity android:name=".ActivityWithFragment"></activity>
</application>

</manifest>
</manifest>
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();
}
}
Loading

0 comments on commit 6b16bc5

Please sign in to comment.