Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Double Click Detection #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -57,6 +57,7 @@ public class SignaturePad extends View {
private long mFirstClick;
private int mCountClick;
private static final int DOUBLE_CLICK_DELAY_MS = 200;
private static final float DOUBLE_CLICK_SQUARED_MOVE_DIST = 75;

//Default attribute values
private final int DEFAULT_ATTR_PEN_MIN_WIDTH_PX = 3;
Expand Down Expand Up @@ -101,6 +102,10 @@ public SignaturePad(Context context, AttributeSet attrs) {

}

public void setClearOnDoubleClick(boolean mClearOnDoubleClick) {
this.mClearOnDoubleClick = mClearOnDoubleClick;
}

@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
Expand Down Expand Up @@ -208,7 +213,6 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
mPoints.clear();
if (isDoubleClick()) break;
mLastTouchX = eventX;
mLastTouchY = eventY;
addPoint(getNewPoint(eventX, eventY));
Expand All @@ -220,6 +224,9 @@ public boolean onTouchEvent(MotionEvent event) {
break;

case MotionEvent.ACTION_UP:
if (this.isDoubleClick(eventX, eventY)) {
break;
}
resetDirtyRect(eventX, eventY);
addPoint(getNewPoint(eventX, eventY));
getParent().requestDisallowInterceptTouchEvent(true);
Expand All @@ -240,6 +247,12 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}

private float calculateSquaredDistance(float startX, float startY, float endX, float endY) {
float xDist = endX - startX;
float yDist = endY - startY;
return xDist * xDist + yDist * yDist;
}

@Override
protected void onDraw(Canvas canvas) {
if (mSignatureBitmap != null) {
Expand Down Expand Up @@ -400,7 +413,7 @@ public Bitmap getTransparentSignatureBitmap(boolean trimBlankSpace) {
return Bitmap.createBitmap(mSignatureBitmap, xMin, yMin, xMax - xMin, yMax - yMin);
}

private boolean isDoubleClick() {
private boolean isDoubleClick(float currentX, float currentY) {
if (mClearOnDoubleClick) {
if (mFirstClick != 0 && System.currentTimeMillis() - mFirstClick > DOUBLE_CLICK_DELAY_MS) {
mCountClick = 0;
Expand All @@ -410,7 +423,8 @@ private boolean isDoubleClick() {
mFirstClick = System.currentTimeMillis();
} else if (mCountClick == 2) {
long lastClick = System.currentTimeMillis();
if (lastClick - mFirstClick < DOUBLE_CLICK_DELAY_MS) {
float dist = this.calculateSquaredDistance(mLastTouchX, mLastTouchY, currentX, currentY);
if (lastClick - mFirstClick < DOUBLE_CLICK_DELAY_MS && dist < DOUBLE_CLICK_SQUARED_MOVE_DIST) {
this.clearView();
return true;
}
Expand Down