Skip to content

Commit

Permalink
made initial height and width to be relative, fixed onMeasure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
polyak01 committed Feb 6, 2017
1 parent 2c0948e commit 8cd922f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 17 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.1.0'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Mon Feb 06 08:14:31 EET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public CropIwaImageView(Context context, AttributeSet attrs, int defStyleAttr, i
}

{
setImageResource(android.R.drawable.ic_menu_always_landscape_portrait);
setImageResource(R.drawable.default_image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*/
public class CropIwaOverlayConfig {

//Default size of crop area in percents
private static final int DEFAULT_INITIAL_HEIGHT = 50;
private static final int DEFAULT_INITIAL_WIDTH = 50;

public static CropIwaOverlayConfig createDefault(Context context) {
ResUtil r = new ResUtil(context);
return new CropIwaOverlayConfig()
Expand All @@ -18,8 +22,8 @@ public static CropIwaOverlayConfig createDefault(Context context) {
.setBorderStrokeWidth(r.dimen(R.dimen.cropiwa_default_border_stroke_width))
.setCornerStrokeWidth(r.dimen(R.dimen.cropiwa_default_corner_stroke_width))
.setGridStrokeWidth(r.dimen(R.dimen.cropiwa_default_grid_stroke_width))
.setInitialWidth(r.dimen(R.dimen.cropiwa_default_initial_width))
.setInitialHeight(r.dimen(R.dimen.cropiwa_default_initial_height))
.setInitialWidth(DEFAULT_INITIAL_WIDTH)
.setInitialHeight(DEFAULT_INITIAL_HEIGHT)
.setMinWidth(r.dimen(R.dimen.cropiwa_default_min_width))
.setMinHeight(r.dimen(R.dimen.cropiwa_default_min_height))
.setShouldCropCircle(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
boolean cornerPointsAreNotInitialized = cornerPoints[0] == null;
if (cornerPointsAreNotInitialized) {
float centerX = w * 0.5f, centerY = h * 0.5f;
float halfWidth = config.getInitialWidth() * 0.5f;
float halfHeight = config.getInitialHeight() * 0.5f;
//Initial width/height are in percents of view's width and height
float halfWidth = w * config.getInitialWidth() * 0.01f * 0.5f;
float halfHeight = h * config.getInitialHeight() * 0.01f * 0.5f;
cropRect.set(
centerX - halfWidth, centerY - halfHeight,
centerX + halfWidth, centerY + halfHeight);
Expand All @@ -92,6 +93,12 @@ private void initCornerPoints() {
cornerPoints[RIGHT_BOTTOM] = new CornerPoint(rightBot, leftBot, rightTop);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//We will get here measured dimensions of an ImageView
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
Expand Down Expand Up @@ -219,7 +226,7 @@ private void drawGrid(Canvas canvas, Paint paint) {
private void configurePaintToDrawCorners(Paint paint) {
paint.setColor(config.getCornerColor());
paint.setStrokeWidth(config.getCornerStrokeWidth());
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}

private void configurePaintToDrawOverlay(Paint paint) {
Expand Down
21 changes: 19 additions & 2 deletions library/src/main/java/com/steelkiwi/cropiwa/CropIwaView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,46 @@ public class CropIwaView extends FrameLayout {
private CropIwaImageView imageView;
private CropIwaOverlayView overlayView;

private CropIwaOverlayConfig overlayConfig;

public CropIwaView(Context context) {
super(context);
init(null);
}

public CropIwaView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}

public CropIwaView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CropIwaView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}

{
private void init(AttributeSet attrs) {
imageView = new CropIwaImageView(getContext());
addView(imageView);

CropIwaOverlayConfig overlayConfig = CropIwaOverlayConfig.createDefault(getContext());
overlayConfig = CropIwaOverlayConfig.createDefault(getContext());
overlayView = new CropIwaOverlayView(getContext(), overlayConfig);
addView(overlayView);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
imageView.measure(widthMeasureSpec, heightMeasureSpec);
overlayView.measure(
imageView.getMeasuredWidthAndState(),
imageView.getMeasuredHeightAndState());
setMeasuredDimension(
imageView.getMeasuredWidthAndState(),
imageView.getMeasuredHeightAndState());
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cropiwa_default_overlay_color">#af000000</color>
<color name="cropiwa_default_border_color">#fff</color>
<color name="cropiwa_default_corner_color">#fff</color>
<color name="cropiwa_default_border_color">#f5f5f5</color>
<color name="cropiwa_default_corner_color">#f5f5f5</color>
<color name="cropiwa_default_grid_color">#fff</color>
</resources>
6 changes: 2 additions & 4 deletions library/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cropiwa_default_border_stroke_width">6dp</dimen>
<dimen name="cropiwa_default_corner_stroke_width">10dp</dimen>
<dimen name="cropiwa_default_border_stroke_width">1dp</dimen>
<dimen name="cropiwa_default_corner_stroke_width">4dp</dimen>
<dimen name="cropiwa_default_grid_stroke_width">4dp</dimen>
<dimen name="cropiwa_default_min_height">40dp</dimen>
<dimen name="cropiwa_default_min_width">40dp</dimen>
<dimen name="cropiwa_default_initial_height">100dp</dimen>
<dimen name="cropiwa_default_initial_width">120dp</dimen>
</resources>
Binary file added sample/src/main/res/drawable/default_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8cd922f

Please sign in to comment.