Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

add crop with fixed size #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.ImageView;
import android.widget.Toast;

Expand Down
16 changes: 16 additions & 0 deletions lib/src/main/java/com/soundcloud/android/crop/Crop.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Crop {
static interface Extra {
String ASPECT_X = "aspect_x";
String ASPECT_Y = "aspect_y";
String FIXED = "fixed";
String FIXED_X = "fixed_x";
String FIXED_Y = "fixed_y";
String MAX_X = "max_x";
String MAX_Y = "max_y";
String ERROR = "error";
Expand Down Expand Up @@ -73,6 +76,19 @@ public Crop asSquare() {
return this;
}

/**
* Set fixed crop size
*
* @param width Min width
* @param height Min height
*/
public Crop withFixedSize(int width, int height) {
cropIntent.putExtra(Extra.FIXED, true);
cropIntent.putExtra(Extra.FIXED_X, width);
cropIntent.putExtra(Extra.FIXED_Y, height);
return this;
}

/**
* Set maximum crop size
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;

import com.soundcloud.android.crop.util.Log;

import java.io.IOException;
Expand All @@ -55,13 +56,16 @@ public class CropImageActivity extends MonitoredActivity {
private int aspectY;

// Output image size
private int fixedX;
private int fixedY;
private int maxX;
private int maxY;
private int exifRotation;

private Uri sourceUri;
private Uri saveUri;

private boolean isFixed;
private boolean isSaving;

private int sampleSize;
Expand Down Expand Up @@ -116,6 +120,9 @@ private void setupFromIntent() {
if (extras != null) {
aspectX = extras.getInt(Crop.Extra.ASPECT_X);
aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
isFixed = extras.getBoolean(Crop.Extra.FIXED);
fixedX = extras.getInt(Crop.Extra.FIXED_X);
fixedY = extras.getInt(Crop.Extra.FIXED_Y);
maxX = extras.getInt(Crop.Extra.MAX_X);
maxY = extras.getInt(Crop.Extra.MAX_Y);
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
Expand Down Expand Up @@ -220,10 +227,10 @@ private void makeDefault() {

Rect imageRect = new Rect(0, 0, width, height);

// Make the default size about 4/5 of the width or height
int cropWidth = Math.min(width, height) * 4 / 5;
// Make the default size about 4/5 of the width or height or set fixed size
int cropWidth = !isFixed ? Math.min(width, height) * 4 / 5 : fixedX;
@SuppressWarnings("SuspiciousNameCombination")
int cropHeight = cropWidth;
int cropHeight = !isFixed ? cropWidth : fixedY;

if (aspectX != 0 && aspectY != 0) {
if (aspectX > aspectY) {
Expand All @@ -237,7 +244,7 @@ private void makeDefault() {
int y = (height - cropHeight) / 2;

RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight);
hv.setup(imageView.getUnrotatedMatrix(), imageRect, cropRect, aspectX != 0 && aspectY != 0);
hv.setup(imageView.getUnrotatedMatrix(), imageRect, cropRect, aspectX != 0 && aspectY != 0, isFixed);
imageView.add(hv);
}

Expand Down
21 changes: 13 additions & 8 deletions lib/src/main/java/com/soundcloud/android/crop/HighlightView.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ enum HandleMode { Changing, Always, Never }
private float handleRadius;
private float outlineWidth;
private boolean isFocused;
private boolean isFixed;

public HighlightView(View context) {
viewContext = context;
Expand All @@ -95,12 +96,14 @@ private void initStyles(Context context) {
}
}

public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio) {
public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio, boolean isFixed) {
matrix = new Matrix(m);

this.cropRect = cropRect;
this.imageRect = new RectF(imageRect);
this.maintainAspectRatio = maintainAspectRatio;
this.isFixed = isFixed;


initialAspectRatio = this.cropRect.width() / this.cropRect.height();
drawRect = computeLayout();
Expand Down Expand Up @@ -177,7 +180,7 @@ private boolean isClipPathSupported(Canvas canvas) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
return false;
} else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|| Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
|| Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
return true;
} else {
return !canvas.isHardwareAccelerated();
Expand All @@ -198,7 +201,7 @@ private void drawThirds(Canvas canvas) {
outlinePaint.setStrokeWidth(1);
float xThird = (drawRect.right - drawRect.left) / 3;
float yThird = (drawRect.bottom - drawRect.top) / 3;

canvas.drawLine(drawRect.left + xThird, drawRect.top,
drawRect.left + xThird, drawRect.bottom, outlinePaint);
canvas.drawLine(drawRect.left + xThird * 2, drawRect.top,
Expand Down Expand Up @@ -257,7 +260,7 @@ void handleMotion(int edge, float dx, float dy) {
if (edge == MOVE) {
// Convert to image space before sending to moveBy()
moveBy(dx * (cropRect.width() / r.width()),
dy * (cropRect.height() / r.height()));
dy * (cropRect.height() / r.height()));
} else {
if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) {
dx = 0;
Expand All @@ -270,8 +273,10 @@ void handleMotion(int edge, float dx, float dy) {
// Convert to image space before sending to growBy()
float xDelta = dx * (cropRect.width() / r.width());
float yDelta = dy * (cropRect.height() / r.height());
growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta,
(((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta);
if(!isFixed) {
growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta,
(((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta);
}
}
}

Expand Down Expand Up @@ -363,10 +368,10 @@ public Rect getScaledCropRect(float scale) {
// Maps the cropping rectangle from image space to screen space
private Rect computeLayout() {
RectF r = new RectF(cropRect.left, cropRect.top,
cropRect.right, cropRect.bottom);
cropRect.right, cropRect.bottom);
matrix.mapRect(r);
return new Rect(Math.round(r.left), Math.round(r.top),
Math.round(r.right), Math.round(r.bottom));
Math.round(r.right), Math.round(r.bottom));
}

public void invalidate() {
Expand Down