Skip to content

Commit

Permalink
sample improvements - use navigation drawer WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur committed Mar 25, 2016
1 parent 59b2da5 commit 32b0e3b
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 571 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,40 @@ public void setCropShape(CropShape cropShape) {
}

/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while
* false allows it to be changed.
*
* @param fixAspectRatio Boolean that signals whether the aspect ratio should be
* maintained.
* whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public boolean isFixAspectRatio() {
return mCropOverlayView.isFixAspectRatio();
}

/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public void setFixedAspectRatio(boolean fixAspectRatio) {
mCropOverlayView.setFixedAspectRatio(fixAspectRatio);
}

/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when
* resizing the application.
*
* @param guidelines Integer that signals whether the guidelines should be on, off, or
* only showing when resizing.
* Get the current guidelines option set.
*/
public Guidelines getGuidelines() {
return mCropOverlayView.getGuidelines();
}

/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when resizing the application.
*/
public void setGuidelines(Guidelines guidelines) {
mCropOverlayView.setGuidelines(guidelines);
}

/**
* both the X and Y values of the aspectRatio.
*/
public Pair<Integer, Integer> getAspectRatio() {
return new Pair<>(mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY());
}

/**
* Sets the both the X and Y values of the aspectRatio.
*
Expand Down Expand Up @@ -308,17 +321,21 @@ public boolean isShowCropOverlay() {
* default: true, may disable for animation or frame transition.
*/
public void setShowCropOverlay(boolean showCropOverlay) {
mShowCropOverlay = showCropOverlay;
setCropOverlayVisibility();
if (mShowCropOverlay != showCropOverlay) {
mShowCropOverlay = showCropOverlay;
setCropOverlayVisibility();
}
}

/**
* if to show progress bar when image async loading/cropping is in progress.<br>
* default: true, disable to provide custom progress bar UI.
*/
public void setShowProgressBar(boolean showProgressBar) {
mShowProgressBar = showProgressBar;
setProgressBarVisibility();
if (mShowProgressBar != showProgressBar) {
mShowProgressBar = showProgressBar;
setProgressBarVisibility();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,46 +209,59 @@ public void setCropShape(CropImageView.CropShape cropShape) {
}

/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to
* show when resizing the application.
*
* @param guidelines Integer that signals whether the guidelines should be
* on, off, or only showing when resizing.
* Get the current guidelines option set.
*/
public CropImageView.Guidelines getGuidelines() {
return mGuidelines;
}

/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when resizing the application.
*/
public void setGuidelines(CropImageView.Guidelines guidelines) {
mGuidelines = guidelines;
if (initializedCropWindow) {
initCropWindow();
invalidate();
if (mGuidelines != guidelines) {
mGuidelines = guidelines;
if (initializedCropWindow) {
initCropWindow();
invalidate();
}
}
}

/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect
* ratio, while false allows it to be changed.
*
* @param fixAspectRatio Boolean that signals whether the aspect ratio
* should be maintained.
* whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public void setFixedAspectRatio(boolean fixAspectRatio) {
mFixAspectRatio = fixAspectRatio;
public boolean isFixAspectRatio() {
return mFixAspectRatio;
}

if (initializedCropWindow) {
initCropWindow();
invalidate();
/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public void setFixedAspectRatio(boolean fixAspectRatio) {
if (mFixAspectRatio != fixAspectRatio) {
mFixAspectRatio = fixAspectRatio;
if (initializedCropWindow) {
initCropWindow();
invalidate();
}
}
}

/**
* the X value of the aspect ratio;
*/
public int getAspectRatioX() {
return mAspectRatioX;
}

/**
* Sets the X value of the aspect ratio; is defaulted to 1.
*
* @param aspectRatioX int that specifies the new X value of the aspect
* ratio
*/
public void setAspectRatioX(int aspectRatioX) {
if (aspectRatioX <= 0)
if (aspectRatioX <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
else {
} else if (mAspectRatioX != aspectRatioX) {
mAspectRatioX = aspectRatioX;
mTargetAspectRatio = ((float) mAspectRatioX) / mAspectRatioY;

Expand All @@ -259,6 +272,13 @@ public void setAspectRatioX(int aspectRatioX) {
}
}

/**
* the Y value of the aspect ratio;
*/
public int getAspectRatioY() {
return mAspectRatioY;
}

/**
* Sets the Y value of the aspect ratio; is defaulted to 1.
*
Expand All @@ -268,7 +288,7 @@ public void setAspectRatioX(int aspectRatioX) {
public void setAspectRatioY(int aspectRatioY) {
if (aspectRatioY <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
} else {
} else if (mAspectRatioY != aspectRatioY) {
mAspectRatioY = aspectRatioY;
mTargetAspectRatio = ((float) mAspectRatioX) / mAspectRatioY;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"

package com.theartofdev.edmodo.cropper.sample;

import android.util.Pair;
import android.widget.ImageView;

import com.theartofdev.edmodo.cropper.CropImageView;

/**
* The crop image view options that can be changed live.
*/
final class CropImageViewOptions {

public ImageView.ScaleType scaleType = ImageView.ScaleType.CENTER_INSIDE;

public CropImageView.CropShape cropShape = CropImageView.CropShape.RECTANGLE;

public CropImageView.Guidelines guidelines = CropImageView.Guidelines.ON_TOUCH;

public Pair<Integer, Integer> aspectRatio = new Pair<>(1, 1);

public boolean fixAspectRatio;

public boolean showCropOverlay;

public boolean showProgressBar;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.croppersample.R;
import com.theartofdev.edmodo.cropper.CropImageHelper;
import com.theartofdev.edmodo.cropper.CropImageView;

public class MainActivity extends Activity {

Expand All @@ -42,12 +46,19 @@ public class MainActivity extends Activity {
private MainFragment mCurrentFragment;

private Uri mCropImageUri;

private CropImageViewOptions mCropImageViewOptions = new CropImageViewOptions();
//endregion

public void setCurrentFragment(MainFragment fragment) {
mCurrentFragment = fragment;
}

public void setCurrentOptions(CropImageViewOptions options) {
mCropImageViewOptions = options;
updateDrawerTogglesByOptions(options);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -126,26 +137,78 @@ public void onDrawerOptionClicked(View view) {
switch (view.getId()) {
case R.id.drawer_option_load:
CropImageHelper.startPickImageActivity(this);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_oval:
setMainFragmentByPreset(CropDemoPreset.CIRCULAR);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_rect:
setMainFragmentByPreset(CropDemoPreset.RECT);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_customized_overlay:
setMainFragmentByPreset(CropDemoPreset.CUSTOMIZED_OVERLAY);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_min_max_override:
setMainFragmentByPreset(CropDemoPreset.MIN_MAX_OVERRIDE);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_scale_center:
setMainFragmentByPreset(CropDemoPreset.SCALE_CENTER_INSIDE);
mDrawerLayout.closeDrawers();
break;
case R.id.drawer_option_toggle_scale:
mCropImageViewOptions.scaleType = mCropImageViewOptions.scaleType == ImageView.ScaleType.FIT_CENTER
? ImageView.ScaleType.CENTER_INSIDE : ImageView.ScaleType.FIT_CENTER;
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
case R.id.drawer_option_toggle_shape:
mCropImageViewOptions.cropShape = mCropImageViewOptions.cropShape == CropImageView.CropShape.RECTANGLE
? CropImageView.CropShape.OVAL : CropImageView.CropShape.RECTANGLE;
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
case R.id.drawer_option_toggle_guidelines:
mCropImageViewOptions.guidelines = mCropImageViewOptions.guidelines == CropImageView.Guidelines.OFF
? CropImageView.Guidelines.ON : mCropImageViewOptions.guidelines == CropImageView.Guidelines.ON
? CropImageView.Guidelines.ON_TOUCH : CropImageView.Guidelines.OFF;
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
case R.id.drawer_option_toggle_aspect_ratio:
if (!mCropImageViewOptions.fixAspectRatio) {
mCropImageViewOptions.fixAspectRatio = true;
mCropImageViewOptions.aspectRatio = new Pair<>(1, 1);
} else {
if (mCropImageViewOptions.aspectRatio.first == 1 && mCropImageViewOptions.aspectRatio.second == 1) {
mCropImageViewOptions.aspectRatio = new Pair<>(4, 3);
} else if (mCropImageViewOptions.aspectRatio.first == 4 && mCropImageViewOptions.aspectRatio.second == 3) {
mCropImageViewOptions.aspectRatio = new Pair<>(16, 9);
} else if (mCropImageViewOptions.aspectRatio.first == 16 && mCropImageViewOptions.aspectRatio.second == 9) {
mCropImageViewOptions.aspectRatio = new Pair<>(9, 16);
} else {
mCropImageViewOptions.fixAspectRatio = false;
}
}
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
case R.id.drawer_option_toggle_show_overlay:
mCropImageViewOptions.showCropOverlay = !mCropImageViewOptions.showCropOverlay;
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
case R.id.drawer_option_toggle_show_progress_bar:
mCropImageViewOptions.showProgressBar = !mCropImageViewOptions.showProgressBar;
mCurrentFragment.setCropImageViewOptions(mCropImageViewOptions);
updateDrawerTogglesByOptions(mCropImageViewOptions);
break;
default:
Toast.makeText(this, "Unknown drawer option clicked", Toast.LENGTH_LONG).show();
}
mDrawerLayout.closeDrawers();
}

private void setMainFragmentByPreset(CropDemoPreset demoPreset) {
Expand All @@ -154,4 +217,18 @@ private void setMainFragmentByPreset(CropDemoPreset demoPreset) {
.replace(R.id.container, MainFragment.newInstance(demoPreset))
.commit();
}

private void updateDrawerTogglesByOptions(CropImageViewOptions options) {
((TextView) findViewById(R.id.drawer_option_toggle_scale)).setText(getResources().getString(R.string.drawer_option_toggle_scale, options.scaleType.name()));
((TextView) findViewById(R.id.drawer_option_toggle_shape)).setText(getResources().getString(R.string.drawer_option_toggle_shape, options.cropShape.name()));
((TextView) findViewById(R.id.drawer_option_toggle_guidelines)).setText(getResources().getString(R.string.drawer_option_toggle_guidelines, options.guidelines.name()));
((TextView) findViewById(R.id.drawer_option_toggle_show_overlay)).setText(getResources().getString(R.string.drawer_option_toggle_show_overlay, Boolean.toString(options.showCropOverlay)));
((TextView) findViewById(R.id.drawer_option_toggle_show_progress_bar)).setText(getResources().getString(R.string.drawer_option_toggle_show_progress_bar, Boolean.toString(options.showProgressBar)));

String aspectRatio = "FREE";
if (options.fixAspectRatio) {
aspectRatio = options.aspectRatio.first + ":" + options.aspectRatio.second;
}
((TextView) findViewById(R.id.drawer_option_toggle_aspect_ratio)).setText(getResources().getString(R.string.drawer_option_toggle_aspect_ratio, aspectRatio));
}
}
Loading

0 comments on commit 32b0e3b

Please sign in to comment.