Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
wwang committed May 20, 2020
1 parent 996dc95 commit 3a744c8
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@
public class MainActivity extends AppCompatActivity
{

private SignaturePad mSignaturePad;
private SilkySignaturePad mSilkySignaturePad;
private Button mClearButton;
private Button mSaveButton;
private Button mCompressButton;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
android:text="@string/agreement"
android:textColor="@android:color/darker_gray" />

<com.williamww.silkysignature.views.SignaturePad
<com.williamww.silkysignature.UI.SilkySignaturePad
android:id="@+id/signature_pad"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.views;
package com.williamww.silkysignature.UI;

import android.content.Context;
import android.content.res.Resources;
Expand All @@ -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<TimedPoint> mPoints;
private boolean mIsEmpty;
Expand All @@ -41,7 +41,7 @@ public class SignaturePad extends View {
// Cache
private List<TimedPoint> mPointsCache = new ArrayList<>();
private ControlTimedPoints mControlTimedPointsCached = new ControlTimedPoints();
private Bezier mBezierCached = new Bezier();
private CurveBezier mCurveBezierCached = new CurveBezier();

//Configurable parameters
private int mMinWidth;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.utils;
package com.williamww.silkysignature.drawerControllers;

/**
* Created by william on 28/11/16.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.utils;
package com.williamww.silkysignature.drawerControllers;

public class TimedPoint {
public float x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.williamww.silkysignature.utils;
package com.williamww.silkysignature.svgUtils;

import com.williamww.silkysignature.drawerControllers.CurveBezier;

public class SvgBuilder {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.utils;
package com.williamww.silkysignature.svgUtils;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.view;
package com.williamww.silkysignature.viewHelper;

import android.os.Build;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.williamww.silkysignature.view;
package com.williamww.silkysignature.viewHelper;

import android.annotation.SuppressLint;
import android.os.Build;
Expand Down
2 changes: 1 addition & 1 deletion silky-signature/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SignaturePad">
<declare-styleable name="SilkySignaturePad">
<attr name="penMinWidth" format="dimension" />
<attr name="penMaxWidth" format="dimension" />
<attr name="penColor" format="color" />
Expand Down

0 comments on commit 3a744c8

Please sign in to comment.