diff --git a/README.md b/README.md index f47351d..d70ede9 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ allprojects { 2.添加依赖 ```gradle dependencies { -  compile 'com.github.pqpo:SmartCropper:v1.1.0@aar' +  compile 'com.github.pqpo:SmartCropper:v1.1.3@aar' } ``` diff --git a/README_EN.md b/README_EN.md index 53aac74..e0b9c03 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ Step 1. Add it in your root build.gradle at the end of repositories: Step 2. Add the dependency ``` dependencies { - compile 'com.github.pqpo:SmartCropper:v1.1.0@aar' + compile 'com.github.pqpo:SmartCropper:v1.1.3@aar' } ``` diff --git a/aar/smartcropperlib-1.1.3.aar b/aar/smartcropperlib-1.1.3.aar new file mode 100644 index 0000000..ee18ac5 Binary files /dev/null and b/aar/smartcropperlib-1.1.3.aar differ diff --git a/app/build.gradle b/app/build.gradle index 03891fa..8390ed3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,5 +29,5 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:support-v4:25.3.1' -// compile 'com.github.pqpo:SmartCropper:v1.1.2@aar' +// compile 'com.github.pqpo:SmartCropper:v1.1.3' } diff --git a/smartcropperlib/build.gradle b/smartcropperlib/build.gradle index 87f356b..5bc0c83 100644 --- a/smartcropperlib/build.gradle +++ b/smartcropperlib/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 25 - versionCode 3 - versionName "1.1.2" + versionCode 4 + versionName "1.1.3" externalNativeBuild { cmake { diff --git a/smartcropperlib/src/main/java/me/pqpo/smartcropperlib/view/CropImageView.java b/smartcropperlib/src/main/java/me/pqpo/smartcropperlib/view/CropImageView.java index 0c03f6c..64388b2 100644 --- a/smartcropperlib/src/main/java/me/pqpo/smartcropperlib/view/CropImageView.java +++ b/smartcropperlib/src/main/java/me/pqpo/smartcropperlib/view/CropImageView.java @@ -80,6 +80,8 @@ public class CropImageView extends ImageView { boolean mShowGuideLine = true; // 是否显示辅助线 boolean mShowMagnifier = true;// 是否显示放大镜 + boolean mDragLimit = true;// 是否限制锚点拖动范围为凸四边形 + public CropImageView(Context context) { this(context, null); } @@ -251,6 +253,15 @@ public void setShowMagnifier(boolean showMagnifier) { this.mShowMagnifier = showMagnifier; } + + /** + * 设置是否限制拖动为凸四边形 + * @param dragLimit 是否 + */ + public void setDragLimit(boolean dragLimit) { + this.mDragLimit = dragLimit; + } + /** * 裁剪 * @return 裁剪后的图片 @@ -293,12 +304,14 @@ public boolean checkPoints(Point[] points) { } private long pointSideLine(Point lineP1, Point lineP2, Point point) { + return pointSideLine(lineP1, lineP2, point.x, point.y); + } + + private long pointSideLine(Point lineP1, Point lineP2, int x, int y) { long x1 = lineP1.x; long y1 = lineP1.y; long x2 = lineP2.x; long y2 = lineP2.y; - long x = point.x; - long y = point.y; return (x - x1)*(y2 - y1) - (y - y1)*(x2 - x1); } @@ -527,10 +540,6 @@ private void toImagePointSize(Point dragPoint, MotionEvent event) { if (dragPoint == null) { return; } - float minX = mActLeft; - float maxX = mActLeft + mActWidth; - float minY = mActTop; - float maxY = mActTop + mActHeight; int pointIndex = -1; for (int i = 0; i < mCropPoints.length; i++) { @@ -540,69 +549,66 @@ private void toImagePointSize(Point dragPoint, MotionEvent event) { } } - //(y-y2)/(y1-y2) = (x-x2)/(x1-x2) - // y = (x-x2)/(x1-x2) * (y1-y2) + y2 - // x = (y-y2)/(y1-y2) * (x1-x2) + x2 - float x = getViewPointX(dragPoint.x); - float y = getViewPointY(dragPoint.y); - float x1, y1, x2, y2; - if (pointIndex >= 0) { - switch (pointIndex){ + int x = (int) ((Math.min(Math.max(event.getX(), mActLeft), mActLeft + mActWidth) - mActLeft) / mScaleX); + int y = (int) ((Math.min(Math.max(event.getY(), mActTop), mActTop + mActHeight)- mActTop) / mScaleY); + + if (mDragLimit && pointIndex >= 0) { + Point lt = mCropPoints[0]; + Point rt = mCropPoints[1]; + Point rb = mCropPoints[2]; + Point lb = mCropPoints[3]; + switch (pointIndex) { case 0: - x1 = getViewPointX(mCropPoints[1].x); - y1 = getViewPointY(mCropPoints[1].y); - x2 = getViewPointX(mCropPoints[2].x); - y2 = getViewPointY(mCropPoints[2].y); - maxX = Math.min(maxX, (y-y2)/(y1-y2) * (x1-x2) + x2); - x1 = getViewPointX(mCropPoints[2].x); - y1 = getViewPointY(mCropPoints[2].y); - x2 = getViewPointX(mCropPoints[3].x); - y2 = getViewPointY(mCropPoints[3].y); - maxY = Math.min(maxY, (x-x2)/(x1-x2) * (y1-y2) + y2); + if (pointSideLine(rt, lb, x, y) * pointSideLine(rt, lb, rb) > 0) { + return; + } + if (pointSideLine(rt, rb, x, y) * pointSideLine(rt, rb, lb) < 0) { + return; + } + if (pointSideLine(lb, rb, x, y) * pointSideLine(lb, rb, rt) < 0) { + return; + } break; case 1: - x1 = getViewPointX(mCropPoints[0].x); - y1 = getViewPointY(mCropPoints[0].y); - x2 = getViewPointX(mCropPoints[3].x); - y2 = getViewPointY(mCropPoints[3].y); - minX = Math.max(minX, (y-y2)/(y1-y2) * (x1-x2) + x2); - x1 = getViewPointX(mCropPoints[2].x); - y1 = getViewPointY(mCropPoints[2].y); - x2 = getViewPointX(mCropPoints[3].x); - y2 = getViewPointY(mCropPoints[3].y); - maxY = Math.min(maxY, ((x-x2)/(x1-x2) * (y1-y2) + y2)); + if (pointSideLine(lt, rb, x, y) * pointSideLine(lt, rb, lb) > 0) { + return; + } + if (pointSideLine(lt, lb, x, y) * pointSideLine(lt, lb, rb) < 0) { + return; + } + if (pointSideLine(lb, rb, x, y) * pointSideLine(lb, rb, lt) < 0) { + return; + } break; case 2: - x1 = getViewPointX(mCropPoints[0].x); - y1 = getViewPointY(mCropPoints[0].y); - x2 = getViewPointX(mCropPoints[3].x); - y2 = getViewPointY(mCropPoints[3].y); - minX = Math.max(minX, ((y-y2)/(y1-y2) * (x1-x2) + x2)); - x1 = getViewPointX(mCropPoints[0].x); - y1 = getViewPointY(mCropPoints[0].y); - x2 = getViewPointX(mCropPoints[1].x); - y2 = getViewPointY(mCropPoints[1].y); - minY = Math.max(minY, ((x-x2)/(x1-x2) * (y1-y2) + y2)); + if (pointSideLine(rt, lb, x, y) * pointSideLine(rt, lb, lt) > 0) { + return; + } + if (pointSideLine(lt, rt, x, y) * pointSideLine(lt, rt, lb) < 0) { + return; + } + if (pointSideLine(lt, lb, x, y) * pointSideLine(lt, lb, rt) < 0) { + return; + } break; case 3: - x1 = getViewPointX(mCropPoints[1].x); - y1 = getViewPointY(mCropPoints[1].y); - x2 = getViewPointX(mCropPoints[2].x); - y2 = getViewPointY(mCropPoints[2].y); - maxX = Math.min(maxX, ((y-y2)/(y1-y2) * (x1-x2) + x2)); - x1 = getViewPointX(mCropPoints[0].x); - y1 = getViewPointY(mCropPoints[0].y); - x2 = getViewPointX(mCropPoints[1].x); - y2 = getViewPointY(mCropPoints[1].y); - minY = Math.max(minY, ((x-x2)/(x1-x2) * (y1-y2) + y2)); + if (pointSideLine(lt, rb, x, y) * pointSideLine(lt, rb, rt) > 0) { + return; + } + if (pointSideLine(lt, rt, x, y) * pointSideLine(lt, rt, rb) < 0) { + return; + } + if (pointSideLine(rt, rb, x, y) * pointSideLine(rt, rb, lt) < 0) { + return; + } break; default: break; } } - dragPoint.x = (int) ((Math.min(Math.max(event.getX(), minX), maxX) - mActLeft) / mScaleX); - dragPoint.y = (int) ((Math.min(Math.max(event.getY(), minY), maxY) - mActTop) / mScaleY); + dragPoint.x = x; + dragPoint.y = y; } private float getViewPointX(Point point){