Skip to content

Commit

Permalink
updated API to configure enabled set of gestures on each tab
Browse files Browse the repository at this point in the history
  • Loading branch information
shliama committed Feb 3, 2016
1 parent 73c0ba2 commit cefcc02
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
classpath 'com.android.tools.build:gradle:2.0.0-beta1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ private UCrop advancedConfig(@NonNull UCrop uCrop) {
options.setCompressionQuality(mSeekBarQuality.getProgress());

/*
If you want to unlock all gestures for all UCropActivity tabs
If you want to configure how gestures work for all UCropActivity tabs
options.setGesturesAlwaysEnabled(true);
options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
* */

/*
Expand Down
11 changes: 6 additions & 5 deletions ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static class Options {
public static final String EXTRA_COMPRESSION_FORMAT_NAME = EXTRA_PREFIX + ".CompressionFormatName";
public static final String EXTRA_COMPRESSION_QUALITY = EXTRA_PREFIX + ".CompressionQuality";

public static final String EXTRA_GESTURES_ALWAYS_ENABLED = EXTRA_PREFIX + ".GesturesAlwaysEnabled";
public static final String EXTRA_ALLOWED_GESTURES = EXTRA_PREFIX + ".AllowedGestures";

public static final String EXTRA_MAX_BITMAP_SIZE = EXTRA_PREFIX + ".MaxBitmapSize";
public static final String EXTRA_MAX_SCALE_MULTIPLIER = EXTRA_PREFIX + ".MaxScaleMultiplier";
Expand Down Expand Up @@ -246,11 +246,12 @@ public void setCompressionQuality(@IntRange(from = 0) int compressQuality) {
}

/**
* If you want all gestures to be enabled simultaneously on all tabs (scale/rotate/crop ratio)
* set it to true.
* Choose what set of gestures will be enabled on each tab - if any.
*/
public void setGesturesAlwaysEnabled(boolean gesturesAlwaysEnabled) {
mOptionBundle.putBoolean(EXTRA_GESTURES_ALWAYS_ENABLED, gesturesAlwaysEnabled);
public void setAllowedGestures(@UCropActivity.GestureTypes int tabScale,
@UCropActivity.GestureTypes int tabRotate,
@UCropActivity.GestureTypes int tabAspectRatio) {
mOptionBundle.putIntArray(EXTRA_ALLOWED_GESTURES, new int[]{tabScale, tabRotate, tabAspectRatio});
}

/**
Expand Down
42 changes: 35 additions & 7 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.IdRes;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
Expand All @@ -35,6 +36,8 @@
import com.yalantis.ucrop.view.widget.HorizontalProgressWheelView;

import java.io.OutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -46,8 +49,20 @@ public class UCropActivity extends AppCompatActivity {
public static final int DEFAULT_COMPRESS_QUALITY = 90;
public static final Bitmap.CompressFormat DEFAULT_COMPRESS_FORMAT = Bitmap.CompressFormat.JPEG;

public static final int NONE = 0;
public static final int SCALE = 1;
public static final int ROTATE = 2;
public static final int ALL = 3;

@IntDef({NONE, SCALE, ROTATE, ALL})
@Retention(RetentionPolicy.SOURCE)
public @interface GestureTypes {

}

private static final String TAG = "UCropActivity";

private static final int TABS_COUNT = 3;
private static final int SCALE_WIDGET_SENSITIVITY_COEFFICIENT = 15000;
private static final int ROTATE_WIDGET_SENSITIVITY_COEFFICIENT = 42;

Expand All @@ -62,7 +77,7 @@ public class UCropActivity extends AppCompatActivity {

private Bitmap.CompressFormat mCompressFormat = DEFAULT_COMPRESS_FORMAT;
private int mCompressQuality = DEFAULT_COMPRESS_QUALITY;
private boolean mGesturesAlwaysEnabled = false;
private int[] mAllowedGestures = new int[]{SCALE, ROTATE, ALL};

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -165,7 +180,10 @@ private void processOptions(@NonNull Intent intent) {
mCompressQuality = optionsBundle.getInt(UCrop.Options.EXTRA_COMPRESSION_QUALITY, UCropActivity.DEFAULT_COMPRESS_QUALITY);

// Gestures options
mGesturesAlwaysEnabled = optionsBundle.getBoolean(UCrop.Options.EXTRA_GESTURES_ALWAYS_ENABLED, false);
int[] allowedGestures = optionsBundle.getIntArray(UCrop.Options.EXTRA_ALLOWED_GESTURES);
if (allowedGestures != null && allowedGestures.length == TABS_COUNT) {
mAllowedGestures = allowedGestures;
}

// Crop image view options
mGestureCropImageView.setMaxBitmapSize(optionsBundle.getInt(UCrop.Options.EXTRA_MAX_BITMAP_SIZE, CropImageView.DEFAULT_MAX_BITMAP_SIZE));
Expand Down Expand Up @@ -246,17 +264,17 @@ private void setupStatesWrapper() {

Drawable stateScaleSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_scale).mutate();
stateScaleSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateScaleSelector.addState(new int[] {android.R.attr.state_selected}, stateScaleSelectedDrawable);
stateScaleSelector.addState(new int[]{android.R.attr.state_selected}, stateScaleSelectedDrawable);
stateScaleSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_scale));

Drawable stateRotateSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_rotate).mutate();
stateRotateSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateRotateSelector.addState(new int[] {android.R.attr.state_selected}, stateRotateSelectedDrawable);
stateRotateSelector.addState(new int[]{android.R.attr.state_selected}, stateRotateSelectedDrawable);
stateRotateSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_rotate));

Drawable stateAspectRatioSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_crop).mutate();
stateAspectRatioSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateAspectRatioSelector.addState(new int[] {android.R.attr.state_selected}, stateAspectRatioSelectedDrawable);
stateAspectRatioSelector.addState(new int[]{android.R.attr.state_selected}, stateAspectRatioSelectedDrawable);
stateAspectRatioSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_crop));

stateScaleImageView.setImageDrawable(stateScaleSelector);
Expand Down Expand Up @@ -409,8 +427,18 @@ private void setWidgetState(@IdRes int stateViewId) {
mLayoutRotate.setVisibility(stateViewId == R.id.state_rotate ? View.VISIBLE : View.GONE);
mLayoutScale.setVisibility(stateViewId == R.id.state_scale ? View.VISIBLE : View.GONE);

mGestureCropImageView.setRotateEnabled(mGesturesAlwaysEnabled || stateViewId != R.id.state_scale);
mGestureCropImageView.setScaleEnabled(mGesturesAlwaysEnabled || stateViewId != R.id.state_rotate);
if (stateViewId == R.id.state_scale) {
setAllowedGestures(0);
} else if (stateViewId == R.id.state_rotate) {
setAllowedGestures(1);
} else {
setAllowedGestures(2);
}
}

private void setAllowedGestures(int tab) {
mGestureCropImageView.setScaleEnabled(mAllowedGestures[tab] == ALL || mAllowedGestures[tab] == SCALE);
mGestureCropImageView.setRotateEnabled(mAllowedGestures[tab] == ALL || mAllowedGestures[tab] == ROTATE);
}

private void cropAndSaveImage() {
Expand Down

0 comments on commit cefcc02

Please sign in to comment.