From 3a744c834105d38e2a0b9c5b7488e53b83f91335 Mon Sep 17 00:00:00 2001 From: wwang Date: Wed, 20 May 2020 14:02:44 -0400 Subject: [PATCH] Code optimization --- .../silkysignaturedemo/MainActivity.java | 16 ++++----- app/src/main/res/layout/activity_main.xml | 2 +- .../SilkySignaturePad.java} | 36 +++++++++---------- .../ControlTimedPoints.java | 2 +- .../CurveBezier.java} | 8 ++--- .../TimedPoint.java | 2 +- .../{utils => svgUtils}/SvgBuilder.java | 6 ++-- .../{utils => svgUtils}/SvgPathBuilder.java | 2 +- .../{utils => svgUtils}/SvgPoint.java | 4 ++- .../{view => viewHelper}/ViewCompat.java | 2 +- .../ViewTreeObserverCompat.java | 2 +- silky-signature/src/main/res/values/attrs.xml | 2 +- 12 files changed, 44 insertions(+), 40 deletions(-) rename silky-signature/src/main/java/com/williamww/silkysignature/{views/SignaturePad.java => UI/SilkySignaturePad.java} (93%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils => drawerControllers}/ControlTimedPoints.java (82%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils/Bezier.java => drawerControllers/CurveBezier.java} (85%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils => drawerControllers}/TimedPoint.java (91%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils => svgUtils}/SvgBuilder.java (92%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils => svgUtils}/SvgPathBuilder.java (98%) rename silky-signature/src/main/java/com/williamww/silkysignature/{utils => svgUtils}/SvgPoint.java (92%) rename silky-signature/src/main/java/com/williamww/silkysignature/{view => viewHelper}/ViewCompat.java (94%) rename silky-signature/src/main/java/com/williamww/silkysignature/{view => viewHelper}/ViewTreeObserverCompat.java (93%) diff --git a/app/src/main/java/com/williamww/silkysignaturedemo/MainActivity.java b/app/src/main/java/com/williamww/silkysignaturedemo/MainActivity.java index 567195e..ad8d827 100644 --- a/app/src/main/java/com/williamww/silkysignaturedemo/MainActivity.java +++ b/app/src/main/java/com/williamww/silkysignaturedemo/MainActivity.java @@ -13,7 +13,7 @@ import android.widget.Button; import android.widget.Toast; -import com.williamww.silkysignature.views.SignaturePad; +import com.williamww.silkysignature.UI.SilkySignaturePad; import java.io.File; import java.io.FileOutputStream; @@ -26,7 +26,7 @@ public class MainActivity extends AppCompatActivity { - private SignaturePad mSignaturePad; + private SilkySignaturePad mSilkySignaturePad; private Button mClearButton; private Button mSaveButton; private Button mCompressButton; @@ -36,8 +36,8 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - mSignaturePad = findViewById(R.id.signature_pad); - mSignaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() { + mSilkySignaturePad = findViewById(R.id.signature_pad); + mSilkySignaturePad.setOnSignedListener(new SilkySignaturePad.OnSignedListener() { @Override public void onStartSigning() { Toast.makeText(MainActivity.this, "OnStartSigning", Toast.LENGTH_SHORT).show(); @@ -64,20 +64,20 @@ public void onClear() { mClearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - mSignaturePad.clear(); + mSilkySignaturePad.clear(); } }); mSaveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap(); + Bitmap signatureBitmap = mSilkySignaturePad.getSignatureBitmap(); if (addJpgSignatureToGallery(signatureBitmap)) { Toast.makeText(MainActivity.this, "Signature saved into the Gallery", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Unable to store the signature", Toast.LENGTH_SHORT).show(); } - if (addSvgSignatureToGallery(mSignaturePad.getSignatureSvg())) { + if (addSvgSignatureToGallery(mSilkySignaturePad.getSignatureSvg())) { Toast.makeText(MainActivity.this, "SVG Signature saved into the Gallery", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Unable to store the SVG signature", Toast.LENGTH_SHORT).show(); @@ -89,7 +89,7 @@ public void onClick(View view) { mCompressButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Bitmap signatureBitmap = mSignaturePad.getCompressedSignatureBitmap(50); + Bitmap signatureBitmap = mSilkySignaturePad.getCompressedSignatureBitmap(50); if (addJpgSignatureToGallery(signatureBitmap)) { Toast.makeText(MainActivity.this, "50% compressed signature saved into the Gallery", Toast.LENGTH_SHORT).show(); } else { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 1e02aad..de3ea43 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -33,7 +33,7 @@ android:text="@string/agreement" android:textColor="@android:color/darker_gray" /> - diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/views/SignaturePad.java b/silky-signature/src/main/java/com/williamww/silkysignature/UI/SilkySignaturePad.java similarity index 93% rename from silky-signature/src/main/java/com/williamww/silkysignature/views/SignaturePad.java rename to silky-signature/src/main/java/com/williamww/silkysignature/UI/SilkySignaturePad.java index dfc2c32..290115d 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/views/SignaturePad.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/UI/SilkySignaturePad.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.views; +package com.williamww.silkysignature.UI; import android.content.Context; import android.content.res.Resources; @@ -15,18 +15,18 @@ import android.view.ViewTreeObserver; import com.williamww.silkysignature.R; -import com.williamww.silkysignature.utils.Bezier; -import com.williamww.silkysignature.utils.ControlTimedPoints; -import com.williamww.silkysignature.utils.SvgBuilder; -import com.williamww.silkysignature.utils.TimedPoint; -import com.williamww.silkysignature.view.ViewCompat; -import com.williamww.silkysignature.view.ViewTreeObserverCompat; +import com.williamww.silkysignature.drawerControllers.CurveBezier; +import com.williamww.silkysignature.drawerControllers.ControlTimedPoints; +import com.williamww.silkysignature.svgUtils.SvgBuilder; +import com.williamww.silkysignature.drawerControllers.TimedPoint; +import com.williamww.silkysignature.viewHelper.ViewCompat; +import com.williamww.silkysignature.viewHelper.ViewTreeObserverCompat; import java.util.ArrayList; import java.util.List; -public class SignaturePad extends View { +public class SilkySignaturePad extends View { //View state private List mPoints; private boolean mIsEmpty; @@ -41,7 +41,7 @@ public class SignaturePad extends View { // Cache private List mPointsCache = new ArrayList<>(); private ControlTimedPoints mControlTimedPointsCached = new ControlTimedPoints(); - private Bezier mBezierCached = new Bezier(); + private CurveBezier mCurveBezierCached = new CurveBezier(); //Configurable parameters private int mMinWidth; @@ -66,21 +66,21 @@ public class SignaturePad extends View { private Bitmap mSignatureBitmap = null; private Canvas mSignatureBitmapCanvas = null; - public SignaturePad(Context context, AttributeSet attrs) { + public SilkySignaturePad(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.getTheme().obtainStyledAttributes( attrs, - R.styleable.SignaturePad, + R.styleable.SilkySignaturePad, 0, 0); //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)); - 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); + mMinWidth = a.getDimensionPixelSize(R.styleable.SilkySignaturePad_penMinWidth, convertDpToPx(DEFAULT_ATTR_PEN_MIN_WIDTH_PX)); + mMaxWidth = a.getDimensionPixelSize(R.styleable.SilkySignaturePad_penMaxWidth, convertDpToPx(DEFAULT_ATTR_PEN_MAX_WIDTH_PX)); + mPaint.setColor(a.getColor(R.styleable.SilkySignaturePad_penColor, DEFAULT_ATTR_PEN_COLOR)); + mVelocityFilterWeight = a.getFloat(R.styleable.SilkySignaturePad_velocityFilterWeight, DEFAULT_ATTR_VELOCITY_FILTER_WEIGHT); + mClearOnDoubleClick = a.getBoolean(R.styleable.SilkySignaturePad_clearOnDoubleClick, DEFAULT_ATTR_CLEAR_ON_DOUBLE_CLICK); } finally { a.recycle(); } @@ -486,7 +486,7 @@ private void addPoint(TimedPoint newPoint) { TimedPoint c3 = tmp.c1; recyclePoint(tmp.c2); - Bezier curve = mBezierCached.set(mPoints.get(1), c2, c3, mPoints.get(2)); + CurveBezier curve = mCurveBezierCached.set(mPoints.get(1), c2, c3, mPoints.get(2)); TimedPoint startPoint = curve.startPoint; TimedPoint endPoint = curve.endPoint; @@ -525,7 +525,7 @@ private void addPoint(TimedPoint newPoint) { } } - private void addBezier(Bezier curve, float startWidth, float endWidth) { + private void addBezier(CurveBezier curve, float startWidth, float endWidth) { mSvgBuilder.append(curve, (startWidth + endWidth) / 2); ensureSignatureBitmap(); float originalWidth = mPaint.getStrokeWidth(); diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/ControlTimedPoints.java b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/ControlTimedPoints.java similarity index 82% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/ControlTimedPoints.java rename to silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/ControlTimedPoints.java index 035af26..f216099 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/ControlTimedPoints.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/ControlTimedPoints.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.drawerControllers; /** * Created by william on 28/11/16. diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/Bezier.java b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/CurveBezier.java similarity index 85% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/Bezier.java rename to silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/CurveBezier.java index bbf0f2a..33ece40 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/Bezier.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/CurveBezier.java @@ -1,14 +1,14 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.drawerControllers; -public class Bezier { +public class CurveBezier { public TimedPoint startPoint; public TimedPoint control1; public TimedPoint control2; public TimedPoint endPoint; - public Bezier set(TimedPoint startPoint, TimedPoint control1, - TimedPoint control2, TimedPoint endPoint) { + public CurveBezier set(TimedPoint startPoint, TimedPoint control1, + TimedPoint control2, TimedPoint endPoint) { this.startPoint = startPoint; this.control1 = control1; this.control2 = control2; diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/TimedPoint.java b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/TimedPoint.java similarity index 91% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/TimedPoint.java rename to silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/TimedPoint.java index 8d8ab7c..044d601 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/TimedPoint.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/drawerControllers/TimedPoint.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.drawerControllers; public class TimedPoint { public float x; diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgBuilder.java b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgBuilder.java similarity index 92% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgBuilder.java rename to silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgBuilder.java index a463e74..b2b0dbf 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgBuilder.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgBuilder.java @@ -1,4 +1,6 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.svgUtils; + +import com.williamww.silkysignature.drawerControllers.CurveBezier; public class SvgBuilder { @@ -38,7 +40,7 @@ public String build(final int width, final int height) { .toString(); } - public SvgBuilder append(final Bezier curve, final float strokeWidth) { + public SvgBuilder append(final CurveBezier curve, final float strokeWidth) { final Integer roundedStrokeWidth = Math.round(strokeWidth); final SvgPoint curveStartSvgPoint = new SvgPoint(curve.startPoint); final SvgPoint curveControlSvgPoint1 = new SvgPoint(curve.control1); diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPathBuilder.java b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPathBuilder.java similarity index 98% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPathBuilder.java rename to silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPathBuilder.java index f9cea96..23fc74f 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPathBuilder.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPathBuilder.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.svgUtils; /** diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPoint.java b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPoint.java similarity index 92% rename from silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPoint.java rename to silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPoint.java index 5c75f12..4c955fe 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/utils/SvgPoint.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/svgUtils/SvgPoint.java @@ -1,4 +1,6 @@ -package com.williamww.silkysignature.utils; +package com.williamww.silkysignature.svgUtils; + +import com.williamww.silkysignature.drawerControllers.TimedPoint; /** * Represent a point as it would be in the generated SVG document. diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/view/ViewCompat.java b/silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewCompat.java similarity index 94% rename from silky-signature/src/main/java/com/williamww/silkysignature/view/ViewCompat.java rename to silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewCompat.java index f5de78a..79eebf2 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/view/ViewCompat.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewCompat.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.view; +package com.williamww.silkysignature.viewHelper; import android.os.Build; import android.view.View; diff --git a/silky-signature/src/main/java/com/williamww/silkysignature/view/ViewTreeObserverCompat.java b/silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewTreeObserverCompat.java similarity index 93% rename from silky-signature/src/main/java/com/williamww/silkysignature/view/ViewTreeObserverCompat.java rename to silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewTreeObserverCompat.java index 86311fc..427d500 100644 --- a/silky-signature/src/main/java/com/williamww/silkysignature/view/ViewTreeObserverCompat.java +++ b/silky-signature/src/main/java/com/williamww/silkysignature/viewHelper/ViewTreeObserverCompat.java @@ -1,4 +1,4 @@ -package com.williamww.silkysignature.view; +package com.williamww.silkysignature.viewHelper; import android.annotation.SuppressLint; import android.os.Build; diff --git a/silky-signature/src/main/res/values/attrs.xml b/silky-signature/src/main/res/values/attrs.xml index b40803c..c1aab88 100644 --- a/silky-signature/src/main/res/values/attrs.xml +++ b/silky-signature/src/main/res/values/attrs.xml @@ -1,6 +1,6 @@ - +