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

Rotation fix #117

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
9 changes: 5 additions & 4 deletions SignaturePad-Example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 9

defaultConfig {
minSdkVersion 14
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
Expand All @@ -21,6 +22,6 @@ android {
}

dependencies {
compile project(":signature-pad")
compile 'com.android.support:appcompat-v7:25.3.1'
implementation project(":signature-pad")
implementation 'com.android.support:appcompat-v7:26.0.1'
}
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
buildscript {
repositories {
jcenter()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
}
}

Expand All @@ -16,5 +17,6 @@ allprojects {

repositories {
jcenter()
google()
}
}
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=gcacace
POM_DEVELOPER_NAME=Gianluca Cacace

ANDROID_BUILD_TARGET_SDK_VERSION=25
ANDROID_BUILD_TOOLS_VERSION=25.0.3
ANDROID_BUILD_SDK_VERSION=25
ANDROID_BUILD_TARGET_SDK_VERSION=26
ANDROID_BUILD_TOOLS_VERSION=26.0.2
ANDROID_BUILD_SDK_VERSION=26
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 @@
#Tue Aug 08 11:36:01 SGT 2017
#Wed May 09 13:17:36 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
3 changes: 2 additions & 1 deletion signature-pad/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION


defaultConfig {
minSdkVersion 9
minSdkVersion 14
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public float length() {

}

public double point(float t, float start, float c1, float c2, float end) {
private double point(float t, float start, float c1, float c2, float end) {
return start * (1.0 - t) * (1.0 - t) * (1.0 - t)
+ 3.0 * c1 * (1.0 - t) * (1.0 - t) * t
+ 3.0 * c2 * (1.0 - t) * t * t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/
public class SvgPathBuilder {

public static final Character SVG_RELATIVE_CUBIC_BEZIER_CURVE = 'c';
public static final Character SVG_MOVE = 'M';
private static final Character SVG_RELATIVE_CUBIC_BEZIER_CURVE = 'c';
private static final Character SVG_MOVE = 'M';
private final StringBuilder mStringBuilder;
private final Integer mStrokeWidth;
private final SvgPoint mStartPoint;
private SvgPoint mLastPoint;

public SvgPathBuilder(final SvgPoint startPoint, final Integer strokeWidth) {
SvgPathBuilder(final SvgPoint startPoint, final Integer strokeWidth) {
mStrokeWidth = strokeWidth;
mStartPoint = startPoint;
mLastPoint = startPoint;
Expand Down Expand Up @@ -57,16 +57,8 @@ private String makeRelativeCubicBezierCurve(final SvgPoint controlPoint1, final
final String sControlPoint2 = controlPoint2.toRelativeCoordinates(mLastPoint);
final String sEndPoint = endPoint.toRelativeCoordinates(mLastPoint);

final StringBuilder sb = new StringBuilder();
sb.append(sControlPoint1);
sb.append(" ");
sb.append(sControlPoint2);
sb.append(" ");
sb.append(sEndPoint);
sb.append(" ");

// discard zero curve
final String svg = sb.toString();
final String svg = sControlPoint1 + " " + sControlPoint2 + " " + sEndPoint + " ";
if ("c0 0 0 0 0 0".equals(svg)) {
return "";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@
*/
class SvgPoint {

final Integer x, y;
private final Integer x, y;

public SvgPoint(TimedPoint point) {
SvgPoint(TimedPoint point) {
// one optimisation is to get rid of decimals as they are mostly non-significant in the
// produced SVG image
x = Math.round(point.x);
y = Math.round(point.y);
}

public SvgPoint(int x, int y) {
private SvgPoint(int x, int y) {
this.x = x;
this.y = y;
}

public String toAbsoluteCoordinates() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(x);
stringBuilder.append(",");
stringBuilder.append(y);
return stringBuilder.toString();
private String toAbsoluteCoordinates() {
return String.valueOf(x) + "," + y;
}

public String toRelativeCoordinates(final SvgPoint referencePoint) {
Expand All @@ -43,8 +39,7 @@ public boolean equals(Object o) {

SvgPoint svgPoint = (SvgPoint) o;

if (!x.equals(svgPoint.x)) return false;
return y.equals(svgPoint.y);
return x.equals(svgPoint.x) && y.equals(svgPoint.y);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class TimedPoint {
public float x;
public float y;
public long timestamp;
private long timestamp;

public TimedPoint set(float x, float y) {
this.x = x;
Expand All @@ -24,7 +24,7 @@ public float velocityFrom(TimedPoint start) {
return velocity;
}

public float distanceTo(TimedPoint point) {
return (float) Math.sqrt(Math.pow(point.x - this.x, 2) + Math.pow(point.y - this.y, 2));
private float distanceTo(TimedPoint point) {
return (float) Math.hypot(point.x - this.x, point.y - this.y);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.gcacace.signaturepad.views;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
Expand All @@ -27,6 +28,10 @@
import java.util.ArrayList;
import java.util.List;

import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.content.res.Configuration.ORIENTATION_UNDEFINED;

public class SignaturePad extends View {
//View state
private List<TimedPoint> mPoints;
Expand Down Expand Up @@ -79,11 +84,15 @@ public SignaturePad(Context context, AttributeSet attrs) {

//Configurable parameters
try {
mMinWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMinWidth, convertDpToPx(DEFAULT_ATTR_PEN_MIN_WIDTH_PX));
mMaxWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMaxWidth, convertDpToPx(DEFAULT_ATTR_PEN_MAX_WIDTH_PX));
mMinWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMinWidth,
convertDpToPx(DEFAULT_ATTR_PEN_MIN_WIDTH_PX));
mMaxWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMaxWidth,
convertDpToPx(DEFAULT_ATTR_PEN_MAX_WIDTH_PX));
mPaint.setColor(a.getColor(R.styleable.SignaturePad_penColor, DEFAULT_ATTR_PEN_COLOR));
mVelocityFilterWeight = a.getFloat(R.styleable.SignaturePad_velocityFilterWeight, DEFAULT_ATTR_VELOCITY_FILTER_WEIGHT);
mClearOnDoubleClick = a.getBoolean(R.styleable.SignaturePad_clearOnDoubleClick, DEFAULT_ATTR_CLEAR_ON_DOUBLE_CLICK);
mVelocityFilterWeight = a.getFloat(R.styleable.SignaturePad_velocityFilterWeight,
DEFAULT_ATTR_VELOCITY_FILTER_WEIGHT);
mClearOnDoubleClick = a.getBoolean(R.styleable.SignaturePad_clearOnDoubleClick,
DEFAULT_ATTR_CLEAR_ON_DOUBLE_CLICK);
} finally {
a.recycle();
}
Expand All @@ -97,6 +106,8 @@ public SignaturePad(Context context, AttributeSet attrs) {
//Dirty rectangle to update only the changed portion of the view
mDirtyRect = new RectF();

// this.orientation = context.getResources().getConfiguration().orientation;

clearView();

}
Expand Down Expand Up @@ -135,7 +146,7 @@ public void setPenColorRes(int colorRes) {
try {
setPenColor(getResources().getColor(colorRes));
} catch (Resources.NotFoundException ex) {
setPenColor(Color.parseColor("#000000"));
setPenColor(Color.BLACK);
}
}

Expand Down Expand Up @@ -247,6 +258,15 @@ protected void onDraw(Canvas canvas) {
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
ensureSignatureBitmap();
float scale = Math.min((float) w / mSignatureBitmap.getWidth(), (float) h / mSignatureBitmap.getHeight());
mSignatureBitmap = Bitmap.createScaledBitmap(mSignatureBitmap, (int)(w * scale), (int)(h * scale), true);
mSignatureBitmapCanvas = new Canvas(mSignatureBitmap);
}

public void setOnSignedListener(OnSignedListener listener) {
mOnSignedListener = listener;
}
Expand All @@ -263,7 +283,8 @@ public String getSignatureSvg() {

public Bitmap getSignatureBitmap() {
Bitmap originalBitmap = getTransparentSignatureBitmap();
Bitmap whiteBgBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap whiteBgBitmap = Bitmap.createBitmap(
originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(whiteBgBitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Expand Down Expand Up @@ -550,7 +571,10 @@ private ControlTimedPoints calculateCurveControlPoints(TimedPoint s1, TimedPoint
float tx = s2.x - cmX;
float ty = s2.y - cmY;

return mControlTimedPointsCached.set(getNewPoint(m1X + tx, m1Y + ty), getNewPoint(m2X + tx, m2Y + ty));
return mControlTimedPointsCached.set(
getNewPoint(m1X + tx, m1Y + ty),
getNewPoint(m2X + tx, m2Y + ty)
);
}

private float strokeWidth(float velocity) {
Expand Down