├── HandWriter.iml ├── LICENSE ├── README.md ├── apks └── HandWriter.apk ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── ac │ │ └── iscas │ │ └── handwriter │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── ac │ │ │ └── iscas │ │ │ └── handwriter │ │ │ ├── MainActivity.java │ │ │ ├── MyDatabase.java │ │ │ ├── MyDatabaseHelper.java │ │ │ ├── utils │ │ │ ├── Bezier.java │ │ │ ├── ControlTimedPoints.java │ │ │ ├── SvgBuilder.java │ │ │ ├── SvgPathBuilder.java │ │ │ ├── SvgPoint.java │ │ │ └── TimedPoint.java │ │ │ ├── view │ │ │ ├── ViewCompat.java │ │ │ └── ViewTreeObserverCompat.java │ │ │ └── views │ │ │ └── SignaturePad.java │ └── res │ │ ├── drawable │ │ └── round_button.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── option_item_dialog.xml │ │ └── userid_name.xml │ │ ├── menu │ │ └── popup_menu.xml │ │ ├── mipmap-hdpi │ │ ├── .directory │ │ ├── ic_launcher.png │ │ ├── ic_option_change_paint_color.png │ │ ├── ic_option_enter_username.png │ │ └── ic_option_new_user.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── ac │ └── iscas │ └── handwriter │ └── ExampleUnitTest.java ├── build.gradle ├── build └── generated │ └── mockable-android-23.jar ├── captures └── cn.ac.iscas.handwriter_2016.07.15_14.49.hprof ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── screenshot └── screenshot.png └── settings.gradle /HandWriter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HandWrite 2 | 手写字体在线识别app。 3 | 4 | 提供一个手写板,用户可以通过触摸屏手写输入。 5 | 6 | 手写输入使用贝塞尔曲线圆滑处理,仿制中国毛笔风格输入。 7 | 8 | apks目录下是预编译好的apk文件,可以直接在android 5.0+上安装。 9 | 10 | 截图: 11 | ![screenshot 0](https://github.com/CreateChance/HandWrite/blob/master/screenshot/screenshot.png) 12 | -------------------------------------------------------------------------------- /apks/HandWriter.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CreateChance/HandWrite/6a4a2f9e8ae8b1b51851c5a6fed19967eae7a641/apks/HandWriter.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "cn.ac.iscas.handwriter" 9 | minSdkVersion 21 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.4.0' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/baniel/UsefulTools/AndroidStudio/AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/ac/iscas/handwriter/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter; 2 | 3 | import android.app.AlertDialog; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.content.SharedPreferences; 7 | import android.database.Cursor; 8 | import android.graphics.Bitmap; 9 | import android.graphics.Canvas; 10 | import android.graphics.Color; 11 | import android.net.Uri; 12 | import android.os.Bundle; 13 | import android.app.Activity; 14 | import android.os.Environment; 15 | import android.os.Handler; 16 | import android.os.Looper; 17 | import android.os.Message; 18 | import android.util.Log; 19 | import android.util.SparseArray; 20 | import android.view.MenuItem; 21 | import android.view.View; 22 | import android.widget.Button; 23 | import android.widget.CursorAdapter; 24 | import android.widget.EditText; 25 | import android.widget.PopupMenu; 26 | import android.widget.SimpleCursorAdapter; 27 | import android.widget.TextView; 28 | import android.widget.Toast; 29 | 30 | import java.io.*; 31 | import java.lang.reflect.Field; 32 | import java.lang.reflect.Method; 33 | 34 | import cn.ac.iscas.handwriter.views.SignaturePad; 35 | 36 | public class MainActivity extends Activity { 37 | 38 | private final String TAG = "MainActivity"; 39 | 40 | private SignaturePad mSignaturePad; 41 | private Button mClearButton; 42 | private Button mSaveButton; 43 | private TextView mSignaturepadDescription; 44 | 45 | private final String RECORDS_FILE_NAME = "SignaturePadRecords.txt"; 46 | private FileOutputStream mRecordsFileOutStream = null; 47 | 48 | private final int ERROR_SDCARD_UNMOUNTED_OR_DENIED = 100; 49 | private final int ERROR_FILE_CREATES_FAILED = 101; 50 | private final int EXTERNAL_CHECK_OK = 102; 51 | 52 | private PopupMenu popupMenu = null; 53 | 54 | // database 55 | private MyDatabase mDatabase = null; 56 | 57 | // shared prefs 58 | private final String PREFS_USER = "cn.ac.iscas.handwriter.currentuser"; 59 | private SharedPreferences mUserPreferences; 60 | private SharedPreferences.Editor mUserEditor; 61 | private String mCurrentUsername = null; 62 | private int mCurrentUserID; 63 | private int mCurrentSampleID = 0; 64 | private int mNextUserId = 1; 65 | private final String CURRENT_USERNAME_KEY = "CurrentUsername"; 66 | private final String CURRENT_USER_ID_KEY = "CurrentUserID"; 67 | private final String CURRENT_USER_SAMPLEID_KEY = "CurrentUserSampleID"; 68 | private final String NEXT_USER_ID_KEY = "NextUserID"; 69 | 70 | // writing tags 71 | private final String SIGNATUREID_TAG = "SignatureID:"; 72 | private final String SIGNATURELABLE_TAG = "SignatureLable:"; 73 | 74 | private Handler mWriteEventHandler = null; 75 | private final int MSG_WRITE_COMPLETE = 500; 76 | 77 | @Override 78 | protected void onCreate(Bundle savedInstanceState) { 79 | super.onCreate(savedInstanceState); 80 | setContentView(R.layout.activity_main); 81 | 82 | // init database. 83 | mDatabase = new MyDatabase(this); 84 | 85 | mWriteEventHandler = new Handler() { 86 | @Override 87 | public void handleMessage(Message msg) { 88 | super.handleMessage(msg); 89 | 90 | switch (msg.what) { 91 | case MSG_WRITE_COMPLETE: 92 | // clear pad when complete. 93 | mSignaturePad.clear(); 94 | mSignaturepadDescription.setText(getString(R.string.hint_info) + mCurrentUsername + 95 | ":" + mCurrentUserID + ":" + mCurrentSampleID); 96 | fullScreenDisplay(); 97 | break; 98 | } 99 | } 100 | }; 101 | 102 | mSignaturePad = (SignaturePad) findViewById(R.id.signature_pad); 103 | fullScreenDisplay(); 104 | 105 | mSignaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() { 106 | @Override 107 | public void onStartSigning() { 108 | if (mCurrentUsername == null) { 109 | new AlertDialog.Builder(MainActivity.this) 110 | .setTitle(R.string.no_username_dialog_title) 111 | .setMessage(R.string.no_username_dialog_message) 112 | .setIcon(android.R.drawable.stat_sys_warning) 113 | .setCancelable(false) 114 | .setPositiveButton("ok", new DialogInterface.OnClickListener() { 115 | @Override 116 | public void onClick(DialogInterface dialog, int which) { 117 | mSignaturePad.clear(); 118 | // clear old records. 119 | mSignaturePad.getMotionEventRecord().clear(); 120 | dialog.dismiss(); 121 | } 122 | }).create().show(); 123 | } else { 124 | fullScreenDisplay(); 125 | } 126 | } 127 | 128 | @Override 129 | public void onSigned() { 130 | mSaveButton.setEnabled(true); 131 | mClearButton.setEnabled(true); 132 | } 133 | 134 | @Override 135 | public void onClear() { 136 | mSaveButton.setEnabled(false); 137 | mClearButton.setEnabled(false); 138 | } 139 | }); 140 | 141 | mClearButton = (Button) findViewById(R.id.clear_button); 142 | mSaveButton = (Button) findViewById(R.id.save_button); 143 | mSignaturepadDescription = (TextView) findViewById(R.id.signature_pad_description); 144 | 145 | mClearButton.setOnClickListener(new View.OnClickListener() { 146 | @Override 147 | public void onClick(View view) { 148 | mSignaturePad.clear(); 149 | // clear old records. 150 | mSignaturePad.getMotionEventRecord().clear(); 151 | } 152 | }); 153 | 154 | mSaveButton.setOnClickListener(new View.OnClickListener() { 155 | @Override 156 | public void onClick(View view) { 157 | // show dialog, let user tell us whether this signature is true. 158 | AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 159 | builder.setTitle(R.string.save_confirm_dialog_title) 160 | .setIcon(android.R.drawable.ic_dialog_info) 161 | .setCancelable(false) 162 | .setSingleChoiceItems(new String[]{"否", "是"}, 0, new DialogInterface.OnClickListener() { 163 | @Override 164 | public void onClick(DialogInterface dialog, int which) { 165 | // save this motion event to local txt file here. 166 | SparseArray records = mSignaturePad.getMotionEventRecord(); 167 | new WriteRecordsThread(records, which).start(); 168 | dialog.dismiss(); 169 | } 170 | }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 171 | @Override 172 | public void onClick(DialogInterface dialog, int which) { 173 | fullScreenDisplay(); 174 | dialog.dismiss(); 175 | } 176 | }).create().show(); 177 | } 178 | }); 179 | 180 | // init prefs 181 | mUserPreferences = getSharedPreferences(PREFS_USER, MODE_PRIVATE); 182 | mUserEditor = mUserPreferences.edit(); 183 | 184 | // get settings last time. 185 | mCurrentUsername = mUserPreferences.getString(CURRENT_USERNAME_KEY, null); 186 | mCurrentUserID = mUserPreferences.getInt(CURRENT_USER_ID_KEY, 0); 187 | mNextUserId = mUserPreferences.getInt(NEXT_USER_ID_KEY, 1); 188 | mCurrentSampleID = mUserPreferences.getInt(CURRENT_USER_SAMPLEID_KEY, 0); 189 | mSignaturepadDescription.setText(getString(R.string.hint_info) + mCurrentUsername + 190 | ":" + mCurrentUserID + ":" + mCurrentSampleID); 191 | 192 | int ret = checkExternalFile(); 193 | String title = null; 194 | String message = null; 195 | 196 | Log.d(TAG, "ret: " + ret); 197 | 198 | switch (ret) { 199 | case ERROR_SDCARD_UNMOUNTED_OR_DENIED: 200 | title = getString(R.string.check_sdcard_unmounted_or_denied_title); 201 | message = getString(R.string.check_sdcard_unmounted_or_denied_message); 202 | break; 203 | case ERROR_FILE_CREATES_FAILED: 204 | title = getString(R.string.check_sdcard_file_create_error_title); 205 | message = getString(R.string.check_sdcard_file_create_error_message); 206 | break; 207 | } 208 | 209 | if (ret != EXTERNAL_CHECK_OK) { 210 | AlertDialog.Builder builder = new AlertDialog.Builder(this) 211 | .setTitle(title) 212 | .setMessage(message) 213 | .setIcon(android.R.drawable.ic_dialog_alert) 214 | .setCancelable(false) 215 | .setNegativeButton("ok", new DialogInterface.OnClickListener() { 216 | @Override 217 | public void onClick(DialogInterface dialog, int which) { 218 | dialog.dismiss(); 219 | MainActivity.this.finish(); 220 | } 221 | }); 222 | builder.create().show(); 223 | } 224 | } 225 | 226 | public File getAlbumStorageDir(String albumName) { 227 | // Get the directory for the user's public pictures directory. 228 | File file = new File(Environment.getExternalStoragePublicDirectory( 229 | Environment.DIRECTORY_PICTURES), albumName); 230 | if (!file.mkdirs()) { 231 | Log.e(TAG, "Directory not created"); 232 | } 233 | return file; 234 | } 235 | 236 | public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException { 237 | Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 238 | Canvas canvas = new Canvas(newBitmap); 239 | canvas.drawColor(Color.WHITE); 240 | canvas.drawBitmap(bitmap, 0, 0, null); 241 | OutputStream stream = new FileOutputStream(photo); 242 | newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream); 243 | stream.close(); 244 | } 245 | 246 | public boolean addJpgSignatureToGallery(Bitmap signature) { 247 | boolean result = false; 248 | try { 249 | File photo = new File(getAlbumStorageDir("SignaturePad"), 250 | mCurrentUserID + "_" + (mCurrentSampleID - 1) + ".jpg"); 251 | saveBitmapToJPG(signature, photo); 252 | scanMediaFile(photo); 253 | result = true; 254 | } catch (IOException e) { 255 | e.printStackTrace(); 256 | } 257 | return result; 258 | } 259 | 260 | private void scanMediaFile(File photo) { 261 | Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 262 | Uri contentUri = Uri.fromFile(photo); 263 | mediaScanIntent.setData(contentUri); 264 | MainActivity.this.sendBroadcast(mediaScanIntent); 265 | } 266 | 267 | public boolean addSvgSignatureToGallery(String signatureSvg) { 268 | boolean result = false; 269 | try { 270 | File svgFile = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.svg", System.currentTimeMillis())); 271 | OutputStream stream = new FileOutputStream(svgFile); 272 | OutputStreamWriter writer = new OutputStreamWriter(stream); 273 | writer.write(signatureSvg); 274 | writer.close(); 275 | stream.flush(); 276 | stream.close(); 277 | scanMediaFile(svgFile); 278 | result = true; 279 | } catch (IOException e) { 280 | e.printStackTrace(); 281 | } 282 | return result; 283 | } 284 | 285 | public void onOptionClicked(View view) { 286 | 287 | if (popupMenu == null) { 288 | popupMenu = new PopupMenu(this, view); 289 | getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu()); 290 | popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 291 | @Override 292 | public boolean onMenuItemClick(final MenuItem item) { 293 | 294 | switch (item.getItemId()) { 295 | case R.id.new_user: 296 | final View v = getLayoutInflater().inflate(R.layout.option_item_dialog, null); 297 | AlertDialog.Builder newUserBuilder = new AlertDialog.Builder(MainActivity.this); 298 | newUserBuilder.setIcon(android.R.drawable.ic_dialog_info); 299 | newUserBuilder.setView(v); 300 | newUserBuilder.setTitle(R.string.new_user_dialog_title); 301 | newUserBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { 302 | @Override 303 | public void onClick(DialogInterface dialog, int which) { 304 | EditText editText = (EditText) v.findViewById(R.id.username); 305 | String username = editText.getText().toString(); 306 | if (username != null && username.length() != 0) { 307 | 308 | // save this user to database. 309 | mDatabase.insertData(mNextUserId, username, 0); 310 | 311 | // save current username. 312 | mCurrentUsername = username; 313 | mUserEditor.putString(CURRENT_USERNAME_KEY, mCurrentUsername); 314 | 315 | // save current user id. 316 | mCurrentUserID = mNextUserId; 317 | mUserEditor.putInt(CURRENT_USER_ID_KEY, mCurrentUserID); 318 | 319 | // current sample id is zero for new user. 320 | mCurrentSampleID = 0; 321 | mUserEditor.putInt(CURRENT_USER_SAMPLEID_KEY, mCurrentSampleID); 322 | 323 | // increase next user id and save id. 324 | mNextUserId++; 325 | mUserEditor.putInt(NEXT_USER_ID_KEY, mNextUserId); 326 | 327 | // commit to persist. 328 | mUserEditor.commit(); 329 | 330 | // update Description 331 | mSignaturepadDescription.setText(getString(R.string.hint_info) + 332 | mCurrentUsername + ":" + mCurrentUserID + ":" + mCurrentSampleID); 333 | } else { 334 | Toast.makeText(MainActivity.this, "亲,用户名不能为空哦!", Toast.LENGTH_SHORT).show(); 335 | } 336 | 337 | dialog.dismiss(); 338 | 339 | fullScreenDisplay(); 340 | } 341 | }); 342 | newUserBuilder.create().show(); 343 | break; 344 | case R.id.switch_user: 345 | AlertDialog.Builder switchUserbuilder = new AlertDialog.Builder(MainActivity.this); 346 | switchUserbuilder.setTitle(R.string.option_switch_user_dialog_title); 347 | switchUserbuilder.setIcon(android.R.drawable.ic_dialog_info); 348 | switchUserbuilder.setCancelable(false); 349 | final Cursor cursor = mDatabase.searchUserByID(0); 350 | switchUserbuilder.setSingleChoiceItems(new SimpleCursorAdapter(MainActivity.this, 351 | R.layout.userid_name, 352 | cursor, 353 | new String[]{"userid", "username"}, 354 | new int[]{R.id.userid, R.id.username}, 355 | CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER), -1, new DialogInterface.OnClickListener() { 356 | @Override 357 | public void onClick(DialogInterface dialog, int which) { 358 | // move to target. 359 | cursor.moveToPosition(which); 360 | 361 | // save current user id. 362 | mCurrentUserID = cursor.getInt(1); 363 | mUserEditor.putInt(CURRENT_USER_ID_KEY, mCurrentUserID); 364 | 365 | // save current user name. 366 | mCurrentUsername = cursor.getString(2); 367 | mUserEditor.putString(CURRENT_USERNAME_KEY, mCurrentUsername); 368 | 369 | // save current user sample id. 370 | mCurrentSampleID = cursor.getInt(3); 371 | mUserEditor.putInt(CURRENT_USER_SAMPLEID_KEY, mCurrentSampleID); 372 | 373 | // commit to persist. 374 | mUserEditor.commit(); 375 | 376 | // update Description 377 | mSignaturepadDescription.setText(getString(R.string.hint_info) + 378 | mCurrentUsername + ":" + mCurrentUserID + ":" + mCurrentSampleID); 379 | 380 | dialog.dismiss(); 381 | 382 | fullScreenDisplay(); 383 | } 384 | }); 385 | switchUserbuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 386 | @Override 387 | public void onClick(DialogInterface dialog, int which) { 388 | dialog.dismiss(); 389 | fullScreenDisplay(); 390 | } 391 | }); 392 | switchUserbuilder.create().show(); 393 | break; 394 | default: 395 | setPaintColor(item); 396 | break; 397 | } 398 | 399 | return true; 400 | } 401 | }); 402 | } 403 | 404 | // Reflect to invoke setForceShowIcon function to show menu icon. 405 | // we may get IllegalAccessException: access to field not allowed here, 406 | // but it's ok, we just catch it and ignore it. 407 | try { 408 | Field[] fields = popupMenu.getClass().getDeclaredFields(); 409 | for (Field field : fields) { 410 | if ("mPopup".equals(field.getName())) { 411 | field.setAccessible(true); 412 | Object menuPopupHelper = field.get(popupMenu); 413 | Class classPopupHelper = Class.forName(menuPopupHelper 414 | .getClass().getName()); 415 | Method setForceIcons = classPopupHelper.getMethod( 416 | "setForceShowIcon", boolean.class); 417 | setForceIcons.invoke(menuPopupHelper, true); 418 | break; 419 | } 420 | } 421 | } catch (Exception e) { 422 | e.printStackTrace(); 423 | } 424 | 425 | popupMenu.show(); 426 | } 427 | 428 | private void setPaintColor(MenuItem item) { 429 | item.setChecked(true); 430 | switch (item.getItemId()) { 431 | case R.id.paint_color_black: 432 | mSignaturePad.setPaintColor(Color.BLACK); 433 | break; 434 | case R.id.paint_color_blue: 435 | mSignaturePad.setPaintColor(Color.BLUE); 436 | break; 437 | case R.id.paint_color_cyan: 438 | mSignaturePad.setPaintColor(Color.CYAN); 439 | break; 440 | case R.id.paint_color_dkgray: 441 | mSignaturePad.setPaintColor(Color.DKGRAY); 442 | break; 443 | case R.id.paint_color_gray: 444 | mSignaturePad.setPaintColor(Color.GRAY); 445 | break; 446 | case R.id.paint_color_green: 447 | mSignaturePad.setPaintColor(Color.GREEN); 448 | break; 449 | case R.id.paint_color_ltgray: 450 | mSignaturePad.setPaintColor(Color.LTGRAY); 451 | break; 452 | case R.id.paint_color_magenta: 453 | mSignaturePad.setPaintColor(Color.MAGENTA); 454 | break; 455 | case R.id.paint_color_red: 456 | mSignaturePad.setPaintColor(Color.RED); 457 | break; 458 | case R.id.paint_color_yellow: 459 | mSignaturePad.setPaintColor(Color.YELLOW); 460 | break; 461 | } 462 | fullScreenDisplay(); 463 | } 464 | 465 | private int checkExternalFile() { 466 | 467 | if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 468 | return ERROR_SDCARD_UNMOUNTED_OR_DENIED; 469 | } 470 | 471 | File sdcardDir = Environment.getExternalStorageDirectory(); 472 | 473 | try { 474 | File records = new File(sdcardDir.getCanonicalPath(), RECORDS_FILE_NAME); 475 | boolean isFileOk; 476 | if (!records.exists()) { 477 | isFileOk = records.createNewFile(); 478 | } else { 479 | isFileOk = true; 480 | } 481 | 482 | if (isFileOk) { 483 | mRecordsFileOutStream = new FileOutputStream(records, true); 484 | } else { 485 | return ERROR_FILE_CREATES_FAILED; 486 | } 487 | } catch (IOException e) { 488 | e.printStackTrace(); 489 | return ERROR_FILE_CREATES_FAILED; 490 | } 491 | 492 | return EXTERNAL_CHECK_OK; 493 | } 494 | 495 | private void fullScreenDisplay (){ 496 | if (mSignaturePad != null) { 497 | // full screen setting, make our sign UI fullscreen. 498 | mSignaturePad.setSystemUiVisibility( 499 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE 500 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 501 | | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 502 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 503 | | View.SYSTEM_UI_FLAG_FULLSCREEN 504 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 505 | } 506 | } 507 | 508 | private class WriteRecordsThread extends Thread { 509 | 510 | private SparseArray records = null; 511 | private int isTrue = 0; 512 | 513 | public WriteRecordsThread(SparseArray records, int isTrue) { 514 | this.records = records; 515 | this.isTrue = isTrue; 516 | } 517 | 518 | @Override 519 | public void run() { 520 | super.run(); 521 | 522 | Looper.prepare(); 523 | 524 | if (records == null || records.size() == 0) { 525 | return; 526 | } 527 | 528 | try { 529 | // write tags 530 | String tag = SIGNATUREID_TAG + " " + mCurrentUserID + "_" + mCurrentSampleID + "\n"; 531 | mRecordsFileOutStream.write(tag.getBytes()); 532 | tag = SIGNATURELABLE_TAG + " " + isTrue + "\n"; 533 | mRecordsFileOutStream.write(tag.getBytes()); 534 | 535 | 536 | for (int i = 0; i < records.size(); i++) { 537 | SignaturePad.MotionEventRecorder recorder = records.get(i, null); 538 | if (recorder != null) { 539 | // save to record file. 540 | mRecordsFileOutStream.write(recorder.toString().getBytes()); 541 | mRecordsFileOutStream.write('\n'); 542 | } 543 | } 544 | 545 | // add a new line 546 | mRecordsFileOutStream.write('\n'); 547 | 548 | // add sample id 549 | mCurrentSampleID++; 550 | mUserEditor.putInt(CURRENT_USER_SAMPLEID_KEY, mCurrentSampleID); 551 | mUserEditor.commit(); 552 | 553 | // update sample id info of description. 554 | mWriteEventHandler.obtainMessage(MSG_WRITE_COMPLETE).sendToTarget(); 555 | 556 | // save a picture of this. 557 | Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap(); 558 | if(addJpgSignatureToGallery(signatureBitmap)) { 559 | Toast.makeText(MainActivity.this, "Signature saved complete!", Toast.LENGTH_SHORT).show(); 560 | } else { 561 | Toast.makeText(MainActivity.this, "Unable to store the signature", Toast.LENGTH_SHORT).show(); 562 | } 563 | } catch (IOException e) { 564 | e.printStackTrace(); 565 | } 566 | 567 | // clear records anyway. 568 | records.clear(); 569 | 570 | Looper.loop(); 571 | } 572 | } 573 | } 574 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/MyDatabase.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.Cursor; 6 | 7 | /** 8 | * Local database class, access data via helper class. 9 | */ 10 | public class MyDatabase { 11 | private final String TAG = "MyDatabase"; 12 | 13 | final String DATABASE_NAME = "handwriter.db3"; 14 | MyDatabaseHelper databaseHelper = null; 15 | 16 | private final String TABLE_NAME = "userlist"; 17 | final String TABLE_USER_ID_COLUMN_NAME = "userid"; 18 | final String TABLE_USERNAME_COLUMN_NAME = "username"; 19 | final String TABLE_SAMPLE_ID_COLUMN_NAME = "sampleid"; 20 | 21 | public MyDatabase(Context context) { 22 | databaseHelper = new MyDatabaseHelper(context, DATABASE_NAME, 1); 23 | } 24 | 25 | public void insertData(int userid, String username, int sampleid) { 26 | ContentValues values = new ContentValues(); 27 | values.put(TABLE_USER_ID_COLUMN_NAME, userid); 28 | values.put(TABLE_USERNAME_COLUMN_NAME, username); 29 | values.put(TABLE_SAMPLE_ID_COLUMN_NAME, sampleid); 30 | databaseHelper.getReadableDatabase().insert(TABLE_NAME, null, values); 31 | } 32 | 33 | public Cursor searchUserByID(int userid) { 34 | Cursor cursor = null; 35 | 36 | // if user id > 0, means search for one user 37 | // if user id == 0, means search for all the users. 38 | if (userid > 0) { 39 | cursor = databaseHelper.getReadableDatabase().rawQuery( 40 | "select * from " + TABLE_NAME + " where userid == " + userid, 41 | null); 42 | } else if (userid == 0) { 43 | cursor = databaseHelper.getReadableDatabase().rawQuery( 44 | "select * from " + TABLE_NAME, 45 | null); 46 | } 47 | 48 | return cursor; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/MyDatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | import android.util.Log; 7 | 8 | /** 9 | * Database helper class. 10 | */ 11 | public class MyDatabaseHelper extends SQLiteOpenHelper { 12 | 13 | private final String TAG = "MyDatabaseHelper"; 14 | 15 | final String CREATE_USERLIST_TABLE_SQL = "create table userlist(_id integer primary " + 16 | "key autoincrement , userid , username, sampleid)"; 17 | 18 | public MyDatabaseHelper(Context context, String name, int version) { 19 | super(context, name, null, version); 20 | } 21 | 22 | @Override 23 | public void onCreate(SQLiteDatabase db) { 24 | db.execSQL(CREATE_USERLIST_TABLE_SQL); 25 | } 26 | 27 | @Override 28 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 29 | Log.d(TAG, "update database from old version: " + 30 | oldVersion + " to new version: " + newVersion); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/Bezier.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | public class Bezier { 4 | 5 | public TimedPoint startPoint; 6 | public TimedPoint control1; 7 | public TimedPoint control2; 8 | public TimedPoint endPoint; 9 | 10 | public Bezier set(TimedPoint startPoint, TimedPoint control1, 11 | TimedPoint control2, TimedPoint endPoint) { 12 | this.startPoint = startPoint; 13 | this.control1 = control1; 14 | this.control2 = control2; 15 | this.endPoint = endPoint; 16 | return this; 17 | } 18 | 19 | public float length() { 20 | int steps = 10; 21 | float length = 0; 22 | double cx, cy, px = 0, py = 0, xDiff, yDiff; 23 | 24 | for (int i = 0; i <= steps; i++) { 25 | float t = (float) i / steps; 26 | cx = point(t, this.startPoint.x, this.control1.x, 27 | this.control2.x, this.endPoint.x); 28 | cy = point(t, this.startPoint.y, this.control1.y, 29 | this.control2.y, this.endPoint.y); 30 | if (i > 0) { 31 | xDiff = cx - px; 32 | yDiff = cy - py; 33 | length += Math.sqrt(xDiff * xDiff + yDiff * yDiff); 34 | } 35 | px = cx; 36 | py = cy; 37 | } 38 | return length; 39 | 40 | } 41 | 42 | public double point(float t, float start, float c1, float c2, float end) { 43 | return start * (1.0 - t) * (1.0 - t) * (1.0 - t) 44 | + 3.0 * c1 * (1.0 - t) * (1.0 - t) * t 45 | + 3.0 * c2 * (1.0 - t) * t * t 46 | + end * t * t * t; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/ControlTimedPoints.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | /** 4 | * Created by gcacace on 28/02/14. 5 | */ 6 | public class ControlTimedPoints { 7 | 8 | public TimedPoint c1; 9 | public TimedPoint c2; 10 | 11 | public ControlTimedPoints set(TimedPoint c1, TimedPoint c2) { 12 | this.c1 = c1; 13 | this.c2 = c2; 14 | return this; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/SvgBuilder.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | public class SvgBuilder { 4 | 5 | private final StringBuilder mSvgPathsBuilder = new StringBuilder(); 6 | private SvgPathBuilder mCurrentPathBuilder = null; 7 | 8 | public SvgBuilder() { 9 | } 10 | 11 | public void clear() { 12 | mSvgPathsBuilder.setLength(0); 13 | mCurrentPathBuilder = null; 14 | } 15 | 16 | public String build(final int width, final int height) { 17 | if (isPathStarted()) { 18 | appendCurrentPath(); 19 | } 20 | return (new StringBuilder()) 21 | .append("\n") 22 | .append("") 29 | .append("") 35 | .append(mSvgPathsBuilder) 36 | .append("") 37 | .append("") 38 | .toString(); 39 | } 40 | 41 | public SvgBuilder append(final Bezier curve, final float strokeWidth) { 42 | final Integer roundedStrokeWidth = Math.round(strokeWidth); 43 | final SvgPoint curveStartSvgPoint = new SvgPoint(curve.startPoint); 44 | final SvgPoint curveControlSvgPoint1 = new SvgPoint(curve.control1); 45 | final SvgPoint curveControlSvgPoint2 = new SvgPoint(curve.control2); 46 | final SvgPoint curveEndSvgPoint = new SvgPoint(curve.endPoint); 47 | 48 | if (!isPathStarted()) { 49 | startNewPath(roundedStrokeWidth, curveStartSvgPoint); 50 | } 51 | 52 | if (!curveStartSvgPoint.equals(mCurrentPathBuilder.getLastPoint()) 53 | || !roundedStrokeWidth.equals(mCurrentPathBuilder.getStrokeWidth())) { 54 | appendCurrentPath(); 55 | startNewPath(roundedStrokeWidth, curveStartSvgPoint); 56 | } 57 | 58 | mCurrentPathBuilder.append(curveControlSvgPoint1, curveControlSvgPoint2, curveEndSvgPoint); 59 | return this; 60 | } 61 | 62 | private void startNewPath(Integer roundedStrokeWidth, SvgPoint curveStartSvgPoint) { 63 | mCurrentPathBuilder = new SvgPathBuilder(curveStartSvgPoint, roundedStrokeWidth); 64 | } 65 | 66 | private void appendCurrentPath() { 67 | mSvgPathsBuilder.append(mCurrentPathBuilder); 68 | } 69 | 70 | private boolean isPathStarted() { 71 | return mCurrentPathBuilder != null; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/SvgPathBuilder.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | 4 | /** 5 | * Build a SVG path as a string. 6 | * 7 | * https://www.w3.org/TR/SVGTiny12/paths.html 8 | */ 9 | public class SvgPathBuilder { 10 | 11 | public static final Character SVG_RELATIVE_CUBIC_BEZIER_CURVE = 'c'; 12 | public static final Character SVG_MOVE = 'M'; 13 | private final StringBuilder mStringBuilder; 14 | private final Integer mStrokeWidth; 15 | private final SvgPoint mStartPoint; 16 | private SvgPoint mLastPoint; 17 | private Character mLastSvgCommand; 18 | 19 | public SvgPathBuilder(final SvgPoint startPoint, final Integer strokeWidth) { 20 | mStrokeWidth = strokeWidth; 21 | mStartPoint = startPoint; 22 | mLastPoint = startPoint; 23 | mLastSvgCommand = null; 24 | mStringBuilder = new StringBuilder(); 25 | } 26 | 27 | public final Integer getStrokeWidth() { 28 | return mStrokeWidth; 29 | } 30 | 31 | public final SvgPoint getLastPoint() { 32 | return mLastPoint; 33 | } 34 | 35 | public SvgPathBuilder append(final SvgPoint controlPoint1, final SvgPoint controlPoint2, final SvgPoint endPoint) { 36 | mStringBuilder.append(makeRelativeCubicBezierCurve(controlPoint1, controlPoint2, endPoint)); 37 | mLastSvgCommand = SVG_RELATIVE_CUBIC_BEZIER_CURVE; 38 | mLastPoint = endPoint; 39 | return this; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return (new StringBuilder()) 45 | .append("") 54 | .toString(); 55 | } 56 | 57 | private String makeRelativeCubicBezierCurve(final SvgPoint controlPoint1, final SvgPoint controlPoint2, final SvgPoint endPoint) { 58 | final String sControlPoint1 = controlPoint1.toRelativeCoordinates(mLastPoint); 59 | final String sControlPoint2 = controlPoint2.toRelativeCoordinates(mLastPoint); 60 | final String sEndPoint = endPoint.toRelativeCoordinates(mLastPoint); 61 | 62 | final StringBuilder sb = new StringBuilder(); 63 | if (!SVG_RELATIVE_CUBIC_BEZIER_CURVE.equals(mLastSvgCommand)) { 64 | sb.append(SVG_RELATIVE_CUBIC_BEZIER_CURVE); 65 | sb.append(sControlPoint1); 66 | } else { 67 | if (!sControlPoint1.startsWith("-")) { 68 | sb.append(" "); 69 | } 70 | sb.append(sControlPoint1); 71 | } 72 | 73 | if (!sControlPoint2.startsWith("-")) { 74 | sb.append(" "); 75 | } 76 | sb.append(sControlPoint2); 77 | 78 | if (!sEndPoint.startsWith("-")) { 79 | sb.append(" "); 80 | } 81 | sb.append(sEndPoint); 82 | 83 | // discard zero curve 84 | final String svg = sb.toString(); 85 | if ("c0 0 0 0 0 0".equals(svg)) { 86 | return ""; 87 | } else { 88 | return svg; 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/SvgPoint.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | /** 4 | * Represent a point as it would be in the generated SVG document. 5 | */ 6 | class SvgPoint { 7 | 8 | final Integer x, y; 9 | 10 | public SvgPoint(TimedPoint point) { 11 | // one optimisation is to get rid of decimals as they are mostly non-significant in the 12 | // produced SVG image 13 | x = Math.round(point.x); 14 | y = Math.round(point.y); 15 | } 16 | 17 | public SvgPoint(int x, int y) { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | 22 | public String toAbsoluteCoordinates() { 23 | StringBuilder stringBuilder = new StringBuilder(); 24 | stringBuilder.append(x); 25 | if (y >= 0) { 26 | stringBuilder.append(" "); 27 | } 28 | stringBuilder.append(y); 29 | return stringBuilder.toString(); 30 | } 31 | 32 | public String toRelativeCoordinates(final SvgPoint referencePoint) { 33 | return (new SvgPoint(x - referencePoint.x, y - referencePoint.y)).toString(); 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return toAbsoluteCoordinates(); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (o == null || getClass() != o.getClass()) return false; 45 | 46 | SvgPoint svgPoint = (SvgPoint) o; 47 | 48 | if (!x.equals(svgPoint.x)) return false; 49 | return y.equals(svgPoint.y); 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = x.hashCode(); 56 | result = 31 * result + y.hashCode(); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/utils/TimedPoint.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.utils; 2 | 3 | public class TimedPoint { 4 | public float x; 5 | public float y; 6 | public long timestamp; 7 | 8 | public TimedPoint set(float x, float y) { 9 | this.x = x; 10 | this.y = y; 11 | this.timestamp = System.currentTimeMillis(); 12 | return this; 13 | } 14 | 15 | public float velocityFrom(TimedPoint start) { 16 | float velocity = distanceTo(start) / (this.timestamp - start.timestamp); 17 | if (velocity != velocity) return 0f; 18 | return velocity; 19 | } 20 | 21 | public float distanceTo(TimedPoint point) { 22 | return (float) Math.sqrt(Math.pow(point.x - this.x, 2) + Math.pow(point.y - this.y, 2)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/view/ViewCompat.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.view; 2 | 3 | import android.os.Build; 4 | import android.view.View; 5 | 6 | public class ViewCompat { 7 | /** 8 | * Returns true if {@code view} has been through at least one layout since it 9 | * was last attached to or detached from a window. 10 | * 11 | * See http://developer.android.com/reference/android/support/v4/view/ViewCompat.html#isLaidOut%28android.view.View%29 12 | * 13 | * @param view the view 14 | * @return true if this view has been through at least one layout since it was last attached to or detached from a window. 15 | */ 16 | public static boolean isLaidOut(View view) { 17 | // Future (API19+)... 18 | if (Build.VERSION.SDK_INT >= 19) { 19 | return view.isLaidOut(); 20 | } 21 | // Legacy... 22 | return view.getWidth() > 0 && view.getHeight() > 0; 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/view/ViewTreeObserverCompat.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.view; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.os.Build; 5 | import android.view.ViewTreeObserver; 6 | 7 | public class ViewTreeObserverCompat { 8 | /** 9 | * Remove a previously installed global layout callback. 10 | * @param observer the view observer 11 | * @param victim the victim 12 | */ 13 | @SuppressLint("NewApi") 14 | @SuppressWarnings("deprecation") 15 | public static void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener victim) { 16 | // Future (API16+)... 17 | if (Build.VERSION.SDK_INT >= 16) { 18 | observer.removeOnGlobalLayoutListener(victim); 19 | } 20 | // Legacy... 21 | else { 22 | observer.removeGlobalOnLayoutListener(victim); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ac/iscas/handwriter/views/SignaturePad.java: -------------------------------------------------------------------------------- 1 | package cn.ac.iscas.handwriter.views; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.content.res.TypedArray; 6 | import android.graphics.Bitmap; 7 | import android.graphics.Canvas; 8 | import android.graphics.Color; 9 | import android.graphics.Matrix; 10 | import android.graphics.Paint; 11 | import android.graphics.RectF; 12 | import android.util.AttributeSet; 13 | import android.util.SparseArray; 14 | import android.view.MotionEvent; 15 | import android.view.View; 16 | import android.view.ViewTreeObserver; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import cn.ac.iscas.handwriter.R; 22 | import cn.ac.iscas.handwriter.utils.Bezier; 23 | import cn.ac.iscas.handwriter.utils.ControlTimedPoints; 24 | import cn.ac.iscas.handwriter.utils.SvgBuilder; 25 | import cn.ac.iscas.handwriter.utils.TimedPoint; 26 | import cn.ac.iscas.handwriter.view.ViewCompat; 27 | import cn.ac.iscas.handwriter.view.ViewTreeObserverCompat; 28 | 29 | public class SignaturePad extends View { 30 | //View state 31 | private List mPoints; 32 | private boolean mIsEmpty; 33 | private float mLastTouchX; 34 | private float mLastTouchY; 35 | private float mLastVelocity; 36 | private float mLastWidth; 37 | private RectF mDirtyRect; 38 | 39 | private final SvgBuilder mSvgBuilder = new SvgBuilder(); 40 | 41 | // Cache 42 | private List mPointsCache = new ArrayList<>(); 43 | private ControlTimedPoints mControlTimedPointsCached = new ControlTimedPoints(); 44 | private Bezier mBezierCached = new Bezier(); 45 | 46 | // MotionEvent record set. 47 | private SparseArray mMotionEventRecord = new SparseArray<>(); 48 | // record index. 49 | private int mMotionEventRecordIndex = 0; 50 | 51 | //Configurable parameters 52 | private int mMinWidth; 53 | private int mMaxWidth; 54 | private float mVelocityFilterWeight; 55 | private OnSignedListener mOnSignedListener; 56 | private boolean mClearOnDoubleClick; 57 | 58 | //Click values 59 | private long mFirstClick; 60 | private int mCountClick; 61 | private static final int DOUBLE_CLICK_DELAY_MS = 200; 62 | 63 | //Default attribute values 64 | private final int DEFAULT_ATTR_PEN_MIN_WIDTH_PX = 3; 65 | private final int DEFAULT_ATTR_PEN_MAX_WIDTH_PX = 7; 66 | private final int DEFAULT_ATTR_PEN_COLOR = Color.BLACK; 67 | private final float DEFAULT_ATTR_VELOCITY_FILTER_WEIGHT = 0.9f; 68 | private final boolean DEFAULT_ATTR_CLEAR_ON_DOUBLE_CLICK = false; 69 | 70 | private Paint mPaint = new Paint(); 71 | private Bitmap mSignatureBitmap = null; 72 | private Canvas mSignatureBitmapCanvas = null; 73 | 74 | public SignaturePad(Context context, AttributeSet attrs) { 75 | super(context, attrs); 76 | 77 | TypedArray a = context.getTheme().obtainStyledAttributes( 78 | attrs, 79 | R.styleable.SignaturePad, 80 | 0, 0); 81 | 82 | //Configurable parameters 83 | try { 84 | mMinWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMinWidth, convertDpToPx(DEFAULT_ATTR_PEN_MIN_WIDTH_PX)); 85 | mMaxWidth = a.getDimensionPixelSize(R.styleable.SignaturePad_penMaxWidth, convertDpToPx(DEFAULT_ATTR_PEN_MAX_WIDTH_PX)); 86 | mPaint.setColor(a.getColor(R.styleable.SignaturePad_penColor, DEFAULT_ATTR_PEN_COLOR)); 87 | mVelocityFilterWeight = a.getFloat(R.styleable.SignaturePad_velocityFilterWeight, DEFAULT_ATTR_VELOCITY_FILTER_WEIGHT); 88 | mClearOnDoubleClick = a.getBoolean(R.styleable.SignaturePad_clearOnDoubleClick, DEFAULT_ATTR_CLEAR_ON_DOUBLE_CLICK); 89 | } finally { 90 | a.recycle(); 91 | } 92 | 93 | //Fixed parameters 94 | mPaint.setAntiAlias(true); 95 | mPaint.setStyle(Paint.Style.STROKE); 96 | mPaint.setStrokeCap(Paint.Cap.ROUND); 97 | mPaint.setStrokeJoin(Paint.Join.ROUND); 98 | 99 | //Dirty rectangle to update only the changed portion of the view 100 | mDirtyRect = new RectF(); 101 | 102 | clear(); 103 | } 104 | 105 | /** 106 | * Set the pen color from a given resource. 107 | * If the resource is not found, {@link android.graphics.Color#BLACK} is assumed. 108 | * 109 | * @param colorRes the color resource. 110 | */ 111 | public void setPenColorRes(int colorRes) { 112 | try { 113 | setPenColor(getResources().getColor(colorRes)); 114 | } catch (Resources.NotFoundException ex) { 115 | setPenColor(Color.parseColor("#000000")); 116 | } 117 | } 118 | 119 | /** 120 | * Set the pen color from a given color. 121 | * 122 | * @param color the color. 123 | */ 124 | public void setPenColor(int color) { 125 | mPaint.setColor(color); 126 | } 127 | 128 | /** 129 | * Set the minimum width of the stroke in pixel. 130 | * 131 | * @param minWidth the width in dp. 132 | */ 133 | public void setMinWidth(float minWidth) { 134 | mMinWidth = convertDpToPx(minWidth); 135 | } 136 | 137 | /** 138 | * Set the maximum width of the stroke in pixel. 139 | * 140 | * @param maxWidth the width in dp. 141 | */ 142 | public void setMaxWidth(float maxWidth) { 143 | mMaxWidth = convertDpToPx(maxWidth); 144 | } 145 | 146 | /** 147 | * Set the velocity filter weight. 148 | * 149 | * @param velocityFilterWeight the weight. 150 | */ 151 | public void setVelocityFilterWeight(float velocityFilterWeight) { 152 | mVelocityFilterWeight = velocityFilterWeight; 153 | } 154 | 155 | public void clear() { 156 | mSvgBuilder.clear(); 157 | mPoints = new ArrayList<>(); 158 | mLastVelocity = 0; 159 | mLastWidth = (mMinWidth + mMaxWidth) / 2; 160 | 161 | if (mSignatureBitmap != null) { 162 | mSignatureBitmap = null; 163 | ensureSignatureBitmap(); 164 | } 165 | 166 | setIsEmpty(true); 167 | 168 | invalidate(); 169 | } 170 | 171 | @Override 172 | public boolean onTouchEvent(MotionEvent event) { 173 | if (!isEnabled()) 174 | return false; 175 | 176 | float eventX = event.getX(); 177 | float eventY = event.getY(); 178 | 179 | switch (event.getAction()) { 180 | case MotionEvent.ACTION_DOWN: 181 | getParent().requestDisallowInterceptTouchEvent(true); 182 | mPoints.clear(); 183 | if (isDoubleClick()) break; 184 | mLastTouchX = eventX; 185 | mLastTouchY = eventY; 186 | addPoint(getNewPoint(eventX, eventY)); 187 | if(mOnSignedListener != null) mOnSignedListener.onStartSigning(); 188 | 189 | case MotionEvent.ACTION_MOVE: 190 | resetDirtyRect(eventX, eventY); 191 | addPoint(getNewPoint(eventX, eventY)); 192 | // add motion event record. 193 | mMotionEventRecord.put(mMotionEventRecordIndex++, 194 | new MotionEventRecorder(event.getEventTime(), 195 | event.getX(), 196 | event.getY(), 197 | event.getPressure())); 198 | break; 199 | 200 | case MotionEvent.ACTION_UP: 201 | resetDirtyRect(eventX, eventY); 202 | addPoint(getNewPoint(eventX, eventY)); 203 | getParent().requestDisallowInterceptTouchEvent(true); 204 | setIsEmpty(false); 205 | break; 206 | 207 | default: 208 | return false; 209 | } 210 | 211 | //invalidate(); 212 | invalidate( 213 | (int) (mDirtyRect.left - mMaxWidth), 214 | (int) (mDirtyRect.top - mMaxWidth), 215 | (int) (mDirtyRect.right + mMaxWidth), 216 | (int) (mDirtyRect.bottom + mMaxWidth)); 217 | 218 | return true; 219 | } 220 | 221 | @Override 222 | protected void onDraw(Canvas canvas) { 223 | if (mSignatureBitmap != null) { 224 | canvas.drawBitmap(mSignatureBitmap, 0, 0, mPaint); 225 | } 226 | } 227 | 228 | public void setOnSignedListener(OnSignedListener listener) { 229 | mOnSignedListener = listener; 230 | } 231 | 232 | public boolean isEmpty() { 233 | return mIsEmpty; 234 | } 235 | 236 | public String getSignatureSvg() { 237 | int width = getTransparentSignatureBitmap().getWidth(); 238 | int height = getTransparentSignatureBitmap().getHeight(); 239 | return mSvgBuilder.build(width, height); 240 | } 241 | 242 | public Bitmap getSignatureBitmap() { 243 | Bitmap originalBitmap = getTransparentSignatureBitmap(); 244 | Bitmap whiteBgBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888); 245 | Canvas canvas = new Canvas(whiteBgBitmap); 246 | canvas.drawColor(Color.WHITE); 247 | canvas.drawBitmap(originalBitmap, 0, 0, null); 248 | return whiteBgBitmap; 249 | } 250 | 251 | public void setSignatureBitmap(final Bitmap signature) { 252 | // View was laid out... 253 | if (ViewCompat.isLaidOut(this)) { 254 | clear(); 255 | ensureSignatureBitmap(); 256 | 257 | RectF tempSrc = new RectF(); 258 | RectF tempDst = new RectF(); 259 | 260 | int dWidth = signature.getWidth(); 261 | int dHeight = signature.getHeight(); 262 | int vWidth = getWidth(); 263 | int vHeight = getHeight(); 264 | 265 | // Generate the required transform. 266 | tempSrc.set(0, 0, dWidth, dHeight); 267 | tempDst.set(0, 0, vWidth, vHeight); 268 | 269 | Matrix drawMatrix = new Matrix(); 270 | drawMatrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.CENTER); 271 | 272 | Canvas canvas = new Canvas(mSignatureBitmap); 273 | canvas.drawBitmap(signature, drawMatrix, null); 274 | setIsEmpty(false); 275 | invalidate(); 276 | } 277 | // View not laid out yet e.g. called from onCreate(), onRestoreInstanceState()... 278 | else { 279 | getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 280 | @Override 281 | public void onGlobalLayout() { 282 | // Remove layout listener... 283 | ViewTreeObserverCompat.removeOnGlobalLayoutListener(getViewTreeObserver(), this); 284 | 285 | // Signature bitmap... 286 | setSignatureBitmap(signature); 287 | } 288 | }); 289 | } 290 | } 291 | 292 | public Bitmap getTransparentSignatureBitmap() { 293 | ensureSignatureBitmap(); 294 | return mSignatureBitmap; 295 | } 296 | 297 | public void setPaintColor(int color) { 298 | mPaint.setColor(color); 299 | } 300 | 301 | public Bitmap getTransparentSignatureBitmap(boolean trimBlankSpace) { 302 | 303 | if (!trimBlankSpace) { 304 | return getTransparentSignatureBitmap(); 305 | } 306 | 307 | ensureSignatureBitmap(); 308 | 309 | int imgHeight = mSignatureBitmap.getHeight(); 310 | int imgWidth = mSignatureBitmap.getWidth(); 311 | 312 | int backgroundColor = Color.TRANSPARENT; 313 | 314 | int xMin = Integer.MAX_VALUE, 315 | xMax = Integer.MIN_VALUE, 316 | yMin = Integer.MAX_VALUE, 317 | yMax = Integer.MIN_VALUE; 318 | 319 | boolean foundPixel = false; 320 | 321 | // Find xMin 322 | for (int x = 0; x < imgWidth; x++) { 323 | boolean stop = false; 324 | for (int y = 0; y < imgHeight; y++) { 325 | if (mSignatureBitmap.getPixel(x, y) != backgroundColor) { 326 | xMin = x; 327 | stop = true; 328 | foundPixel = true; 329 | break; 330 | } 331 | } 332 | if (stop) 333 | break; 334 | } 335 | 336 | // Image is empty... 337 | if (!foundPixel) 338 | return null; 339 | 340 | // Find yMin 341 | for (int y = 0; y < imgHeight; y++) { 342 | boolean stop = false; 343 | for (int x = xMin; x < imgWidth; x++) { 344 | if (mSignatureBitmap.getPixel(x, y) != backgroundColor) { 345 | yMin = y; 346 | stop = true; 347 | break; 348 | } 349 | } 350 | if (stop) 351 | break; 352 | } 353 | 354 | // Find xMax 355 | for (int x = imgWidth - 1; x >= xMin; x--) { 356 | boolean stop = false; 357 | for (int y = yMin; y < imgHeight; y++) { 358 | if (mSignatureBitmap.getPixel(x, y) != backgroundColor) { 359 | xMax = x; 360 | stop = true; 361 | break; 362 | } 363 | } 364 | if (stop) 365 | break; 366 | } 367 | 368 | // Find yMax 369 | for (int y = imgHeight - 1; y >= yMin; y--) { 370 | boolean stop = false; 371 | for (int x = xMin; x <= xMax; x++) { 372 | if (mSignatureBitmap.getPixel(x, y) != backgroundColor) { 373 | yMax = y; 374 | stop = true; 375 | break; 376 | } 377 | } 378 | if (stop) 379 | break; 380 | } 381 | 382 | return Bitmap.createBitmap(mSignatureBitmap, xMin, yMin, xMax - xMin, yMax - yMin); 383 | } 384 | 385 | public SparseArray getMotionEventRecord() { 386 | mMotionEventRecordIndex = 0; 387 | 388 | return this.mMotionEventRecord; 389 | } 390 | 391 | private boolean isDoubleClick() { 392 | if (mClearOnDoubleClick) { 393 | if (mFirstClick != 0 && System.currentTimeMillis() - mFirstClick > DOUBLE_CLICK_DELAY_MS) { 394 | mCountClick = 0; 395 | } 396 | mCountClick++; 397 | if (mCountClick == 1) { 398 | mFirstClick = System.currentTimeMillis(); 399 | } else if (mCountClick == 2) { 400 | long lastClick = System.currentTimeMillis(); 401 | if (lastClick - mFirstClick < DOUBLE_CLICK_DELAY_MS) { 402 | this.clear(); 403 | return true; 404 | } 405 | } 406 | } 407 | return false; 408 | } 409 | 410 | private TimedPoint getNewPoint(float x, float y) { 411 | int mCacheSize = mPointsCache.size(); 412 | TimedPoint timedPoint; 413 | if (mCacheSize == 0) { 414 | // Cache is empty, create a new point 415 | timedPoint = new TimedPoint(); 416 | } else { 417 | // Get point from cache 418 | timedPoint = mPointsCache.remove(mCacheSize-1); 419 | } 420 | 421 | return timedPoint.set(x, y); 422 | } 423 | 424 | private void recyclePoint(TimedPoint point) { 425 | mPointsCache.add(point); 426 | } 427 | 428 | private void addPoint(TimedPoint newPoint) { 429 | mPoints.add(newPoint); 430 | 431 | int pointsCount = mPoints.size(); 432 | if (pointsCount > 3) { 433 | 434 | ControlTimedPoints tmp = calculateCurveControlPoints(mPoints.get(0), mPoints.get(1), mPoints.get(2)); 435 | TimedPoint c2 = tmp.c2; 436 | recyclePoint(tmp.c1); 437 | 438 | tmp = calculateCurveControlPoints(mPoints.get(1), mPoints.get(2), mPoints.get(3)); 439 | TimedPoint c3 = tmp.c1; 440 | recyclePoint(tmp.c2); 441 | 442 | Bezier curve = mBezierCached.set(mPoints.get(1), c2, c3, mPoints.get(2)); 443 | 444 | TimedPoint startPoint = curve.startPoint; 445 | TimedPoint endPoint = curve.endPoint; 446 | 447 | float velocity = endPoint.velocityFrom(startPoint); 448 | velocity = Float.isNaN(velocity) ? 0.0f : velocity; 449 | 450 | velocity = mVelocityFilterWeight * velocity 451 | + (1 - mVelocityFilterWeight) * mLastVelocity; 452 | 453 | // The new width is a function of the velocity. Higher velocities 454 | // correspond to thinner strokes. 455 | float newWidth = strokeWidth(velocity); 456 | 457 | // The Bezier's width starts out as last curve's final width, and 458 | // gradually changes to the stroke width just calculated. The new 459 | // width calculation is based on the velocity between the Bezier's 460 | // start and end mPoints. 461 | addBezier(curve, mLastWidth, newWidth); 462 | 463 | mLastVelocity = velocity; 464 | mLastWidth = newWidth; 465 | 466 | // Remove the first element from the list, 467 | // so that we always have no more than 4 mPoints in mPoints array. 468 | recyclePoint(mPoints.remove(0)); 469 | 470 | recyclePoint(c2); 471 | recyclePoint(c3); 472 | 473 | } else if (pointsCount == 1) { 474 | // To reduce the initial lag make it work with 3 mPoints 475 | // by duplicating the first point 476 | TimedPoint firstPoint = mPoints.get(0); 477 | mPoints.add(getNewPoint(firstPoint.x, firstPoint.y)); 478 | } 479 | } 480 | 481 | private void addBezier(Bezier curve, float startWidth, float endWidth) { 482 | mSvgBuilder.append(curve, (startWidth + endWidth) / 2); 483 | ensureSignatureBitmap(); 484 | float originalWidth = mPaint.getStrokeWidth(); 485 | float widthDelta = endWidth - startWidth; 486 | float drawSteps = (float) Math.floor(curve.length()); 487 | 488 | for (int i = 0; i < drawSteps; i++) { 489 | // Calculate the Bezier (x, y) coordinate for this step. 490 | float t = ((float) i) / drawSteps; 491 | float tt = t * t; 492 | float ttt = tt * t; 493 | float u = 1 - t; 494 | float uu = u * u; 495 | float uuu = uu * u; 496 | 497 | float x = uuu * curve.startPoint.x; 498 | x += 3 * uu * t * curve.control1.x; 499 | x += 3 * u * tt * curve.control2.x; 500 | x += ttt * curve.endPoint.x; 501 | 502 | float y = uuu * curve.startPoint.y; 503 | y += 3 * uu * t * curve.control1.y; 504 | y += 3 * u * tt * curve.control2.y; 505 | y += ttt * curve.endPoint.y; 506 | 507 | // Set the incremental stroke width and draw. 508 | mPaint.setStrokeWidth(startWidth + ttt * widthDelta); 509 | mSignatureBitmapCanvas.drawPoint(x, y, mPaint); 510 | expandDirtyRect(x, y); 511 | } 512 | 513 | mPaint.setStrokeWidth(originalWidth); 514 | } 515 | 516 | private ControlTimedPoints calculateCurveControlPoints(TimedPoint s1, TimedPoint s2, TimedPoint s3) { 517 | float dx1 = s1.x - s2.x; 518 | float dy1 = s1.y - s2.y; 519 | float dx2 = s2.x - s3.x; 520 | float dy2 = s2.y - s3.y; 521 | 522 | float m1X = (s1.x + s2.x) / 2.0f; 523 | float m1Y = (s1.y + s2.y) / 2.0f; 524 | float m2X = (s2.x + s3.x) / 2.0f; 525 | float m2Y = (s2.y + s3.y) / 2.0f; 526 | 527 | float l1 = (float) Math.sqrt(dx1 * dx1 + dy1 * dy1); 528 | float l2 = (float) Math.sqrt(dx2 * dx2 + dy2 * dy2); 529 | 530 | float dxm = (m1X - m2X); 531 | float dym = (m1Y - m2Y); 532 | float k = l2 / (l1 + l2); 533 | if (Float.isNaN(k)) k = 0.0f; 534 | float cmX = m2X + dxm * k; 535 | float cmY = m2Y + dym * k; 536 | 537 | float tx = s2.x - cmX; 538 | float ty = s2.y - cmY; 539 | 540 | return mControlTimedPointsCached.set(getNewPoint(m1X + tx, m1Y + ty), getNewPoint(m2X + tx, m2Y + ty)); 541 | } 542 | 543 | private float strokeWidth(float velocity) { 544 | return Math.max(mMaxWidth / (velocity + 1), mMinWidth); 545 | } 546 | 547 | /** 548 | * Called when replaying history to ensure the dirty region includes all 549 | * mPoints. 550 | * 551 | * @param historicalX the previous x coordinate. 552 | * @param historicalY the previous y coordinate. 553 | */ 554 | private void expandDirtyRect(float historicalX, float historicalY) { 555 | if (historicalX < mDirtyRect.left) { 556 | mDirtyRect.left = historicalX; 557 | } else if (historicalX > mDirtyRect.right) { 558 | mDirtyRect.right = historicalX; 559 | } 560 | if (historicalY < mDirtyRect.top) { 561 | mDirtyRect.top = historicalY; 562 | } else if (historicalY > mDirtyRect.bottom) { 563 | mDirtyRect.bottom = historicalY; 564 | } 565 | } 566 | 567 | /** 568 | * Resets the dirty region when the motion event occurs. 569 | * 570 | * @param eventX the event x coordinate. 571 | * @param eventY the event y coordinate. 572 | */ 573 | private void resetDirtyRect(float eventX, float eventY) { 574 | 575 | // The mLastTouchX and mLastTouchY were set when the ACTION_DOWN motion event occurred. 576 | mDirtyRect.left = Math.min(mLastTouchX, eventX); 577 | mDirtyRect.right = Math.max(mLastTouchX, eventX); 578 | mDirtyRect.top = Math.min(mLastTouchY, eventY); 579 | mDirtyRect.bottom = Math.max(mLastTouchY, eventY); 580 | } 581 | 582 | private void setIsEmpty(boolean newValue) { 583 | mIsEmpty = newValue; 584 | if (mOnSignedListener != null) { 585 | if (mIsEmpty) { 586 | mOnSignedListener.onClear(); 587 | } else { 588 | mOnSignedListener.onSigned(); 589 | } 590 | } 591 | } 592 | 593 | private void ensureSignatureBitmap() { 594 | if (mSignatureBitmap == null) { 595 | mSignatureBitmap = Bitmap.createBitmap(getWidth(), getHeight(), 596 | Bitmap.Config.ARGB_8888); 597 | mSignatureBitmapCanvas = new Canvas(mSignatureBitmap); 598 | } 599 | } 600 | 601 | private int convertDpToPx(float dp){ 602 | return Math.round(getContext().getResources().getDisplayMetrics().density * dp); 603 | } 604 | 605 | public interface OnSignedListener { 606 | void onStartSigning(); 607 | void onSigned(); 608 | void onClear(); 609 | } 610 | 611 | public class MotionEventRecorder { 612 | // ms 613 | private long time = 0; 614 | private float x = 0; 615 | private float y = 0; 616 | private float z = 0; 617 | 618 | public MotionEventRecorder(long time, float x, float y, float z) { 619 | this.time = time; 620 | this.x = x; 621 | this.y = y; 622 | this.z = z; 623 | } 624 | 625 | public long getTime() { 626 | return time; 627 | } 628 | 629 | public void setTime(long time) { 630 | this.time = time; 631 | } 632 | 633 | public float getX() { 634 | return x; 635 | } 636 | 637 | public void setX(float x) { 638 | this.x = x; 639 | } 640 | 641 | public float getY() { 642 | return y; 643 | } 644 | 645 | public void setY(float y) { 646 | this.y = y; 647 | } 648 | 649 | public float getZ() { 650 | return z; 651 | } 652 | 653 | public void setZ(float z) { 654 | this.z = z; 655 | } 656 | 657 | @Override 658 | public String toString() { 659 | return time + " " + x + " " + y + " " + z; 660 | } 661 | } 662 | } 663 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/round_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 14 | 15 | 21 | 22 | 30 | 31 | 35 | 36 |