├── .gitignore
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── henley.jks
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── henleylee
│ │ └── lockpattern
│ │ └── demo
│ │ ├── MainActivity.java
│ │ ├── PatternCheckActivity.java
│ │ ├── PatternCreateActivity.java
│ │ ├── PatternSettingActivity.java
│ │ └── Utility.java
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_main.xml
│ ├── activity_pattern_locker.xml
│ └── activity_pattern_setting.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-mdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
├── bintray.gradle
├── config.gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lockpattern
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── henleylee
│ │ └── lockpattern
│ │ ├── Cell.java
│ │ ├── CellStatus.java
│ │ ├── Config.java
│ │ ├── DecoratorStyle.java
│ │ ├── OnPatternChangedListener.java
│ │ ├── PatternHelper.java
│ │ ├── PatternIndicatorView.java
│ │ ├── PatternLockerView.java
│ │ └── style
│ │ ├── ArrowLockerLinkedLineStyle.java
│ │ ├── DefaultIndicatorCellStyle.java
│ │ ├── DefaultIndicatorLinkedLineStyle.java
│ │ ├── DefaultLockerCellStyle.java
│ │ ├── DefaultLockerLinkedLineStyle.java
│ │ ├── ICellStyle.java
│ │ ├── ILinkedLineStyle.java
│ │ ├── RippleLockerCellStyle.java
│ │ └── StyleHelper.java
│ └── res
│ └── values
│ ├── attrs.xml
│ └── strings.xml
├── screenshots
├── pattern_check.png
├── pattern_check_error.png
├── pattern_create.png
├── pattern_create_confirm.png
└── pattern_setting.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.aar
4 | *.ap_
5 | *.aab
6 |
7 | # Files for the ART/Dalvik VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # Generated files
14 | bin/
15 | gen/
16 | out/
17 | # Uncomment the following line in case you need and you don't have the release build type files in your app
18 | # release/
19 |
20 | # Gradle files
21 | .gradle/
22 | build/
23 |
24 | # Local configuration file (sdk path, etc)
25 | local.properties
26 |
27 | # Proguard folder generated by Eclipse
28 | proguard/
29 |
30 | # Log Files
31 | *.log
32 |
33 | # Android Studio Navigation editor temp files
34 | .navigation/
35 |
36 | # Android Studio captures folder
37 | captures/
38 |
39 | # IntelliJ
40 | *.iml
41 | .idea/workspace.xml
42 | .idea/tasks.xml
43 | .idea/gradle.xml
44 | .idea/assetWizardSettings.xml
45 | .idea/dictionaries
46 | .idea/libraries
47 | # Android Studio 3 in .gitignore file.
48 | .idea/caches
49 | .idea/modules.xml
50 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you
51 | .idea/navEditor.xml
52 |
53 | # Keystore files
54 | # Uncomment the following lines if you do not want to check your keystore files in.
55 | #*.jks
56 | #*.keystore
57 |
58 | # External native build folder generated in Android Studio 2.2 and later
59 | .externalNativeBuild
60 | .cxx/
61 |
62 | # Google Services (e.g. APIs or Firebase)
63 | # google-services.json
64 |
65 | # Freeline
66 | freeline.py
67 | freeline/
68 | freeline_project_description.json
69 |
70 | # fastlane
71 | fastlane/report.xml
72 | fastlane/Preview.html
73 | fastlane/screenshots
74 | fastlane/test_output
75 | fastlane/readme.md
76 |
77 | # Version control
78 | vcs.xml
79 |
80 | # lint
81 | lint/intermediates/
82 | lint/generated/
83 | lint/outputs/
84 | lint/tmp/
85 | # lint/reports/
86 |
--------------------------------------------------------------------------------
/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 | # LockPattern —— Android
2 | This is a lock pattern in the android.
3 |
4 | 
5 | 
6 | 
7 |
8 | 
9 | 
10 |
11 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | def config = rootProject.ext.android // 配置
4 |
5 | android {
6 | compileSdkVersion config.compileSdkVersion
7 | buildToolsVersion config.buildToolsVersion
8 |
9 | defaultConfig {
10 | minSdkVersion config.minSdkVersion
11 | targetSdkVersion config.targetSdkVersion
12 | versionCode config.versionCode
13 | versionName config.versionName
14 | applicationId "com.henleylee.lockpattern.demo"
15 | }
16 |
17 | signingConfigs {
18 |
19 | henley {
20 | storeFile file('./henley.jks')
21 | storePassword '123456'
22 | keyAlias 'henley'
23 | keyPassword '123456'
24 | }
25 |
26 | }
27 |
28 | buildTypes {
29 | release {
30 | minifyEnabled false
31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
32 | signingConfig signingConfigs.henley
33 | }
34 | debug {
35 | debuggable true
36 | signingConfig signingConfigs.henley
37 | }
38 | }
39 | }
40 |
41 | dependencies {
42 | implementation fileTree(dir: 'libs', include: ['*.jar'])
43 | implementation 'androidx.appcompat:appcompat:1.0.2'
44 | implementation 'androidx.annotation:annotation:1.1.0'
45 | implementation 'com.henley.android:lockpattern:1.0.1'
46 | // implementation project(':lockpattern')
47 | }
48 |
--------------------------------------------------------------------------------
/app/henley.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/henley.jks
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
24 |
28 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/henleylee/lockpattern/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.demo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.view.View;
6 |
7 | import androidx.appcompat.app.AppCompatActivity;
8 |
9 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | setContentView(R.layout.activity_main);
15 |
16 | findViewById(R.id.pattern_create).setOnClickListener(this);
17 | findViewById(R.id.pattern_check).setOnClickListener(this);
18 | findViewById(R.id.pattern_setting).setOnClickListener(this);
19 | }
20 |
21 | @Override
22 | public void onClick(View view) {
23 | switch (view.getId()) {
24 | case R.id.pattern_create:
25 | startActivity(new Intent(this, PatternCreateActivity.class));
26 | break;
27 | case R.id.pattern_check:
28 | startActivity(new Intent(this, PatternCheckActivity.class));
29 | break;
30 | case R.id.pattern_setting:
31 | startActivity(new Intent(this, PatternSettingActivity.class));
32 | break;
33 | default:
34 | break;
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/henleylee/lockpattern/demo/PatternCheckActivity.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.demo;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.text.TextUtils;
6 | import android.view.MenuItem;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.Nullable;
10 | import androidx.appcompat.app.AppCompatActivity;
11 |
12 | import com.henleylee.lockpattern.Cell;
13 | import com.henleylee.lockpattern.CellStatus;
14 | import com.henleylee.lockpattern.OnPatternChangedListener;
15 | import com.henleylee.lockpattern.PatternHelper;
16 | import com.henleylee.lockpattern.PatternIndicatorView;
17 | import com.henleylee.lockpattern.PatternLockerView;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * 创建密码
23 | */
24 | public class PatternCheckActivity extends AppCompatActivity {
25 |
26 | private static final int MAX_RETRY_COUNT = 4;
27 |
28 | private int retryCount = MAX_RETRY_COUNT;
29 | private String password;
30 | private TextView tvMessage;
31 | private PatternIndicatorView indicatorView;
32 | private PatternLockerView lockerView;
33 |
34 | @Override
35 | protected void onCreate(@Nullable Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_pattern_locker);
38 | Utility.initActionBar(getSupportActionBar());
39 |
40 | password = Utility.getPatternPassword(this);
41 |
42 | tvMessage = findViewById(R.id.pattern_message);
43 | indicatorView = findViewById(R.id.pattern_indicator_view);
44 | lockerView = findViewById(R.id.pattern_locker_view);
45 | lockerView.setOnPatternChangedListener(new OnPatternChangedListener() {
46 | @Override
47 | public void onPatternStart() {
48 | indicatorView.setSelectedCells(null);
49 | }
50 |
51 | @Override
52 | public void onPatternChange(PatternLockerView view, List cells) {
53 |
54 | }
55 |
56 | @Override
57 | public void onPatternComplete(PatternLockerView view, List cells) {
58 | handlePatternPassword(view.getSide(), cells);
59 | }
60 |
61 | @Override
62 | public void onPatternClear() {
63 |
64 | }
65 | });
66 | }
67 |
68 | @Override
69 | public boolean onOptionsItemSelected(MenuItem item) {
70 | if (item.getItemId() == android.R.id.home) {
71 | finish();
72 | return true;
73 | }
74 | return super.onOptionsItemSelected(item);
75 | }
76 |
77 | private void handlePatternPassword(int side, List cells) {
78 | if (!TextUtils.equals(password, PatternHelper.patternToString(side, cells))) {
79 | retryCount--;
80 | lockerView.setPatternStatus(CellStatus.ERROR);
81 | indicatorView.setSelectedCells(cells);
82 | tvMessage.setTextColor(Color.RED);
83 | if (retryCount > 0) {
84 | tvMessage.setText("Wrong password,You can also enter " + retryCount + " Second-rate");
85 | } else {
86 | retryCount = MAX_RETRY_COUNT;
87 | tvMessage.setText("Wrong password,please enter again");
88 | }
89 | return;
90 | }
91 | indicatorView.setSelectedCells(cells);
92 | Utility.showToast(this, "Password is correct");
93 | finish();
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/main/java/com/henleylee/lockpattern/demo/PatternCreateActivity.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.demo;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.text.TextUtils;
6 | import android.view.MenuItem;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.Nullable;
10 | import androidx.appcompat.app.AppCompatActivity;
11 |
12 | import com.henleylee.lockpattern.Cell;
13 | import com.henleylee.lockpattern.CellStatus;
14 | import com.henleylee.lockpattern.OnPatternChangedListener;
15 | import com.henleylee.lockpattern.PatternHelper;
16 | import com.henleylee.lockpattern.PatternIndicatorView;
17 | import com.henleylee.lockpattern.PatternLockerView;
18 | import com.henleylee.lockpattern.style.ArrowLockerLinkedLineStyle;
19 | import com.henleylee.lockpattern.style.RippleLockerCellStyle;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | * 创建密码
25 | */
26 | public class PatternCreateActivity extends AppCompatActivity {
27 |
28 | private static final int MIN_PASSWORD_LENGTH = 4;
29 |
30 | private String password;
31 | private TextView tvMessage;
32 | private PatternIndicatorView indicatorView;
33 | private PatternLockerView lockerView;
34 |
35 | @Override
36 | protected void onCreate(@Nullable Bundle savedInstanceState) {
37 | super.onCreate(savedInstanceState);
38 | setContentView(R.layout.activity_pattern_locker);
39 | Utility.initActionBar(getSupportActionBar());
40 |
41 | tvMessage = findViewById(R.id.pattern_message);
42 | indicatorView = findViewById(R.id.pattern_indicator_view);
43 | lockerView = findViewById(R.id.pattern_locker_view);
44 | lockerView.setCellStyle(new RippleLockerCellStyle(lockerView.getDecoratorStyle()));
45 | lockerView.setLinkedLineStyle(new ArrowLockerLinkedLineStyle(lockerView.getDecoratorStyle()));
46 | lockerView.setOnPatternChangedListener(new OnPatternChangedListener() {
47 | @Override
48 | public void onPatternStart() {
49 | }
50 |
51 | @Override
52 | public void onPatternChange(PatternLockerView view, List cells) {
53 |
54 | }
55 |
56 | @Override
57 | public void onPatternComplete(PatternLockerView view, List cells) {
58 | handlePatternPassword(view.getSide(), cells);
59 | }
60 |
61 | @Override
62 | public void onPatternClear() {
63 |
64 | }
65 | });
66 | }
67 |
68 | @Override
69 | public boolean onOptionsItemSelected(MenuItem item) {
70 | if (item.getItemId() == android.R.id.home) {
71 | finish();
72 | return true;
73 | }
74 | return super.onOptionsItemSelected(item);
75 | }
76 |
77 | private void handlePatternPassword(int side, List cells) {
78 | lockerView.setPatternStatus(CellStatus.ERROR);
79 | if (TextUtils.isEmpty(password)) {
80 | if (cells == null || cells.size() < 4) {
81 | lockerView.setPatternStatus(CellStatus.ERROR);
82 | indicatorView.setSelectedCells(cells);
83 | tvMessage.setTextColor(Color.RED);
84 | tvMessage.setText("Least connection " + MIN_PASSWORD_LENGTH + " Points,Please redraw");
85 | return;
86 | }
87 | indicatorView.setSelectedCells(cells);
88 | password = PatternHelper.patternToString(side, cells);
89 | tvMessage.setTextColor(Color.DKGRAY);
90 | tvMessage.setText("Draw the unlock pattern again");
91 | } else {
92 | if (!TextUtils.equals(password, PatternHelper.patternToString(side, cells))) {
93 | lockerView.setPatternStatus(CellStatus.ERROR);
94 | indicatorView.setSelectedCells(cells);
95 | tvMessage.setTextColor(Color.RED);
96 | tvMessage.setText("Inconsistent with the last drawing,Please redraw");
97 | return;
98 | }
99 | indicatorView.setSelectedCells(cells);
100 | Utility.savePatternPassword(this, password);
101 | Utility.showToast(this, "Password set successfully");
102 | finish();
103 | }
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/app/src/main/java/com/henleylee/lockpattern/demo/PatternSettingActivity.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.demo;
2 |
3 | import android.os.Bundle;
4 | import android.view.MenuItem;
5 | import android.widget.CheckBox;
6 | import android.widget.CompoundButton;
7 |
8 | import androidx.appcompat.app.AppCompatActivity;
9 |
10 | import com.henleylee.lockpattern.Cell;
11 | import com.henleylee.lockpattern.CellStatus;
12 | import com.henleylee.lockpattern.OnPatternChangedListener;
13 | import com.henleylee.lockpattern.PatternIndicatorView;
14 | import com.henleylee.lockpattern.PatternLockerView;
15 | import com.henleylee.lockpattern.style.ArrowLockerLinkedLineStyle;
16 | import com.henleylee.lockpattern.style.DefaultLockerCellStyle;
17 | import com.henleylee.lockpattern.style.DefaultLockerLinkedLineStyle;
18 | import com.henleylee.lockpattern.style.RippleLockerCellStyle;
19 |
20 | import java.util.List;
21 |
22 | public class PatternSettingActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
23 |
24 | private boolean isFailure;
25 | private PatternIndicatorView indicatorView;
26 | private PatternLockerView lockerView;
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_pattern_setting);
32 | Utility.initActionBar(getSupportActionBar());
33 |
34 | indicatorView = findViewById(R.id.pattern_indicator_view);
35 | lockerView = findViewById(R.id.pattern_locker_view);
36 | lockerView.setOnPatternChangedListener(new OnPatternChangedListener() {
37 | @Override
38 | public void onPatternStart() {
39 | indicatorView.setSelectedCells(null);
40 | }
41 |
42 | @Override
43 | public void onPatternChange(PatternLockerView view, List cells) {
44 |
45 | }
46 |
47 | @Override
48 | public void onPatternComplete(PatternLockerView view, List cells) {
49 | if (isFailure) {
50 | view.setPatternStatus(CellStatus.ERROR);
51 | }
52 | indicatorView.setSelectedCells(cells);
53 | }
54 |
55 | @Override
56 | public void onPatternClear() {
57 |
58 | }
59 | });
60 |
61 | ((CheckBox) findViewById(R.id.check_box_fail)).setOnCheckedChangeListener(this);
62 | ((CheckBox) findViewById(R.id.check_box_feedback)).setOnCheckedChangeListener(this);
63 | ((CheckBox) findViewById(R.id.check_box_skip_mode)).setOnCheckedChangeListener(this);
64 | ((CheckBox) findViewById(R.id.check_box_stealth_mode)).setOnCheckedChangeListener(this);
65 | ((CheckBox) findViewById(R.id.check_box_auto_clean)).setOnCheckedChangeListener(this);
66 | ((CheckBox) findViewById(R.id.check_box_ripple)).setOnCheckedChangeListener(this);
67 | ((CheckBox) findViewById(R.id.check_box_arrow)).setOnCheckedChangeListener(this);
68 | }
69 |
70 | @Override
71 | public boolean onOptionsItemSelected(MenuItem item) {
72 | if (item.getItemId() == android.R.id.home) {
73 | finish();
74 | return true;
75 | }
76 | return super.onOptionsItemSelected(item);
77 | }
78 |
79 | @Override
80 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
81 | switch (buttonView.getId()) {
82 | case R.id.check_box_fail:
83 | isFailure = isChecked;
84 | break;
85 | case R.id.check_box_feedback:
86 | lockerView.setEnableHapticFeedback(isChecked);
87 | break;
88 | case R.id.check_box_skip_mode:
89 | lockerView.setEnableSkipMode(isChecked);
90 | break;
91 | case R.id.check_box_stealth_mode:
92 | lockerView.setEnableInStealthMode(isChecked);
93 | break;
94 | case R.id.check_box_auto_clean:
95 | lockerView.setEnableAutoClean(isChecked);
96 | break;
97 | case R.id.check_box_ripple:
98 | if (isChecked) {
99 | lockerView.setCellStyle(new RippleLockerCellStyle(lockerView.getDecoratorStyle()));
100 | } else {
101 | lockerView.setCellStyle(new DefaultLockerCellStyle(lockerView.getDecoratorStyle()));
102 | }
103 | break;
104 | case R.id.check_box_arrow:
105 | if (isChecked) {
106 | lockerView.setLinkedLineStyle(new ArrowLockerLinkedLineStyle(lockerView.getDecoratorStyle()));
107 | } else {
108 | lockerView.setLinkedLineStyle(new DefaultLockerLinkedLineStyle(lockerView.getDecoratorStyle()));
109 | }
110 | break;
111 | default:
112 | break;
113 | }
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/app/src/main/java/com/henleylee/lockpattern/demo/Utility.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.demo;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.text.TextUtils;
6 | import android.widget.Toast;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.annotation.Nullable;
10 | import androidx.appcompat.app.ActionBar;
11 |
12 | /**
13 | * Toast工具类
14 | */
15 | public class Utility {
16 |
17 | private static final String PREFERENCES_NAME = "config_pref";
18 | private static final String PATTERN_PASSWORD = "pattern_password";
19 |
20 | private static Toast sToast;
21 |
22 | @SuppressLint("ShowToast")
23 | public static void showToast(@NonNull Context context, @Nullable CharSequence text) {
24 | if (text == null || TextUtils.isEmpty(text)) {
25 | return;
26 | }
27 | int duration = Toast.LENGTH_SHORT;
28 | if (text.length() > 15) {
29 | duration = Toast.LENGTH_LONG;
30 | }
31 | if (sToast == null) {
32 | sToast = Toast.makeText(context.getApplicationContext(), text, duration);
33 | } else {
34 | sToast.setText(text);
35 | }
36 | sToast.show();
37 | }
38 |
39 | /**
40 | * 初始化ActionBar
41 | */
42 | public static void initActionBar(ActionBar actionBar) {
43 | if (actionBar != null) {
44 | actionBar.setDisplayShowTitleEnabled(true); // Set whether to title/subtitle (default is true)
45 | actionBar.setHomeButtonEnabled(true); // Set whether to enable the icon on the left to be clickable (default is false)
46 | actionBar.setDisplayShowHomeEnabled(true); // Set whether to display the application icon (default is true)
47 | actionBar.setDisplayHomeAsUpEnabled(true); // Set whether to add a return icon to the left side (default is false)
48 | }
49 | }
50 |
51 | public static String getPatternPassword(@NonNull Context context) {
52 | return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
53 | .getString(PATTERN_PASSWORD, null);
54 | }
55 |
56 | public static void savePatternPassword(@NonNull Context context, @Nullable String password) {
57 | context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
58 | .edit()
59 | .putString(PATTERN_PASSWORD, password)
60 | .apply();
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
20 |
21 |
31 |
32 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_pattern_locker.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
26 |
27 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_pattern_setting.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
25 |
26 |
32 |
33 |
38 |
39 |
43 |
44 |
48 |
49 |
55 |
56 |
62 |
63 |
64 |
68 |
69 |
75 |
76 |
82 |
83 |
84 |
85 |
91 |
92 |
96 |
97 |
104 |
105 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #108EE9
4 | #108EE9
5 | #108EE9
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LockPattern
3 |
4 | Pattern Create
5 | Pattern Check
6 | Pattern Setting
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | apply from: "./gradle/config.gradle"
3 |
4 | buildscript {
5 | repositories {
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:4.1.3'
12 | classpath 'com.novoda:bintray-release:0.9'
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 |
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/gradle/bintray.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.novoda.bintray-release'
2 |
3 | allprojects {
4 | repositories {
5 | jcenter()
6 | }
7 | tasks.withType(Javadoc) {
8 | options{
9 | encoding "UTF-8"
10 | charSet 'UTF-8'
11 | links "http://docs.oracle.com/javase/7/docs/api"
12 | }
13 | options.addStringOption('Xdoclint:none', '-quiet')
14 | }
15 | }
16 |
17 |
18 | def siteUrl = 'https://github.com/HenleyLee/LockPattern'
19 |
20 | publish {
21 | userOrg = 'henleylee'
22 | groupId = 'com.henley.android'
23 | artifactId = 'lockpattern'
24 | publishVersion = '1.0.1'
25 | uploadName = 'LockPattern'
26 | desc = 'Android 手势解锁、九宫格图案解锁(默认为3*3九空格,支持N*N宫格扩展)。'
27 | website = siteUrl
28 | }
--------------------------------------------------------------------------------
/gradle/config.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 |
3 | android = [ // 配置信息
4 | compileSdkVersion: 31,
5 | buildToolsVersion: "30.0.3",
6 | minSdkVersion : 21,
7 | targetSdkVersion : 31,
8 | versionCode : 2,
9 | versionName : "1.0.1"
10 | ]
11 |
12 | // 依赖第三方库版本
13 | junitVersion = "4.12"
14 | supportVersion = "30.3.3"
15 | multidexVersion = "1.0.3"
16 | constraintVersion = "1.1.3"
17 | zxingVersion = "3.3.3"
18 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Dec 24 08:40:21 MSK 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/lockpattern/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lockpattern/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | def config = rootProject.ext.android // 配置
4 |
5 | android {
6 | compileSdkVersion config.compileSdkVersion
7 | buildToolsVersion config.buildToolsVersion
8 |
9 | defaultConfig {
10 | minSdkVersion config.minSdkVersion
11 | targetSdkVersion config.targetSdkVersion
12 | versionCode config.versionCode
13 | versionName config.versionName
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 |
28 | compileOnly 'androidx.annotation:annotation:1.1.0'
29 | }
30 |
31 | apply from: '../gradle/bintray.gradle'
--------------------------------------------------------------------------------
/lockpattern/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/lockpattern/consumer-rules.pro
--------------------------------------------------------------------------------
/lockpattern/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/lockpattern/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/Cell.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import androidx.annotation.NonNull;
4 |
5 | /**
6 | * {@link Cell}信息
7 | *
8 | * @author Henley
9 | * @since 2019/8/26 16:39
10 | */
11 | public class Cell {
12 |
13 | private int index; // the cell index
14 | private float x; // the x position of circle's center point
15 | private float y; // the y position of circle's center point
16 | private float radius; // the cell radius
17 | private int status; // the cell status
18 |
19 | public Cell(int index, float x, float y, float radius, int status) {
20 | this.index = index;
21 | this.x = x;
22 | this.y = y;
23 | this.radius = radius;
24 | this.status = status;
25 | }
26 |
27 | public int getIndex() {
28 | return index;
29 | }
30 |
31 | public void setIndex(int index) {
32 | this.index = index;
33 | }
34 |
35 | public float getX() {
36 | return x;
37 | }
38 |
39 | public void setX(float x) {
40 | this.x = x;
41 | }
42 |
43 | public float getY() {
44 | return y;
45 | }
46 |
47 | public void setY(float y) {
48 | this.y = y;
49 | }
50 |
51 | public float getRadius() {
52 | return radius;
53 | }
54 |
55 | public void setRadius(float radius) {
56 | this.radius = radius;
57 | }
58 |
59 | public int getStatus() {
60 | return status;
61 | }
62 |
63 | public void setStatus(int status) {
64 | this.status = status;
65 | }
66 |
67 | @NonNull
68 | @Override
69 | public String toString() {
70 | return "Cell{" +
71 | "index=" + index +
72 | ", x=" + x +
73 | ", y=" + y +
74 | ", radius=" + radius +
75 | ", status=" + status +
76 | '}';
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/CellStatus.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import androidx.annotation.IntDef;
4 |
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 |
8 | /**
9 | * {@link Cell}状态
10 | *
11 | * @author Henley
12 | * @since 2019/8/26 19:52
13 | */
14 | @IntDef({
15 | CellStatus.NORMAL,
16 | CellStatus.CHECK,
17 | CellStatus.ERROR
18 | })
19 | @Retention(RetentionPolicy.SOURCE)
20 | public @interface CellStatus {
21 |
22 | /**
23 | * default status
24 | */
25 | int NORMAL = 0;
26 | /**
27 | * checked status
28 | */
29 | int CHECK = 1;
30 | /**
31 | * checked error status
32 | */
33 | int ERROR = 2;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/Config.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import android.graphics.Color;
4 |
5 | import androidx.annotation.ColorInt;
6 |
7 | /**
8 | * Config
9 | *
10 | * @author Henley
11 | * @since 2019/8/26 18:04
12 | */
13 | final class Config {
14 |
15 | @ColorInt
16 | static final int DEFAULT_NORMAL_COLOR = Color.parseColor("#108EE9");
17 | @ColorInt
18 | static final int DEFAULT_CHECK_COLOR = Color.parseColor("#108EE9");
19 | @ColorInt
20 | static final int DEFAULT_ERROR_COLOR = Color.parseColor("#F44336");
21 | @ColorInt
22 | static final int DEFAULT_FILL_COLOR = Color.parseColor("#FFFFFF");
23 |
24 | static final int DEFAULT_SIDE = 3;
25 | static final int DEFAULT_FREEZE_DURATION = 1000; // ms
26 |
27 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/DecoratorStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import androidx.annotation.ColorInt;
4 |
5 | /**
6 | * Style(颜色、描边等信息)
7 | *
8 | * @author Henley
9 | * @since 2019/8/26 17:05
10 | */
11 | public class DecoratorStyle {
12 |
13 | @ColorInt
14 | private int normalColor;
15 | @ColorInt
16 | private int fillColor;
17 | @ColorInt
18 | private int checkColor;
19 | @ColorInt
20 | private int errorColor;
21 |
22 | private float strokeWidth;
23 | private float lineWidth;
24 |
25 | public DecoratorStyle(int normalColor, int checkColor, int errorColor, int fillColor, float strokeWidth, float lineWidth) {
26 | this.normalColor = normalColor;
27 | this.checkColor = checkColor;
28 | this.errorColor = errorColor;
29 | this.fillColor = fillColor;
30 | this.strokeWidth = strokeWidth;
31 | this.lineWidth = lineWidth;
32 | }
33 |
34 | public int getNormalColor() {
35 | return normalColor;
36 | }
37 |
38 | public void setNormalColor(int normalColor) {
39 | this.normalColor = normalColor;
40 | }
41 |
42 | public int getFillColor() {
43 | return fillColor;
44 | }
45 |
46 | public void setFillColor(int fillColor) {
47 | this.fillColor = fillColor;
48 | }
49 |
50 | public int getCheckColor() {
51 | return checkColor;
52 | }
53 |
54 | public void setCheckColor(int checkColor) {
55 | this.checkColor = checkColor;
56 | }
57 |
58 | public int getErrorColor() {
59 | return errorColor;
60 | }
61 |
62 | public void setErrorColor(int errorColor) {
63 | this.errorColor = errorColor;
64 | }
65 |
66 | public float getStrokeWidth() {
67 | return strokeWidth;
68 | }
69 |
70 | public void setStrokeWidth(float strokeWidth) {
71 | this.strokeWidth = strokeWidth;
72 | }
73 |
74 | public float getLineWidth() {
75 | return lineWidth;
76 | }
77 |
78 | public void setLineWidth(float lineWidth) {
79 | this.lineWidth = lineWidth;
80 | }
81 |
82 | @ColorInt
83 | public int getColor(@CellStatus int status) {
84 | if (status == CellStatus.ERROR) {
85 | return getErrorColor();
86 | } else if (status == CellStatus.CHECK) {
87 | return getCheckColor();
88 | } else {
89 | return getNormalColor();
90 | }
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/OnPatternChangedListener.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 手势解锁监听
7 | *
8 | * @author Henley
9 | * @since 2019/8/26 16:54
10 | */
11 | public interface OnPatternChangedListener {
12 |
13 | /**
14 | * 开始绘制图案时(即手指按下触碰到绘画区域时)会调用该方法
15 | */
16 | void onPatternStart();
17 |
18 | /**
19 | * 图案绘制改变时(即手指在绘画区域移动时)会调用该方法,请注意只有 @param hitList改变了才会触发此方法
20 | *
21 | * @param view
22 | * @param cells
23 | */
24 | void onPatternChange(PatternLockerView view, List cells);
25 |
26 | /**
27 | * 图案绘制完成时(即手指抬起离开绘画区域时)会调用该方法
28 | *
29 | * @param view
30 | * @param cells
31 | */
32 | void onPatternComplete(PatternLockerView view, List cells);
33 |
34 | /**
35 | * 已绘制的图案被清除时会调用该方法
36 | */
37 | void onPatternClear();
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/PatternHelper.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import java.security.MessageDigest;
4 | import java.security.NoSuchAlgorithmException;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * 工具类
10 | *
11 | * @author Henley
12 | * @since 2019/8/27 17:41
13 | */
14 | public final class PatternHelper {
15 |
16 | static List getCellList(int length, int width, int height, int offsetX, int offsetY) {
17 | List result = new ArrayList<>();
18 | float radius = Math.min(width, height) * 1f / (length * 2 + length - 1);
19 | for (int i = 0; i < length; i++) {
20 | for (int j = 0; j < length; j++) {
21 | int index = (i * length + j) % (length * length);
22 | float x = (j * 3 + 1) * radius + offsetX;
23 | float y = (i * 3 + 1) * radius + offsetY;
24 | result.add(new Cell(index, x, y, radius, CellStatus.NORMAL));
25 | }
26 | }
27 | return result;
28 | }
29 |
30 | /**
31 | * 判断三个点是否在一条直线上
32 | */
33 | static boolean isInLine(double x1, double y1, double x2, double y2, double x3, double y3) {
34 | if ((x1 == x2 && x2 == x3) || (y1 == y2 && y2 == y3)) {
35 | return true;
36 | }
37 | return (x2 - x1) * (y3 - y1) == (x3 - x1) * (y2 - y1);
38 | }
39 |
40 | /**
41 | * 点(x2,y2)是否在以(x1,y1)为圆心,r为半径的圆内
42 | */
43 | static boolean isInRound(double x1, double y1, double x2, double y2, double r) {
44 | return distance(x1, y1, x2, y2) < r;
45 | }
46 |
47 | /**
48 | * 两点间距
49 | */
50 | public static double distance(double x1, double y1, double x2, double y2) {
51 | return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
52 | }
53 |
54 | public static String patternToString(int side, List cells) {
55 | if (cells == null) {
56 | return null;
57 | }
58 | StringBuilder builder = new StringBuilder();
59 | builder.append("$").append(side);
60 | for (Cell cell : cells) {
61 | builder.append("-").append(cell.getIndex());
62 | }
63 | return builder.toString();
64 | }
65 |
66 | public static String patternToHash(int side, List cells) {
67 | if (cells == null) {
68 | return null;
69 | }
70 | StringBuilder builder = new StringBuilder();
71 | builder.append("$").append(side);
72 | for (Cell cell : cells) {
73 | builder.append("-").append(cell.getIndex());
74 | }
75 | byte[] bytes = builder.toString().getBytes();
76 | try {
77 | return toHexString(MessageDigest.getInstance("SHA-1").digest(bytes));
78 | } catch (NoSuchAlgorithmException var1) {
79 | return toHexString(bytes);
80 | }
81 | }
82 |
83 | public static String toHexString(byte[] bytes) {
84 | StringBuilder builder = new StringBuilder();
85 | for (int i = 0; i < bytes.length; i++) {
86 | String hex = Integer.toHexString(bytes[i] & 0xFF);
87 | if (hex.length() == 1) {
88 | hex = '0' + hex;
89 | }
90 | builder.append(hex.toUpperCase());
91 | }
92 | return builder.toString();
93 | }
94 |
95 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/PatternIndicatorView.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 |
9 | import androidx.annotation.Nullable;
10 |
11 | import com.henleylee.lockpattern.style.DefaultIndicatorCellStyle;
12 | import com.henleylee.lockpattern.style.DefaultIndicatorLinkedLineStyle;
13 | import com.henleylee.lockpattern.style.ICellStyle;
14 | import com.henleylee.lockpattern.style.ILinkedLineStyle;
15 |
16 | import java.util.ArrayList;
17 | import java.util.List;
18 |
19 | /**
20 | * 手势解锁指示器控件(用于展示绘制路径)
21 | *
22 | * @author Henley
23 | * @since 2019/8/26 17:43
24 | */
25 | public class PatternIndicatorView extends View {
26 |
27 | /** 每条边有几个点,默认为{@code 3} */
28 | private int side = 3;
29 | /** 绘制{@link Cell}样式 */
30 | private ICellStyle cellStyle = null;
31 | /** 绘制{@link Cell}连接线样式 */
32 | private ILinkedLineStyle linkedLineStyle = null;
33 | /** 真正的{@link Cell}集合 */
34 | private List mCells = new ArrayList<>();
35 | /** 选中的{@link Cell}集合 */
36 | private List mSelectedCells = new ArrayList<>();
37 | /** 绘制样式(颜色、描边等信息) */
38 | private DecoratorStyle decoratorStyle;
39 |
40 | public PatternIndicatorView(Context context) {
41 | this(context, null);
42 | }
43 |
44 | public PatternIndicatorView(Context context, @Nullable AttributeSet attrs) {
45 | this(context, attrs, 0);
46 | }
47 |
48 | public PatternIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
49 | super(context, attrs, defStyleAttr);
50 | initAttrs(context, attrs, defStyleAttr);
51 | }
52 |
53 | private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
54 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PatternIndicatorView, defStyleAttr, 0);
55 |
56 | this.side = ta.getInt(R.styleable.PatternIndicatorView_pattern_side, Config.DEFAULT_SIDE);
57 | int normalColor = ta.getColor(R.styleable.PatternIndicatorView_pattern_normalColor, Config.DEFAULT_NORMAL_COLOR);
58 | int checkColor = ta.getColor(R.styleable.PatternIndicatorView_pattern_checkColor, Config.DEFAULT_CHECK_COLOR);
59 | int errorColor = ta.getColor(R.styleable.PatternIndicatorView_pattern_errorColor, Config.DEFAULT_ERROR_COLOR);
60 | int fillColor = ta.getColor(R.styleable.PatternIndicatorView_pattern_fillColor, Config.DEFAULT_FILL_COLOR);
61 | float strokeWidth = ta.getDimension(R.styleable.PatternIndicatorView_pattern_strokeWidth, 2.5f);
62 | float lineWidth = ta.getDimension(R.styleable.PatternIndicatorView_pattern_lineWidth, 5f);
63 |
64 | ta.recycle();
65 |
66 | // style
67 | this.decoratorStyle = new DecoratorStyle(normalColor, checkColor, errorColor, fillColor, strokeWidth, lineWidth);
68 | this.cellStyle = new DefaultIndicatorCellStyle(decoratorStyle);
69 | this.linkedLineStyle = new DefaultIndicatorLinkedLineStyle(decoratorStyle);
70 | }
71 |
72 | /**
73 | * 返回每条边有几个点(默认为3)
74 | */
75 | public int getSide() {
76 | return side;
77 | }
78 |
79 | /**
80 | * 设置每条边有几个点
81 | *
82 | * @param side 每条边有几个点(默认为3)
83 | */
84 | public void setSide(int side) {
85 | this.side = side;
86 | }
87 |
88 | /**
89 | * 返回绘制{@link Cell}样式
90 | *
91 | * @see ICellStyle
92 | * @see DefaultIndicatorCellStyle
93 | */
94 | public ICellStyle getCellStyle() {
95 | return cellStyle;
96 | }
97 |
98 | /**
99 | * 设置绘制{@link Cell}样式
100 | *
101 | * @param cellStyle 绘制{@link Cell}样式
102 | * @see ICellStyle
103 | * @see DefaultIndicatorCellStyle
104 | */
105 | public void setCellStyle(ICellStyle cellStyle) {
106 | this.cellStyle = cellStyle;
107 | }
108 |
109 | /**
110 | * 返回绘制连接线样式
111 | *
112 | * @see ILinkedLineStyle
113 | * @see DefaultIndicatorLinkedLineStyle
114 | */
115 | public ILinkedLineStyle getLinkedLineStyle() {
116 | return linkedLineStyle;
117 | }
118 |
119 | /**
120 | * 返回绘制连接线样式
121 | *
122 | * @param linkedLineStyle 绘制连接线样式
123 | * @see ILinkedLineStyle
124 | * @see DefaultIndicatorLinkedLineStyle
125 | */
126 | public void setLinkedLineStyle(ILinkedLineStyle linkedLineStyle) {
127 | this.linkedLineStyle = linkedLineStyle;
128 | }
129 |
130 | /**
131 | * 返回Style(颜色、描边等信息)
132 | */
133 | public DecoratorStyle getDecoratorStyle() {
134 | return decoratorStyle;
135 | }
136 |
137 | /**
138 | * 设置Style(颜色、描边等信息)
139 | *
140 | * @param decoratorStyle Style(颜色、描边等信息)
141 | */
142 | public void setDecoratorStyle(DecoratorStyle decoratorStyle) {
143 | this.decoratorStyle = decoratorStyle;
144 | }
145 |
146 | /**
147 | * 返回选中的{@link Cell}集合
148 | */
149 | public List getSelectedCells() {
150 | return mSelectedCells;
151 | }
152 |
153 | /**
154 | * 设置选中的{@link Cell}集合
155 | */
156 | public void setSelectedCells(List selectedCells) {
157 | // 1. clear pre state
158 | mSelectedCells.clear();
159 | setPatternStatus(CellStatus.NORMAL);
160 |
161 | // 2. record new state
162 | if (selectedCells != null) {
163 | for (Cell selected : selectedCells) {
164 | for (Cell cell : mCells) {
165 | if (selected.getIndex() == cell.getIndex()) {
166 | cell.setStatus(selected.getStatus());
167 | mSelectedCells.add(cell);
168 | }
169 | }
170 | }
171 | }
172 |
173 | // 3. invalidate view
174 | postInvalidate();
175 | }
176 |
177 | /**
178 | * 设置绘制状态
179 | *
180 | * @param status 绘制状态
181 | * @see CellStatus
182 | */
183 | public void setPatternStatus(@CellStatus int status) {
184 | for (Cell cell : mCells) {
185 | cell.setStatus(status);
186 | }
187 | postInvalidate();
188 | }
189 |
190 | @Override
191 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
192 | int measureSpec = Math.min(widthMeasureSpec, heightMeasureSpec);
193 | super.onMeasure(measureSpec, measureSpec);
194 | initCells();
195 | }
196 |
197 | @Override
198 | protected void onDraw(Canvas canvas) {
199 | super.onDraw(canvas);
200 | if (cellStyle != null && !mCells.isEmpty()) {
201 | cellStyle.draw(canvas, mCells);
202 | }
203 | if (linkedLineStyle != null && !mSelectedCells.isEmpty()) {
204 | linkedLineStyle.draw(canvas, mSelectedCells, 0, 0);
205 | }
206 | }
207 |
208 | private void initCells() {
209 | mCells.clear();
210 | mSelectedCells.clear();
211 | int offsetX = getPaddingLeft();
212 | int offsetY = getPaddingTop();
213 | int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
214 | int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
215 | mCells.addAll(PatternHelper.getCellList(side, width, height, offsetX, offsetY));
216 | }
217 |
218 | }
219 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/PatternLockerView.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Canvas;
7 | import android.util.AttributeSet;
8 | import android.view.HapticFeedbackConstants;
9 | import android.view.MotionEvent;
10 | import android.view.View;
11 |
12 | import androidx.annotation.Nullable;
13 |
14 | import com.henleylee.lockpattern.style.DefaultLockerCellStyle;
15 | import com.henleylee.lockpattern.style.DefaultLockerLinkedLineStyle;
16 | import com.henleylee.lockpattern.style.ICellStyle;
17 | import com.henleylee.lockpattern.style.ILinkedLineStyle;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | /**
23 | * 手势解锁控件(默认为3*3九空格,支持N*N宫格扩展)
24 | *
25 | * @author Henley
26 | * @since 2019/8/26 16:55
27 | */
28 | public class PatternLockerView extends View {
29 |
30 | /** 每条边有几个点,默认为{@code 3} */
31 | private int side = 3;
32 | /** 绘制完成清除路径的延迟时间(单位ms),只有在{@link #enableAutoClean}为{@code true}时有效 */
33 | private long freezeDuration = 1000L;
34 | /** 绘制完成后是否自动清除标志位,如果开启了该标志位,延时{@link #freezeDuration}毫秒后自动清除已绘制图案 */
35 | private boolean enableAutoClean = true;
36 | /** 是否开启跳过中间点模式,如果开启了该标志,则可以不用连续中间点 */
37 | private boolean enableSkipMode = false;
38 | /** 是否开启绘制路径隐藏模式,如果开启了该标志位,则隐藏绘制路径 */
39 | private boolean enableInStealthMode = false;
40 | /** 是否开启触摸反馈,如果开启了该标志,则每连接一个{@link Cell}则会震动 */
41 | private boolean enableHapticFeedback = false;
42 |
43 | /** 绘制{@link Cell}样式 */
44 | private ICellStyle cellStyle = null;
45 | /** 绘制{@link Cell}连接线样式 */
46 | private ILinkedLineStyle linkedLineStyle = null;
47 | /** 绘制样式(颜色、描边等信息) */
48 | private DecoratorStyle decoratorStyle;
49 |
50 | /** 终点X坐标 */
51 | private float endX = 0f;
52 | /** 终点Y坐标 */
53 | private float endY = 0f;
54 | /** 记录绘制多少个{@link Cell},用于判断是否调用{@link OnPatternChangedListener} */
55 | private int hitSize = 0;
56 |
57 | /** 真正的{@link Cell}集合 */
58 | private List mCells = new ArrayList<>();
59 | /** 选中的{@link Cell}集合 */
60 | private List mSelectedCells = new ArrayList<>();
61 | /** 手势解锁监听 */
62 | private OnPatternChangedListener mPatternChangedListener = null;
63 | /** 清除绘制路径的{@link Runnable} */
64 | private Runnable mClearPatternRunnable = new Runnable() {
65 | @Override
66 | public void run() {
67 | clearPattern();
68 | }
69 | };
70 |
71 | public PatternLockerView(Context context) {
72 | this(context, null);
73 | }
74 |
75 | public PatternLockerView(Context context, @Nullable AttributeSet attrs) {
76 | this(context, attrs, 0);
77 | }
78 |
79 | public PatternLockerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
80 | super(context, attrs, defStyleAttr);
81 | initAttrs(context, attrs, defStyleAttr);
82 | }
83 |
84 | private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
85 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PatternLockerView, defStyleAttr, 0);
86 |
87 | int normalColor = ta.getColor(R.styleable.PatternLockerView_pattern_normalColor, Config.DEFAULT_NORMAL_COLOR);
88 | int checkColor = ta.getColor(R.styleable.PatternLockerView_pattern_checkColor, Config.DEFAULT_CHECK_COLOR);
89 | int errorColor = ta.getColor(R.styleable.PatternLockerView_pattern_errorColor, Config.DEFAULT_ERROR_COLOR);
90 | int fillColor = ta.getColor(R.styleable.PatternLockerView_pattern_fillColor, Config.DEFAULT_FILL_COLOR);
91 | float strokeWidth = ta.getDimension(R.styleable.PatternLockerView_pattern_strokeWidth, 4f);
92 | float lineWidth = ta.getDimension(R.styleable.PatternLockerView_pattern_lineWidth, 8f);
93 |
94 | this.side = ta.getInt(R.styleable.PatternLockerView_pattern_side, Config.DEFAULT_SIDE);
95 | this.enableSkipMode = ta.getBoolean(R.styleable.PatternLockerView_pattern_enableSkipMode, false);
96 | this.enableInStealthMode = ta.getBoolean(R.styleable.PatternLockerView_pattern_enableInStealthMode, false);
97 | this.freezeDuration = ta.getInteger(R.styleable.PatternLockerView_pattern_freezeDuration, Config.DEFAULT_FREEZE_DURATION);
98 | this.enableAutoClean = ta.getBoolean(R.styleable.PatternLockerView_pattern_enableAutoClean, true);
99 | this.enableHapticFeedback = ta.getBoolean(R.styleable.PatternLockerView_pattern_enableHapticFeedback, false);
100 |
101 | ta.recycle();
102 |
103 | // style
104 | this.decoratorStyle = new DecoratorStyle(normalColor, checkColor, errorColor, fillColor, strokeWidth, lineWidth);
105 | this.cellStyle = new DefaultLockerCellStyle(decoratorStyle);
106 | this.linkedLineStyle = new DefaultLockerLinkedLineStyle(decoratorStyle);
107 | }
108 |
109 | /**
110 | * 返回每条边有几个点(默认为3)
111 | */
112 | public int getSide() {
113 | return side;
114 | }
115 |
116 | /**
117 | * 设置每条边有几个点
118 | *
119 | * @param side 每条边有几个点(默认为3)
120 | */
121 | public void setSide(int side) {
122 | this.side = side;
123 | }
124 |
125 | /**
126 | * 返回绘制完成清除路径的延迟时间(单位:ms)
127 | */
128 | public long getFreezeDuration() {
129 | return freezeDuration;
130 | }
131 |
132 | /**
133 | * 设置绘制完成清除路径的延迟时间
134 | *
135 | * @param freezeDuration 绘制完成清除路径的延迟时间(单位:ms)
136 | */
137 | public void setFreezeDuration(long freezeDuration) {
138 | this.freezeDuration = freezeDuration;
139 | }
140 |
141 | /**
142 | * 返回绘制完后是否自动清除路径(默认为true)
143 | */
144 | public boolean isEnableAutoClean() {
145 | return enableAutoClean;
146 | }
147 |
148 | /**
149 | * 设置绘制完成后是否自动清除路径
150 | *
151 | * @param enableAutoClean 绘制完成后是否自动清除路径(默认为true)
152 | */
153 | public void setEnableAutoClean(boolean enableAutoClean) {
154 | this.enableAutoClean = enableAutoClean;
155 | }
156 |
157 | /**
158 | * 返回是否开启跳过中间点模式(默认为false)
159 | */
160 | public boolean isEnableSkipMode() {
161 | return enableSkipMode;
162 | }
163 |
164 | /**
165 | * 设置是否开启跳过中间点模式
166 | *
167 | * @param enableSkipMode 是否开启跳过中间点模式(默认为false)
168 | */
169 | public void setEnableSkipMode(boolean enableSkipMode) {
170 | this.enableSkipMode = enableSkipMode;
171 | }
172 |
173 | /**
174 | * 返回是否开启绘制路径隐藏模式(默认为false)
175 | */
176 | public boolean isEnableInStealthMode() {
177 | return enableInStealthMode;
178 | }
179 |
180 | /**
181 | * 设置是否开启绘制路径隐藏模式(默认为false)
182 | *
183 | * @param enableInStealthMode 是否开启绘制路径隐藏模式(默认为false)
184 | */
185 | public void setEnableInStealthMode(boolean enableInStealthMode) {
186 | this.enableInStealthMode = enableInStealthMode;
187 | }
188 |
189 | /**
190 | * 返回是否开启触摸反馈(默认为false)
191 | */
192 | public boolean isEnableHapticFeedback() {
193 | return enableHapticFeedback;
194 | }
195 |
196 | /**
197 | * 设置是否开启触摸反馈
198 | *
199 | * @param enableHapticFeedback 是否开启触摸反馈(默认为false)
200 | */
201 | public void setEnableHapticFeedback(boolean enableHapticFeedback) {
202 | this.enableHapticFeedback = enableHapticFeedback;
203 | }
204 |
205 | /**
206 | * 返回绘制{@link Cell}样式
207 | *
208 | * @see ICellStyle
209 | * @see DefaultLockerCellStyle
210 | */
211 | public ICellStyle getCellStyle() {
212 | return cellStyle;
213 | }
214 |
215 | /**
216 | * 设置绘制{@link Cell}样式
217 | *
218 | * @param cellStyle 绘制{@link Cell}样式
219 | * @see ICellStyle
220 | * @see DefaultLockerCellStyle
221 | */
222 | public void setCellStyle(ICellStyle cellStyle) {
223 | this.cellStyle = cellStyle;
224 | }
225 |
226 | /**
227 | * 返回绘制连接线样式
228 | *
229 | * @see ILinkedLineStyle
230 | * @see DefaultLockerLinkedLineStyle
231 | */
232 | public ILinkedLineStyle getLinkedLineStyle() {
233 | return linkedLineStyle;
234 | }
235 |
236 | /**
237 | * 返回绘制连接线样式
238 | *
239 | * @param linkedLineStyle 绘制连接线样式
240 | * @see ILinkedLineStyle
241 | * @see DefaultLockerLinkedLineStyle
242 | */
243 | public void setLinkedLineStyle(ILinkedLineStyle linkedLineStyle) {
244 | this.linkedLineStyle = linkedLineStyle;
245 | }
246 |
247 | /**
248 | * 返回Style(颜色、描边等信息)
249 | */
250 | public DecoratorStyle getDecoratorStyle() {
251 | return decoratorStyle;
252 | }
253 |
254 | /**
255 | * 设置Style(颜色、描边等信息)
256 | *
257 | * @param decoratorStyle Style(颜色、描边等信息)
258 | */
259 | public void setDecoratorStyle(DecoratorStyle decoratorStyle) {
260 | this.decoratorStyle = decoratorStyle;
261 | }
262 |
263 | /**
264 | * 返回手势解锁监听
265 | */
266 | public OnPatternChangedListener getOnPatternChangedListener() {
267 | return mPatternChangedListener;
268 | }
269 |
270 | /**
271 | * 设置手势解锁监听
272 | *
273 | * @param listener 手势解锁监听
274 | */
275 | public void setOnPatternChangedListener(OnPatternChangedListener listener) {
276 | this.mPatternChangedListener = listener;
277 | }
278 |
279 | /**
280 | * 返回选中的{@link Cell}集合
281 | */
282 | public List getSelectedCells() {
283 | return mSelectedCells;
284 | }
285 |
286 | /**
287 | * 设置绘制状态
288 | *
289 | * @param status 绘制状态
290 | * @see CellStatus
291 | */
292 | public void setPatternStatus(@CellStatus int status) {
293 | for (Cell cell : mSelectedCells) {
294 | cell.setStatus(status);
295 | }
296 | if (status == CellStatus.NORMAL) {
297 | mSelectedCells.clear();
298 | }
299 | handleStealthMode(status);
300 | }
301 |
302 | @Override
303 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
304 | int measureSpec = Math.min(widthMeasureSpec, heightMeasureSpec);
305 | super.onMeasure(measureSpec, measureSpec);
306 | initCells();
307 | }
308 |
309 | @Override
310 | protected void onDraw(Canvas canvas) {
311 | super.onDraw(canvas);
312 | if (cellStyle != null && !mCells.isEmpty()) {
313 | cellStyle.draw(canvas, mCells);
314 | }
315 | if (linkedLineStyle != null && !mSelectedCells.isEmpty()) {
316 | linkedLineStyle.draw(canvas, mSelectedCells, endX, endY);
317 | }
318 | }
319 |
320 | @SuppressLint("ClickableViewAccessibility")
321 | @Override
322 | public boolean onTouchEvent(MotionEvent event) {
323 | if (!isEnabled()) {
324 | return super.onTouchEvent(event);
325 | }
326 | switch (event.getAction()) {
327 | case MotionEvent.ACTION_DOWN:
328 | handleActionDown(event);
329 | break;
330 | case MotionEvent.ACTION_MOVE:
331 | handleActionMove(event);
332 | break;
333 | case MotionEvent.ACTION_UP:
334 | handleActionUp();
335 | break;
336 | default:
337 | break;
338 | }
339 | return true;
340 | }
341 |
342 | @Override
343 | protected void onDetachedFromWindow() {
344 | super.onDetachedFromWindow();
345 | this.setOnPatternChangedListener(null);
346 | this.removeCallbacks(mClearPatternRunnable);
347 | }
348 |
349 | /**
350 | * init cells
351 | */
352 | private void initCells() {
353 | mCells.clear();
354 | mSelectedCells.clear();
355 | int offsetX = getPaddingLeft();
356 | int offsetY = getPaddingTop();
357 | int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
358 | int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
359 | mCells.addAll(PatternHelper.getCellList(side, width, height, offsetX, offsetY));
360 | }
361 |
362 | /**
363 | * handle {@link MotionEvent#ACTION_DOWN} event
364 | */
365 | private void handleActionDown(MotionEvent event) {
366 | // 1. reset to default state
367 | setPatternStatus(CellStatus.NORMAL);
368 | // 2. check selected cell
369 | checkSelectedCell(event);
370 | // 3. notify listener
371 | if (mPatternChangedListener != null) {
372 | mPatternChangedListener.onPatternStart();
373 | }
374 | }
375 |
376 | /**
377 | * handle {@link MotionEvent#ACTION_MOVE} event
378 | */
379 | private void handleActionMove(MotionEvent event) {
380 | // 1. check selected cell
381 | checkSelectedCell(event);
382 | // 2. update end point
383 | endX = event.getX();
384 | endY = event.getY();
385 | // 3. notify listener if needed
386 | int size = mSelectedCells.size();
387 | if (hitSize != size) {
388 | hitSize = size;
389 | if (mPatternChangedListener != null) {
390 | mPatternChangedListener.onPatternChange(this, mSelectedCells);
391 | }
392 | }
393 |
394 | }
395 |
396 | /**
397 | * handle {@link MotionEvent#ACTION_UP} event
398 | */
399 | private void handleActionUp() {
400 | // 1. set pattern status
401 | setPatternStatus(CellStatus.CHECK);
402 | // 2. update end point
403 | endX = 0f;
404 | endY = 0f;
405 | // 3. notify listener
406 | if (mPatternChangedListener != null) {
407 | mPatternChangedListener.onPatternComplete(this, mSelectedCells);
408 | }
409 | // 4. post clear pattern runnable if needed
410 | if (enableAutoClean && !mSelectedCells.isEmpty()) {
411 | postClearPatternRunnable();
412 | }
413 | }
414 |
415 | /**
416 | * clear pattern
417 | */
418 | private void clearPattern() {
419 | // 1. set pattern status
420 | setPatternStatus(CellStatus.NORMAL);
421 | // 2. invalidate view
422 | if (enableInStealthMode) {
423 | invalidate();
424 | }
425 | // 3. notify listener
426 | if (mPatternChangedListener != null) {
427 | mPatternChangedListener.onPatternClear();
428 | }
429 | }
430 |
431 | /**
432 | * post clear pattern runnable
433 | */
434 | private void postClearPatternRunnable() {
435 | removeCallbacks(mClearPatternRunnable);
436 | postDelayed(mClearPatternRunnable, freezeDuration);
437 | }
438 |
439 | /**
440 | * check user's touch moving is or not in the area of cells
441 | */
442 | private void checkSelectedCell(MotionEvent event) {
443 | for (Cell cell : mCells) {
444 | if (PatternHelper.isInRound(event.getX(), event.getY(), cell.getX(), cell.getY(), cell.getRadius())) {
445 | if (!enableSkipMode && !mSelectedCells.isEmpty()) {
446 | Cell last = mSelectedCells.get(mSelectedCells.size() - 1);
447 | if (last.getIndex() < cell.getIndex()) {
448 | for (int i = last.getIndex() + 1; i < cell.getIndex(); i++) {
449 | Cell center = mCells.get(i);
450 | if (PatternHelper.isInLine(last.getX(), last.getY(), center.getX(), center.getY(), cell.getX(), cell.getY())) {
451 | addSelectedCell(center);
452 | }
453 | }
454 | } else if (last.getIndex() > cell.getIndex()) {
455 | for (int i = last.getIndex() - 1; i > cell.getIndex(); i--) {
456 | Cell center = mCells.get(i);
457 | if (PatternHelper.isInLine(last.getX(), last.getY(), center.getX(), center.getY(), cell.getX(), cell.getY())) {
458 | addSelectedCell(center);
459 | }
460 | }
461 | }
462 | }
463 | addSelectedCell(cell);
464 | } else if (!enableInStealthMode) {
465 | invalidate();
466 | }
467 | }
468 | }
469 |
470 | /**
471 | * add selected cell
472 | */
473 | private void addSelectedCell(Cell cell) {
474 | if (!mSelectedCells.contains(cell)) {
475 | mSelectedCells.add(cell);
476 | handleHapticFeedback();
477 | }
478 | setPatternStatus(CellStatus.CHECK);
479 | }
480 |
481 | /**
482 | * handle the stealth mode (if true: do not post invalidate; false: post invalidate)
483 | */
484 | private void handleStealthMode(@CellStatus int status) {
485 | if (!enableInStealthMode || status == CellStatus.ERROR) {
486 | this.postInvalidate();
487 | }
488 | }
489 |
490 | /**
491 | * handle haptic feedback
492 | */
493 | private void handleHapticFeedback() {
494 | if (enableHapticFeedback) {
495 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
496 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING |
497 | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
498 | }
499 | }
500 |
501 | }
502 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/ArrowLockerLinkedLineStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 | import android.graphics.Path;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import com.henleylee.lockpattern.Cell;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 | import com.henleylee.lockpattern.PatternHelper;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * @author Henley
17 | * @since 2019/8/29 15:29
18 | */
19 | public class ArrowLockerLinkedLineStyle implements ILinkedLineStyle {
20 |
21 | private Paint paint;
22 | private Paint arrowPaint;
23 | private DecoratorStyle style;
24 |
25 | public ArrowLockerLinkedLineStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.STROKE);
29 | this.arrowPaint = new Paint();
30 | this.arrowPaint.setDither(true);
31 | this.arrowPaint.setAntiAlias(true);
32 | this.arrowPaint.setStyle(Paint.Style.FILL_AND_STROKE);
33 | }
34 |
35 | @Override
36 | public void draw(@NonNull Canvas canvas, List cells, float endX, float endY) {
37 | if (cells == null || cells.isEmpty()) {
38 | return;
39 | }
40 | int saveCount = canvas.save();
41 |
42 | paint.setColor(style.getColor(cells.get(0).getStatus()));
43 | paint.setStrokeWidth(style.getLineWidth());
44 | arrowPaint.setColor(style.getColor(cells.get(0).getStatus()));
45 | arrowPaint.setStrokeWidth(style.getLineWidth());
46 |
47 | Path path = new Path();
48 | for (int i = 1; i < cells.size(); i++) {
49 | Cell start = cells.get(i - 1);
50 | Cell end = cells.get(i);
51 | path.reset();
52 | path.moveTo(start.getX(), start.getY());
53 | path.lineTo(end.getX(), end.getY());
54 | canvas.drawPath(path, paint);
55 |
56 | path.reset();
57 | int arrowHeight = 8;
58 | double d = PatternHelper.distance(start.getX(), start.getY(), end.getX(), end.getY());
59 | float cosB = (float) ((end.getX() - start.getX()) / d);
60 | float sinB = (float) ((end.getY() - start.getY()) / d);
61 | float tanC = (float) Math.tan(Math.PI / 4);// 箭头尖锐程度,默认为直角
62 | float h = start.getRadius() / 2f + arrowHeight;
63 | float l = arrowHeight * tanC;
64 | float a = l * cosB;
65 | float b = l * sinB;
66 | float x0 = h * cosB;
67 | float y0 = h * sinB;
68 | float x1 = start.getX() + (h + arrowHeight) * cosB;
69 | float y1 = start.getY() + (h + arrowHeight) * sinB;
70 | float x2 = start.getX() + x0 - b;
71 | float y2 = start.getY() + y0 + a;
72 | float x3 = start.getX() + x0 + b;
73 | float y3 = start.getY() + y0 - a;
74 | path.moveTo(x1, y1);
75 | path.lineTo(x2, y2);
76 | path.lineTo(x3, y3);
77 | path.close();
78 | canvas.drawPath(path, arrowPaint);
79 | }
80 |
81 | if ((endX != 0f || endY != 0f) && cells.size() < 9) {
82 | path.reset();
83 | Cell cell = cells.get(cells.size() - 1);
84 | path.moveTo(cell.getX(), cell.getY());
85 | path.lineTo(endX, endY);
86 | canvas.drawPath(path, paint);
87 | }
88 |
89 | canvas.restoreToCount(saveCount);
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/DefaultIndicatorCellStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import com.henleylee.lockpattern.Cell;
9 | import com.henleylee.lockpattern.CellStatus;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * 指示器{@link Cell}默认绘制样式
16 | *
17 | * @author Henley
18 | * @since 2019/8/26 18:19
19 | */
20 | public class DefaultIndicatorCellStyle implements ICellStyle {
21 |
22 | private Paint paint;
23 | private DecoratorStyle style;
24 |
25 | public DefaultIndicatorCellStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.FILL);
29 | }
30 |
31 | @Override
32 | public void draw(@NonNull Canvas canvas, List cells) {
33 | if (cells == null || cells.isEmpty()) {
34 | return;
35 | }
36 |
37 | int saveCount = canvas.save();
38 |
39 | for (Cell cell : cells) {
40 | int status = cell.getStatus();
41 |
42 | // outer circle
43 | paint.setColor(style.getColor(status));
44 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius(), paint);
45 |
46 | // inner circle
47 | if (status == CellStatus.NORMAL) {
48 | paint.setColor(style.getFillColor());
49 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() - style.getStrokeWidth(), paint);
50 | }
51 | }
52 |
53 | canvas.restoreToCount(saveCount);
54 | }
55 |
56 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/DefaultIndicatorLinkedLineStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 | import android.graphics.Path;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import com.henleylee.lockpattern.Cell;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * 指示器连接线默认绘制样式
16 | *
17 | * @author Henley
18 | * @since 2019/8/26 18:11
19 | */
20 | public class DefaultIndicatorLinkedLineStyle implements ILinkedLineStyle {
21 |
22 | private Paint paint;
23 | private DecoratorStyle style;
24 |
25 | public DefaultIndicatorLinkedLineStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.STROKE);
29 | }
30 |
31 | @Override
32 | public void draw(@NonNull Canvas canvas, List cells, float endX, float endY) {
33 | if (cells == null || cells.isEmpty()) {
34 | return;
35 | }
36 | int saveCount = canvas.save();
37 | Path path = new Path();
38 | for (Cell cell : cells) {
39 | if (path.isEmpty()) {
40 | path.moveTo(cell.getX(), cell.getY());
41 | } else {
42 | path.lineTo(cell.getX(), cell.getY());
43 | }
44 | }
45 |
46 | paint.setColor(style.getColor(cells.get(0).getStatus()));
47 | paint.setStrokeWidth(style.getLineWidth());
48 | canvas.drawPath(path, paint);
49 |
50 | canvas.restoreToCount(saveCount);
51 | }
52 |
53 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/DefaultLockerCellStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import com.henleylee.lockpattern.Cell;
9 | import com.henleylee.lockpattern.CellStatus;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * 手势解锁{@link Cell}默认绘制样式
16 | *
17 | * @author Henley
18 | * @since 2019/8/26 19:39
19 | */
20 | public class DefaultLockerCellStyle implements ICellStyle {
21 |
22 | private Paint paint;
23 | private DecoratorStyle style;
24 |
25 | public DefaultLockerCellStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.FILL);
29 | }
30 |
31 | @Override
32 | public void draw(@NonNull Canvas canvas, List cells) {
33 | if (cells == null || cells.isEmpty()) {
34 | return;
35 | }
36 |
37 | int saveCount = canvas.save();
38 |
39 | for (Cell cell : cells) {
40 | int status = cell.getStatus();
41 |
42 | // draw outer circle
43 | paint.setColor(style.getColor(status));
44 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius(), paint);
45 |
46 | // draw fill circle
47 | paint.setColor(style.getFillColor());
48 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() - style.getStrokeWidth(), paint);
49 |
50 | // draw inner circle
51 | if (status != CellStatus.NORMAL) {
52 | paint.setColor(style.getColor(status));
53 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() / 3f, paint);
54 | }
55 | }
56 |
57 | canvas.restoreToCount(saveCount);
58 | }
59 |
60 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/DefaultLockerLinkedLineStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 | import android.graphics.Path;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import com.henleylee.lockpattern.Cell;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * 手势解锁连接线默认绘制样式
16 | *
17 | * @author Henley
18 | * @since 2019/8/26 18:26
19 | */
20 | public class DefaultLockerLinkedLineStyle implements ILinkedLineStyle {
21 |
22 | private Paint paint;
23 | private DecoratorStyle style;
24 |
25 | public DefaultLockerLinkedLineStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.STROKE);
29 | }
30 |
31 |
32 | @Override
33 | public void draw(@NonNull Canvas canvas, List cells, float endX, float endY) {
34 | if (cells == null || cells.isEmpty()) {
35 | return;
36 | }
37 | int saveCount = canvas.save();
38 | Path path = new Path();
39 | for (Cell cell : cells) {
40 | if (path.isEmpty()) {
41 | path.moveTo(cell.getX(), cell.getY());
42 | } else {
43 | path.lineTo(cell.getX(), cell.getY());
44 | }
45 | }
46 |
47 | if ((endX != 0f || endY != 0f) && cells.size() < 9) {
48 | path.lineTo(endX, endY);
49 | }
50 |
51 | paint.setColor(style.getColor(cells.get(0).getStatus()));
52 | paint.setStrokeWidth(style.getLineWidth());
53 | canvas.drawPath(path, paint);
54 |
55 | canvas.restoreToCount(saveCount);
56 | }
57 |
58 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/ICellStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | import com.henleylee.lockpattern.Cell;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * {@link Cell}绘制样式抽象接口
13 | *
14 | * @author Henley
15 | * @since 2019/8/26 16:53
16 | */
17 | public interface ICellStyle {
18 |
19 | /**
20 | * 绘制正常情况下(即未设置的)每个图案的样式
21 | *
22 | * @param canvas
23 | * @param cells the target cell view
24 | */
25 | void draw(@NonNull Canvas canvas, List cells);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/ILinkedLineStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | import com.henleylee.lockpattern.Cell;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * {@link Cell}连接线绘制样式抽象接口
13 | *
14 | * @author Henley
15 | * @since 2019/8/26 16:51
16 | */
17 | public interface ILinkedLineStyle {
18 |
19 | /**
20 | * 绘制图案密码连接线
21 | *
22 | * @param canvas
23 | * @param cells
24 | * @param endX
25 | * @param endY
26 | */
27 | void draw(@NonNull Canvas canvas, List cells, float endX, float endY);
28 |
29 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/RippleLockerCellStyle.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import com.henleylee.lockpattern.Cell;
9 | import com.henleylee.lockpattern.CellStatus;
10 | import com.henleylee.lockpattern.DecoratorStyle;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * 手势解锁{@link Cell}水波纹绘制样式
16 | *
17 | * @author Henley
18 | * @since 2019/8/29 16:15
19 | */
20 | public class RippleLockerCellStyle implements ICellStyle {
21 |
22 | private Paint paint;
23 | private DecoratorStyle style;
24 |
25 | public RippleLockerCellStyle(DecoratorStyle style) {
26 | this.style = style;
27 | this.paint = StyleHelper.createPaint();
28 | this.paint.setStyle(Paint.Style.FILL);
29 | }
30 |
31 | @Override
32 | public void draw(@NonNull Canvas canvas, List cells) {
33 | if (cells == null || cells.isEmpty()) {
34 | return;
35 | }
36 |
37 | int saveCount = canvas.save();
38 |
39 | for (Cell cell : cells) {
40 | int status = cell.getStatus();
41 |
42 | if (status == CellStatus.NORMAL) {
43 | // draw outer circle
44 | paint.setColor(style.getColor(status));
45 | paint.setStrokeWidth(style.getLineWidth() / 2f);
46 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius(), paint);
47 |
48 | // draw fill circle
49 | paint.setColor(style.getFillColor());
50 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() - style.getLineWidth() / 2f, paint);
51 | } else {
52 | // draw outer circle
53 | paint.setColor(style.getColor(status) & 0x14FFFFFF);
54 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius(), paint);
55 |
56 | // draw inner circle
57 | paint.setColor(style.getColor(status) & 0x43FFFFFF);
58 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() * 2f / 3f, paint);
59 |
60 | // draw inner circle
61 | paint.setColor(style.getColor(status));
62 | canvas.drawCircle(cell.getX(), cell.getY(), cell.getRadius() / 3f, paint);
63 | }
64 | }
65 |
66 | canvas.restoreToCount(saveCount);
67 | }
68 |
69 | }
--------------------------------------------------------------------------------
/lockpattern/src/main/java/com/henleylee/lockpattern/style/StyleHelper.java:
--------------------------------------------------------------------------------
1 | package com.henleylee.lockpattern.style;
2 |
3 | import android.graphics.Paint;
4 |
5 | /**
6 | * 绘制样式辅助类
7 | *
8 | * @author Henley
9 | * @since 2019/8/26 16:50
10 | */
11 | class StyleHelper {
12 |
13 | static Paint createPaint() {
14 | Paint paint = new Paint();
15 | paint.setDither(true);
16 | paint.setAntiAlias(true);
17 | paint.setStrokeCap(Paint.Cap.ROUND);
18 | paint.setStrokeJoin(Paint.Join.ROUND);
19 | return paint;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/lockpattern/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/lockpattern/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LockPattern
3 |
4 |
--------------------------------------------------------------------------------
/screenshots/pattern_check.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/screenshots/pattern_check.png
--------------------------------------------------------------------------------
/screenshots/pattern_check_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/screenshots/pattern_check_error.png
--------------------------------------------------------------------------------
/screenshots/pattern_create.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/screenshots/pattern_create.png
--------------------------------------------------------------------------------
/screenshots/pattern_create_confirm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/screenshots/pattern_create_confirm.png
--------------------------------------------------------------------------------
/screenshots/pattern_setting.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SnowStar0423/LockPattern/8fad02b30e6bebdc429c21909707e73d244aadab/screenshots/pattern_setting.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | include ':lockpattern'
3 | rootProject.name = 'LockPattern'
4 |
--------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | |