├── gesturelock
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── sevenheaven
│ │ │ └── gesturelock
│ │ │ ├── GestureLockView.java
│ │ │ └── GestureLock.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── sevenheaven
│ │ │ └── gesturelock
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── sevenheaven
│ │ └── gesturelock
│ │ └── ApplicationTest.java
├── proguard-rules.pro
├── build.gradle
└── bintray.gradle
├── settings.gradle
├── art
├── art1.png
├── art10.png
├── art11.png
├── art12.png
├── art2.png
├── art3.png
├── art4.png
├── art5.png
├── art6.png
├── art7.png
├── art8.png
└── art9.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── example
├── src
│ └── main
│ │ ├── res
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values-sw600dp
│ │ │ └── dimens.xml
│ │ ├── values
│ │ │ ├── dimens.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ ├── menu
│ │ │ └── main.xml
│ │ ├── values-sw720dp-land
│ │ │ └── dimens.xml
│ │ ├── values-v11
│ │ │ └── styles.xml
│ │ ├── values-v14
│ │ │ └── styles.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── gesturelock
│ │ ├── MainActivity.java
│ │ └── widget
│ │ ├── NexusStyleLockView.java
│ │ └── MyStyleLockView.java
└── build.gradle
├── .gitignore
├── LICENSE
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/gesturelock/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':example', ':gesturelock'
2 |
--------------------------------------------------------------------------------
/art/art1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art1.png
--------------------------------------------------------------------------------
/art/art10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art10.png
--------------------------------------------------------------------------------
/art/art11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art11.png
--------------------------------------------------------------------------------
/art/art12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art12.png
--------------------------------------------------------------------------------
/art/art2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art2.png
--------------------------------------------------------------------------------
/art/art3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art3.png
--------------------------------------------------------------------------------
/art/art4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art4.png
--------------------------------------------------------------------------------
/art/art5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art5.png
--------------------------------------------------------------------------------
/art/art6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art6.png
--------------------------------------------------------------------------------
/art/art7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art7.png
--------------------------------------------------------------------------------
/art/art8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art8.png
--------------------------------------------------------------------------------
/art/art9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/art/art9.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gesturelock/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | GestureLock
3 |
4 |
--------------------------------------------------------------------------------
/example/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/example/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/example/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/example/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/7heaven/GestureLock/HEAD/example/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/gesturelock/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.classpath
2 | *.project
3 | *.txt
4 | *.swp
5 | .DS_Store
6 | .idea/
7 | .gradle/
8 | build/
9 | app/build/
10 | *.iml
11 | app/app.iml
12 | local.properties
13 |
--------------------------------------------------------------------------------
/example/src/main/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/example/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | gesturelock
5 | Settings
6 | Hello world!
7 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jun 20 11:38:51 CST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/example/src/main/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/example/src/main/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/gesturelock/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/example/src/main/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/gesturelock/src/test/java/com/sevenheaven/gesturelock/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sevenheaven.gesturelock;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/example/src/main/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/gesturelock/src/androidTest/java/com/sevenheaven/gesturelock/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.sevenheaven.gesturelock;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2016 7heaven
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 22
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.example.gesturelock"
9 | minSdkVersion 8
10 | targetSdkVersion 22
11 | }
12 |
13 |
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile 'com.android.support:support-v4:22.1.0'
24 | compile project(':gesturelock')
25 | }
26 |
--------------------------------------------------------------------------------
/gesturelock/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/caifangmao/Documents/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/gesturelock/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion rootProject.ext.minSdkVersion
9 | targetSdkVersion rootProject.ext.targetSdkVersion
10 | versionCode rootProject.ext.versionCode
11 | versionName rootProject.ext.versionName
12 | }
13 |
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | }
26 |
27 | apply from: 'bintray.gradle'
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true%
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
12 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/gesturelock/bintray.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 | apply plugin: 'com.jfrog.bintray'
3 |
4 | Properties bintraySProperties = new Properties()
5 | File f = new File("local.properties")
6 | if(f.exists()) {
7 | bintraySProperties.load(project.rootProject.file('local.properties').newDataInputStream())
8 | }
9 |
10 | group = rootProject.ext.pomGroup
11 | version = rootProject.ext.versionName
12 |
13 | install {
14 | repositories.mavenInstaller {
15 | // This generates POM.xml with proper parameters
16 | pom {
17 | project {
18 | packaging rootProject.ext.pomPackaging
19 | name rootProject.ext.pomName
20 | url rootProject.ext.pomSiteUrl
21 |
22 | licenses {
23 | license {
24 | name rootProject.ext.pomLicenseName
25 | url rootProject.ext.pomLicenseUrl
26 | }
27 | }
28 | developers {
29 | developer {
30 | id bintraySProperties.getProperty("bintray.id")
31 | name bintraySProperties.getProperty("bintray.name")
32 | email bintraySProperties.getProperty("bintray.email")
33 | }
34 | }
35 |
36 | scm {
37 | connection rootProject.ext.pomGitUrl
38 | developerConnection rootProject.ext.pomGitUrl
39 | url rootProject.ext.pomSiteUrl
40 | }
41 |
42 | }
43 | }
44 | }
45 | }
46 |
47 | task sourcesJar(type: Jar) {
48 | from android.sourceSets.main.java.srcDirs
49 | classifier = 'sources'
50 | }
51 |
52 | task javadoc(type: Javadoc){
53 | source = android.sourceSets.main.java.srcDirs
54 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
55 | }
56 |
57 | task javadocJar(type: Jar, dependsOn: javadoc) {
58 | classifier = 'javadoc'
59 | from javadoc.destinationDir
60 | }
61 |
62 | artifacts {
63 | archives javadocJar
64 | archives sourcesJar
65 | }
66 |
67 | bintray {
68 | user = bintraySProperties.getProperty("bintray.user")
69 | key = bintraySProperties.getProperty("bintray.apikey")
70 | configurations = ['archives']
71 | pkg {
72 | repo = "maven"
73 | name = rootProject.ext.pomName
74 | websiteUrl = rootProject.ext.pomSiteUrl
75 | vcsUrl = rootProject.ext.pomGitUrl
76 | licenses = [rootProject.ext.pomLicenseShort]
77 | publish = true
78 | }
79 | }
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/example/src/main/java/com/example/gesturelock/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.gesturelock;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.view.Menu;
8 | import android.widget.Toast;
9 |
10 | import com.example.gesturelock.widget.MyStyleLockView;
11 | import com.example.gesturelock.widget.NexusStyleLockView;
12 | import com.sevenheaven.gesturelock.GestureLock;
13 | import com.sevenheaven.gesturelock.GestureLockView;
14 |
15 | public class MainActivity extends Activity {
16 |
17 | private GestureLock gestureView;
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 |
24 | gestureView = (GestureLock) findViewById(R.id.gesture_lock);
25 | gestureView.setAdapter(new GestureLock.GestureLockAdapter() {
26 | @Override
27 | public int getDepth() {
28 | return 7;
29 | }
30 |
31 | @Override
32 | public int[] getCorrectGestures() {
33 | return new int[]{0, 3, 6, 7, 8, 5, 2, 1, 4};
34 | }
35 |
36 | @Override
37 | public int getUnmatchedBoundary() {
38 | return 5;
39 | }
40 |
41 | @Override
42 | public int getBlockGapSize(){
43 | return 10;
44 | }
45 |
46 | @Override
47 | public GestureLockView getGestureLockViewInstance(Context context, int position) {
48 | if(position % 2 == 0){
49 | return new MyStyleLockView(context);
50 | }else{
51 | return new NexusStyleLockView(context);
52 | }
53 | }
54 | });
55 |
56 | gestureView.setOnGestureEventListener(new GestureLock.OnGestureEventListener(){
57 |
58 | @Override
59 | public void onGestureEvent(boolean matched) {
60 | Toast.makeText(MainActivity.this, "Match:" + matched, Toast.LENGTH_SHORT).show();
61 | gestureView.clear();
62 | }
63 |
64 | @Override
65 | public void onUnmatchedExceedBoundary() {
66 | Toast.makeText(MainActivity.this, "输入5次错误!30秒后才能输入", Toast.LENGTH_SHORT).show();
67 | }
68 |
69 | @Override
70 | public void onBlockSelected(int position) {
71 | Log.d("position", position + "");
72 | }
73 |
74 | });
75 | }
76 |
77 | @Override
78 | public boolean onCreateOptionsMenu(Menu menu) {
79 | getMenuInflater().inflate(R.menu.main, menu);
80 | return true;
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/gesturelock/src/main/java/com/sevenheaven/gesturelock/GestureLockView.java:
--------------------------------------------------------------------------------
1 | package com.sevenheaven.gesturelock;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 |
9 | /**
10 | * Abstract class for user to customize the GestureLock block style
11 | */
12 | public abstract class GestureLockView extends View {
13 |
14 | private static final boolean DEBUG = false;
15 |
16 | private int mWidth, mHeight;
17 | private int mCenterX, mCenterY;
18 |
19 | private Paint mPaint;
20 |
21 | public enum LockerState{
22 | LOCKER_STATE_NORMAL, LOCKER_STATE_ERROR, LOCKER_STATE_SELECTED
23 | }
24 |
25 | private LockerState mState = LockerState.LOCKER_STATE_NORMAL;
26 |
27 | private int errorArrow = -1;
28 |
29 | public GestureLockView(Context context){
30 | this(context, null);
31 | }
32 |
33 | public GestureLockView(Context context, AttributeSet attrs){
34 | this(context, attrs, 0);
35 | }
36 |
37 | public GestureLockView(Context context, AttributeSet attrs, int defStyle){
38 | super(context, attrs, defStyle);
39 |
40 | }
41 |
42 | @Override
43 | protected void onSizeChanged(int w, int h, int oldw, int oldh){
44 |
45 | mWidth = w;
46 | mHeight = h;
47 |
48 | mCenterX = mWidth / 2;
49 | mCenterY = mHeight / 2;
50 | }
51 |
52 | public void setLockerState(LockerState state){
53 | mState = state;
54 | invalidate();
55 | }
56 |
57 | public LockerState getLockerState(){
58 | return mState;
59 | }
60 |
61 | public void setArrow(int arrow){
62 | errorArrow = arrow;
63 |
64 | invalidate();
65 | }
66 |
67 | public int getArrow(){
68 | return errorArrow;
69 | }
70 |
71 | /**
72 | * override this method to do GestureLock block drawing work
73 | * @param state current state of this GustureLock block
74 | * @param canvas
75 | */
76 | protected abstract void doDraw(LockerState state, Canvas canvas);
77 |
78 | /**
79 | * override this method to do Arrow drawing, you should notice
80 | * @param canvas
81 | */
82 | protected abstract void doArrowDraw(Canvas canvas);
83 |
84 | @Override
85 | public void onDraw(Canvas canvas){
86 |
87 | if(DEBUG){
88 | if(mPaint == null){
89 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
90 | }
91 | mPaint.setColor(0xFFFF0000);
92 | mPaint.setTextSize(20);
93 | canvas.drawText(getId() + "", 0, getHeight(), mPaint);
94 | }
95 |
96 | doDraw(mState, canvas);
97 |
98 | if(errorArrow != -1){
99 |
100 | canvas.save();
101 | canvas.rotate(errorArrow, mCenterX, mCenterY);
102 | doArrowDraw(canvas);
103 |
104 | canvas.restore();
105 | }
106 |
107 | }
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/example/src/main/java/com/example/gesturelock/widget/NexusStyleLockView.java:
--------------------------------------------------------------------------------
1 | package com.example.gesturelock.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.graphics.Path;
7 | import android.util.AttributeSet;
8 |
9 | import com.sevenheaven.gesturelock.GestureLockView;
10 |
11 | /**
12 | * Created by caifangmao on 1/27/16.
13 | */
14 | public class NexusStyleLockView extends GestureLockView {
15 |
16 | private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
17 |
18 | private int mCenterX, mCenterY;
19 | private int mWidth, mHeight;
20 | private int mRadius;
21 |
22 | private static final int COLOR_NORMAL = 0xFFFFFFFF;
23 | private static final int COLOR_ERROR = 0xFFFF0000;
24 |
25 | private float innerRate = 0.2F;
26 | private float outerWidthRate = 0.15F;
27 | private float outerRate = 0.91F;
28 |
29 | private float arrowRate = 0.3F;
30 | private float arrowDistanceRate = 0.65F;
31 | private int arrowDistance;
32 |
33 | private Path arrow;
34 |
35 | public NexusStyleLockView(Context context){
36 | this(context, null);
37 | }
38 |
39 | public NexusStyleLockView(Context context, AttributeSet attrs){
40 | this(context, attrs, 0);
41 | }
42 |
43 | public NexusStyleLockView(Context context, AttributeSet attrs, int defStyle){
44 | super(context, attrs, defStyle);
45 |
46 | arrow = new Path();
47 | }
48 |
49 | @Override
50 | protected void onSizeChanged(int w, int h, int oldw, int oldh){
51 | super.onSizeChanged(w, h, oldw, oldh);
52 |
53 | mWidth = w;
54 | mHeight = h;
55 |
56 | mCenterX = w / 2;
57 | mCenterY = h / 2;
58 |
59 | mRadius = w > h ? h : w;
60 | mRadius /= 2;
61 |
62 | arrowDistance = (int) (mRadius * arrowDistanceRate);
63 |
64 | int length = (int) (mRadius * arrowRate);
65 | arrow.reset();
66 | arrow.moveTo(mCenterX - length, mCenterY + length - arrowDistance);
67 | arrow.lineTo(mCenterX, mCenterY - arrowDistance);
68 | arrow.lineTo(mCenterX + length, mCenterY + length - arrowDistance);
69 | arrow.close();
70 | }
71 |
72 | @Override
73 | protected void doArrowDraw(Canvas canvas){
74 | mPaint.setStyle(Paint.Style.FILL);
75 | mPaint.setColor(COLOR_ERROR);
76 | canvas.drawPath(arrow, mPaint);
77 | }
78 |
79 | @Override
80 | protected void doDraw(LockerState state, Canvas canvas){
81 | switch(state){
82 | case LOCKER_STATE_NORMAL:
83 | mPaint.setStyle(Paint.Style.FILL);
84 | mPaint.setColor(COLOR_NORMAL);
85 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
86 | break;
87 | case LOCKER_STATE_SELECTED:
88 | mPaint.setStyle(Paint.Style.STROKE);
89 | mPaint.setColor(COLOR_NORMAL);
90 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
91 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
92 | mPaint.setStrokeWidth(2);
93 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
94 | break;
95 | case LOCKER_STATE_ERROR:
96 | mPaint.setStyle(Paint.Style.STROKE);
97 | mPaint.setColor(COLOR_ERROR);
98 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
99 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
100 | mPaint.setStrokeWidth(2);
101 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
102 | break;
103 | }
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 | [  ](https://bintray.com/7heaven/maven/GestureLock/_latestVersion)
3 |
4 | # GestureLock
5 |
6 | ### GestureLock是一个可以自定义数量、自定义样式的手势解锁控件
7 |
8 | 
9 | 
10 |
11 | ## 用法
12 |
13 | ### gradle:
14 |
15 | ```
16 | compile 'com.7heaven.widgets:gesturelock:1.3'
17 | ```
18 |
19 | 使用GestureLock类提供的Adapter来定制样式
20 |
21 | ```java
22 | gestureView.setAdapter(new GestureLock.GestureLockAdapter() {
23 |
24 | @Override
25 | public int getDepth() {
26 | return 7;
27 | }
28 |
29 | @Override
30 | public int[] getCorrectGestures() {
31 | return new int[]{1, 2, 3, 4};
32 | }
33 |
34 | @Override
35 | public int getUnmatchedBoundary() {
36 | return 5;
37 | }
38 |
39 | @Override
40 | public int getBlockGapSize(){
41 | return 10;
42 | }
43 |
44 | @Override
45 | public GestureLockView getGestureLockViewInstance(Context context, int position) {
46 | return new NexusStyleLockView(context);
47 | }
48 | });
49 |
50 | ```
51 |
52 |
53 | ```getDepth() 手势解锁的宽高数量```
54 |
55 | ```getCorrectGestures() 正确的解锁手势```
56 |
57 | ```getUnmatchedBoundary() 最大可重试次数```
58 |
59 | ```getBlockGapSize() block之前的间隔大小```
60 |
61 | ```getGestureLockViewInstance(Context context, int position) block的样式```
62 |
63 |
64 | 继承GestureLockView来实现自定义样式的block
65 |
66 | * 重写doArrowDraw绘制箭头(箭头角度0的时候为向上)
67 | * 重写onDraw实现Block内容样式的绘制
68 |
69 | Block分为三种状态
70 |
71 | * LockerState.LOCKER_STATE_NORMAL `正常状态`
72 | * LockerState.LOCKER_STATE_SELECTED `选中状态`
73 | * LockerState.LOCKER_STATE_ERROR `错误状态`
74 |
75 |
76 | ```java
77 |
78 | @Override
79 | protected void doArrowDraw(Canvas canvas){
80 | mPaint.setStyle(Paint.Style.FILL);
81 | mPaint.setColor(COLOR_ERROR);
82 | canvas.drawPath(arrow, mPaint);
83 | }
84 |
85 | @Override
86 | protected void doDraw(LockerState state, Canvas canvas){
87 | switch(state){
88 | case LOCKER_STATE_NORMAL:
89 | mPaint.setStyle(Paint.Style.FILL);
90 | mPaint.setColor(COLOR_NORMAL);
91 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
92 | break;
93 | case LOCKER_STATE_SELECTED:
94 | mPaint.setStyle(Paint.Style.STROKE);
95 | mPaint.setColor(COLOR_NORMAL);
96 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
97 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
98 | mPaint.setStrokeWidth(2);
99 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
100 | break;
101 | case LOCKER_STATE_ERROR:
102 | mPaint.setStyle(Paint.Style.STROKE);
103 | mPaint.setColor(COLOR_ERROR);
104 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
105 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
106 | mPaint.setStrokeWidth(2);
107 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
108 | break;
109 | }
110 | }
111 |
112 | ```
113 |
114 |
115 | 
116 | 
117 | 
118 | 
119 | 
120 | 
121 | 
122 | 
123 | 
124 | 
125 |
--------------------------------------------------------------------------------
/example/src/main/java/com/example/gesturelock/widget/MyStyleLockView.java:
--------------------------------------------------------------------------------
1 | package com.example.gesturelock.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.graphics.Path;
7 | import android.graphics.RectF;
8 | import android.util.AttributeSet;
9 |
10 | import com.sevenheaven.gesturelock.GestureLockView;
11 |
12 | /**
13 | * Created by caifangmao on 1/28/16.
14 | */
15 | public class MyStyleLockView extends GestureLockView {
16 |
17 | private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
18 |
19 | private int mCenterX, mCenterY;
20 | private int mWidth, mHeight;
21 |
22 | private int mRadius;
23 |
24 | private static final int COLOR_NORMAL = 0xFFFFFFFF;
25 | private static final int COLOR_SELECTED = 0xFF0099CC;
26 | private static final int COLOR_ERROR = 0xFFFF0000;
27 |
28 | private static final int QUAD_ANGLE = 84;
29 |
30 | private float innerRate = 0.2F;
31 | private float outerWidthRate = 0.05F;
32 | private float middleWidthRate = 0.1F;
33 | private float innerWidthRate = 0.08F;
34 | private float outerRate = 0.91F;
35 | private float middleRate = 0.8F;
36 | private float arrowRate = 0.3F;
37 | private float arrowDistanceRate = 0.65F;
38 | private int arrowDistance;
39 |
40 | private RectF middleOval;
41 |
42 | private Path arrow;
43 |
44 | public MyStyleLockView(Context context){
45 | this(context, null);
46 | }
47 |
48 | public MyStyleLockView(Context context, AttributeSet attrs){
49 | this(context, attrs, 0);
50 | }
51 |
52 | public MyStyleLockView(Context context, AttributeSet attrs, int defStyle){
53 | super(context, attrs, defStyle);
54 |
55 | arrow = new Path();
56 | middleOval = new RectF();
57 | }
58 |
59 | @Override
60 | protected void onSizeChanged(int w, int h, int oldw, int oldh){
61 | super.onSizeChanged(w, h, oldw, oldh);
62 |
63 | mWidth = w;
64 | mHeight = h;
65 |
66 | mCenterX = mWidth / 2;
67 | mCenterY = mHeight / 2;
68 |
69 | mRadius = mCenterX > mCenterY ? mCenterY : mCenterX;
70 |
71 | float r = mRadius * middleRate;
72 | middleOval.left = mCenterX - r;
73 | middleOval.right = mCenterX + r;
74 | middleOval.top = mCenterY - r;
75 | middleOval.bottom = mCenterY + r;
76 |
77 | arrowDistance = (int) (mRadius * arrowDistanceRate);
78 |
79 | int length = (int) (mRadius * arrowRate);
80 | arrow.reset();
81 | arrow.moveTo(mCenterX - length, mCenterY + length - arrowDistance);
82 | arrow.lineTo(mCenterX, mCenterY - arrowDistance);
83 | arrow.lineTo(mCenterX + length, mCenterY + length - arrowDistance);
84 | arrow.close();
85 | }
86 |
87 | @Override
88 | protected void doDraw(LockerState state, Canvas canvas){
89 | switch(state){
90 | case LOCKER_STATE_NORMAL:
91 | mPaint.setStyle(Paint.Style.STROKE);
92 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
93 | mPaint.setColor(COLOR_NORMAL);
94 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
95 | break;
96 | case LOCKER_STATE_SELECTED:
97 | mPaint.setStyle(Paint.Style.STROKE);
98 | mPaint.setColor(COLOR_SELECTED);
99 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
100 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
101 | mPaint.setStrokeWidth(mRadius * innerWidthRate);
102 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
103 |
104 | mPaint.setColor(0x600099CC);
105 | mPaint.setStrokeWidth(mRadius * middleWidthRate);
106 | canvas.drawArc(middleOval, 3, QUAD_ANGLE, false, mPaint);
107 | canvas.drawArc(middleOval, 93, QUAD_ANGLE, false, mPaint);
108 | canvas.drawArc(middleOval, 183, QUAD_ANGLE, false, mPaint);
109 | canvas.drawArc(middleOval, 273, QUAD_ANGLE, false, mPaint);
110 |
111 | mPaint.setStyle(Paint.Style.FILL);
112 | mPaint.setColor(COLOR_SELECTED & 0xFFFFFF | 0xA0000000);
113 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
114 | break;
115 | case LOCKER_STATE_ERROR:
116 | mPaint.setStyle(Paint.Style.STROKE);
117 | mPaint.setColor(COLOR_ERROR);
118 | mPaint.setStrokeWidth(mRadius * outerWidthRate);
119 | canvas.drawCircle(mCenterX, mCenterY, mRadius * outerRate, mPaint);
120 | mPaint.setStrokeWidth(mRadius * innerWidthRate);
121 | canvas.drawCircle(mCenterX, mCenterY, mRadius * innerRate, mPaint);
122 |
123 | mPaint.setColor(0x600099CC);
124 | mPaint.setStrokeWidth(mRadius * middleWidthRate);
125 | canvas.drawArc(middleOval, 3, QUAD_ANGLE, false, mPaint);
126 | canvas.drawArc(middleOval, 93, QUAD_ANGLE, false, mPaint);
127 | canvas.drawArc(middleOval, 183, QUAD_ANGLE, false, mPaint);
128 | canvas.drawArc(middleOval, 273, QUAD_ANGLE, false, mPaint);
129 |
130 | break;
131 | }
132 | }
133 |
134 | @Override
135 | protected void doArrowDraw(Canvas canvas){
136 | mPaint.setStyle(Paint.Style.FILL);
137 | mPaint.setColor(COLOR_ERROR);
138 | canvas.drawPath(arrow, mPaint);
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >/dev/null
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >/dev/null
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 | JAVACMD=`cygpath --unix "$JAVACMD"`
118 |
119 | # We build the pattern for arguments to be converted via cygpath
120 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
121 | SEP=""
122 | for dir in $ROOTDIRSRAW ; do
123 | ROOTDIRS="$ROOTDIRS$SEP$dir"
124 | SEP="|"
125 | done
126 | OURCYGPATTERN="(^($ROOTDIRS))"
127 | # Add a user-defined pattern to the cygpath arguments
128 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
129 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
130 | fi
131 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
132 | i=0
133 | for arg in "$@" ; do
134 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
135 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
136 |
137 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
138 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
139 | else
140 | eval `echo args$i`="\"$arg\""
141 | fi
142 | i=$((i+1))
143 | done
144 | case $i in
145 | (0) set -- ;;
146 | (1) set -- "$args0" ;;
147 | (2) set -- "$args0" "$args1" ;;
148 | (3) set -- "$args0" "$args1" "$args2" ;;
149 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
150 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
151 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
152 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
153 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
154 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
155 | esac
156 | fi
157 |
158 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
159 | function splitJvmOpts() {
160 | JVM_OPTS=("$@")
161 | }
162 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
163 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
164 |
165 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
166 |
--------------------------------------------------------------------------------
/gesturelock/src/main/java/com/sevenheaven/gesturelock/GestureLock.java:
--------------------------------------------------------------------------------
1 | package com.sevenheaven.gesturelock;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.Paint;
7 | import android.graphics.Path;
8 | import android.util.AttributeSet;
9 | import android.util.TypedValue;
10 | import android.view.MotionEvent;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 |
14 | public class GestureLock extends ViewGroup {
15 |
16 | public static final int MODE_NORMAL = 0;
17 | public static final int MODE_EDIT = 1;
18 |
19 | private int mode = MODE_NORMAL;
20 |
21 | private int depth = 3;
22 |
23 | private int mWidth, mHeight;
24 | private int mCenterX, mCenterY;
25 |
26 | private int[] defaultGestures = new int[]{0};
27 | private int[] negativeGestures;
28 |
29 | private int[] gesturesContainer;
30 | private int gestureCursor = 0;
31 |
32 | private Path gesturePath;
33 |
34 | private int lastX;
35 | private int lastY;
36 | private int lastPathX;
37 | private int lastPathY;
38 |
39 | private static final int MAX_BLOCK_SIZE = 200;
40 |
41 | private int mContentSize;
42 | private int mHalfContentSize;
43 |
44 | private Paint paint;
45 |
46 | private int unmatchedCount;
47 | private int unmatchedBoundary = 5;
48 |
49 | private boolean touchable;
50 |
51 | private int DEFAULT_ERROR_COLOR = 0x66FF0000;
52 | private int DEFAULT_COLOR = 0x66FFFFFF;
53 | private int mCustomColor;
54 | private int mCustomErrorColor;
55 |
56 | private OnGestureEventListener onGestureEventListener;
57 | private GestureLockAdapter mAdapter;
58 |
59 | public interface OnGestureEventListener{
60 | void onBlockSelected(int position);
61 | void onGestureEvent(boolean matched);
62 | void onUnmatchedExceedBoundary();
63 | }
64 |
65 | /**
66 | * GestureLockAdapter provide a way to customize the depth, correct gestures and gesture locker style
67 | */
68 | public interface GestureLockAdapter{
69 | int getDepth();
70 | int[] getCorrectGestures();
71 | int getUnmatchedBoundary();
72 | int getBlockGapSize();
73 | GestureLockView getGestureLockViewInstance(Context context, int position);
74 | }
75 |
76 | public GestureLock(Context context){
77 | this(context, null);
78 | }
79 |
80 | public GestureLock(Context context, AttributeSet attrs){
81 | this(context, attrs, 0);
82 | }
83 |
84 | public GestureLock(Context context, AttributeSet attrs, int defStyle){
85 | super(context, attrs, defStyle);
86 |
87 | negativeGestures = new int[depth * depth];
88 | for(int i = 0; i < negativeGestures.length; i++) negativeGestures[i] = -1;
89 | gesturesContainer = negativeGestures.clone();
90 |
91 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GestureLock);
92 | int lineWidth = ta.getDimensionPixelSize(R.styleable.GestureLock_line_width, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics()));
93 | mCustomColor = ta.getColor(R.styleable.GestureLock_line_normal_color, DEFAULT_COLOR);
94 | mCustomErrorColor = ta.getColor(R.styleable.GestureLock_line_error_color, DEFAULT_ERROR_COLOR);
95 | ta.recycle();
96 |
97 | paint = new Paint(Paint.ANTI_ALIAS_FLAG);
98 | paint.setStyle(Paint.Style.STROKE);
99 | paint.setStrokeWidth(lineWidth);
100 | paint.setStrokeCap(Paint.Cap.ROUND);
101 | paint.setStrokeJoin(Paint.Join.ROUND);
102 |
103 | unmatchedCount = 0;
104 |
105 | touchable = true;
106 | }
107 |
108 | public void setAdapter(GestureLockAdapter adapter){
109 | if(mAdapter == adapter) return;
110 |
111 | mAdapter = adapter;
112 |
113 | if(mAdapter != null){
114 | updateParametersForAdapter();
115 | updateChildForAdapter();
116 | }
117 | }
118 |
119 | private void updateChildForAdapter(){
120 | final int totalBlockCount = depth * depth;
121 | removeAllViewsInLayout();
122 |
123 | for(int i = 0; i < totalBlockCount; i++){
124 | GestureLockView child = mAdapter.getGestureLockViewInstance(getContext(), i);
125 | child.setLockerState(GestureLockView.LockerState.LOCKER_STATE_NORMAL);
126 | child.setId(i + 1);
127 |
128 | addViewInLayout(child, i, generateDefaultLayoutParams());
129 | }
130 |
131 | requestLayout();
132 | }
133 |
134 | private void updateParametersForAdapter(){
135 | this.depth = mAdapter.getDepth();
136 |
137 | negativeGestures = new int[depth * depth];
138 | for(int i = 0; i < negativeGestures.length; i++) negativeGestures[i] = -1;
139 | gesturesContainer = negativeGestures.clone();
140 | defaultGestures = mAdapter.getCorrectGestures();
141 |
142 | if(defaultGestures.length > negativeGestures.length) throw new IllegalArgumentException("defaultGestures length must be less than or equal to " + negativeGestures.length);
143 |
144 | unmatchedBoundary = mAdapter.getUnmatchedBoundary();
145 | }
146 |
147 | public void notifyDataChanged(){
148 | updateParametersForAdapter();
149 | updateChildForAdapter();
150 | }
151 |
152 | public void setTouchable(boolean touchable){
153 | this.touchable = touchable;
154 | }
155 |
156 | public void resetUnmatchedCount(){
157 | unmatchedCount = 0;
158 | }
159 |
160 | public void setOnGestureEventListener(OnGestureEventListener onGestureEventListener){
161 | this.onGestureEventListener = onGestureEventListener;
162 | }
163 |
164 | @Override
165 | public void addView(View child, int width, int height){
166 | if(!(child instanceof GestureLockView)){
167 | throw new IllegalArgumentException();
168 | }
169 |
170 | super.addView(child, width, height);
171 | }
172 |
173 | @Override
174 | public void addView(View child, int index, ViewGroup.LayoutParams layoutParams){
175 | if(!(child instanceof GestureLockView)){
176 | throw new IllegalArgumentException();
177 | }
178 |
179 | super.addView(child, index, layoutParams);
180 | }
181 |
182 | @Override
183 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
184 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
185 |
186 | int widthMode = MeasureSpec.getMode(widthMeasureSpec);
187 | int heightMode = MeasureSpec.getMode(heightMeasureSpec);
188 |
189 | int widthSize = MeasureSpec.getSize(widthMeasureSpec);
190 | int heightSize = MeasureSpec.getSize(heightMeasureSpec);
191 |
192 | if(mAdapter != null){
193 | final int childCount = getChildCount();
194 | if(childCount == depth * depth){
195 |
196 | int childWidthMode, childHeightMode;
197 | int childWidthSize, childHeightSize;
198 |
199 | int totalGap = mAdapter.getBlockGapSize() * (depth - 1);
200 |
201 | if(widthMode == MeasureSpec.EXACTLY){
202 | childWidthMode = MeasureSpec.EXACTLY;
203 | childWidthSize = (widthSize - totalGap) / depth;
204 | }else{
205 | childWidthMode = MeasureSpec.AT_MOST;
206 | childWidthSize = MAX_BLOCK_SIZE;
207 | }
208 |
209 | if(heightMode == MeasureSpec.EXACTLY){
210 | childHeightMode = MeasureSpec.EXACTLY;
211 | childHeightSize = (heightSize - totalGap) / depth;
212 | }else{
213 | childHeightMode = MeasureSpec.AT_MOST;
214 | childHeightSize = MAX_BLOCK_SIZE;
215 | }
216 |
217 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, childWidthMode);
218 | int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode);
219 |
220 | for(int i = 0; i < childCount; i++){
221 | View child = getChildAt(i);
222 |
223 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
224 | }
225 | }
226 | }
227 | }
228 |
229 | @Override
230 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
231 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
232 | }
233 |
234 | @Override
235 | protected void onSizeChanged(int w, int h, int oldw, int oldh){
236 | mWidth = w;
237 | mHeight = h;
238 |
239 | mCenterX = w / 2;
240 | mCenterY = h / 2;
241 |
242 | mContentSize = mWidth > mHeight ? mHeight : mWidth;
243 | mHalfContentSize = mContentSize / 2;
244 | }
245 |
246 | @Override
247 | protected void onLayout(boolean changed, int l, int t, int r, int b){
248 |
249 | int totalGap = mAdapter.getBlockGapSize() * (depth - 1);
250 |
251 | final int xStart = mCenterX - mHalfContentSize;
252 | final int yStart = mCenterY - mHalfContentSize;
253 |
254 | int xStep = xStart;
255 | int yStep = yStart;
256 |
257 | int childSize = (mContentSize - totalGap) / depth;
258 |
259 | final int childCount = getChildCount();
260 |
261 | for(int i = 0; i < childCount; i++){
262 |
263 | View child = getChildAt(i);
264 |
265 | child.layout(xStep, yStep, xStep + childSize, yStep + childSize);
266 |
267 | if(i % depth == depth - 1){
268 | xStep = xStart;
269 | yStep += childSize + mAdapter.getBlockGapSize();
270 | }else{
271 | xStep += childSize + mAdapter.getBlockGapSize();
272 | }
273 | }
274 | }
275 |
276 | public void clear(){
277 | for(int i = 0; i < getChildCount(); i++) {
278 | View c = getChildAt(i);
279 | if(c instanceof GestureLockView){
280 | ((GestureLockView) c).setLockerState(GestureLockView.LockerState.LOCKER_STATE_NORMAL);
281 | ((GestureLockView) c).setArrow(-1);
282 | }
283 | }
284 |
285 | gesturePath = null;
286 | invalidate();
287 | }
288 |
289 | @Override
290 | public boolean onTouchEvent(MotionEvent event) {
291 |
292 | if (touchable) {
293 | switch (event.getActionMasked()) {
294 | case MotionEvent.ACTION_DOWN:
295 | for (int i = 0; i < getChildCount(); i++) {
296 | View c = getChildAt(i);
297 | if (c instanceof GestureLockView) {
298 | ((GestureLockView) c).setLockerState(GestureLockView.LockerState.LOCKER_STATE_NORMAL);
299 | ((GestureLockView) c).setArrow(-1);
300 | }
301 | }
302 |
303 | gesturePath = null;
304 |
305 | lastX = (int) event.getX();
306 | lastY = (int) event.getY();
307 | lastPathX = lastX;
308 | lastPathY = lastY;
309 |
310 | paint.setColor(mCustomColor);
311 |
312 | break;
313 | case MotionEvent.ACTION_MOVE:
314 |
315 | lastX = (int) event.getX();
316 | lastY = (int) event.getY();
317 |
318 | int cId = calculateChildIdByCoords(lastX, lastY);
319 |
320 | View child = findViewById(cId + 1);
321 | boolean checked = false;
322 | for (int id : gesturesContainer) {
323 | if (id == cId) {
324 | checked = true;
325 | break;
326 | }
327 | }
328 |
329 | if (child != null && child instanceof GestureLockView && checkChildInCoords(lastX, lastY, child)) {
330 | ((GestureLockView) child).setLockerState(GestureLockView.LockerState.LOCKER_STATE_SELECTED);
331 |
332 | if (!checked) {
333 | int checkedX = child.getLeft() + child.getWidth() / 2;
334 | int checkedY = child.getTop() + child.getHeight() / 2;
335 | if (gesturePath == null) {
336 | gesturePath = new Path();
337 | gesturePath.moveTo(checkedX, checkedY);
338 | } else {
339 | gesturePath.lineTo(checkedX, checkedY);
340 | }
341 | gesturesContainer[gestureCursor] = cId;
342 | gestureCursor++;
343 |
344 | lastPathX = checkedX;
345 | lastPathY = checkedY;
346 |
347 | if (onGestureEventListener != null)
348 | onGestureEventListener.onBlockSelected(cId);
349 | }
350 | }
351 |
352 | invalidate();
353 | break;
354 | case MotionEvent.ACTION_CANCEL:
355 | case MotionEvent.ACTION_UP:
356 |
357 | if (gesturesContainer[0] != -1) {
358 | boolean matched = false;
359 |
360 | if (gesturesContainer.length == defaultGestures.length || gesturesContainer[defaultGestures.length] == -1) {
361 | for (int j = 0; j < defaultGestures.length; j++) {
362 | if (gesturesContainer[j] == defaultGestures[j]) {
363 | matched = true;
364 | } else {
365 | matched = false;
366 | break;
367 | }
368 | }
369 | }
370 |
371 | if (!matched && mode != MODE_EDIT) {
372 | unmatchedCount++;
373 | paint.setColor(mCustomErrorColor);
374 | for (int k = 0; k < gesturesContainer.length; k++) {
375 | View selectedChild = findViewById(gesturesContainer[k] + 1);
376 | if (selectedChild != null && selectedChild instanceof GestureLockView) {
377 | ((GestureLockView) selectedChild).setLockerState(GestureLockView.LockerState.LOCKER_STATE_ERROR);
378 |
379 | if (k < gesturesContainer.length - 1 && gesturesContainer[k + 1] != -1) {
380 | View nextChild = findViewById(gesturesContainer[k + 1] + 1);
381 | if (nextChild != null) {
382 | int dx = nextChild.getLeft() - selectedChild.getLeft();
383 | int dy = nextChild.getTop() - selectedChild.getTop();
384 |
385 | int angle = (int) Math.toDegrees(Math.atan2(dy, dx)) + 90;
386 | ((GestureLockView) selectedChild).setArrow(angle);
387 | }
388 | }
389 | }
390 | }
391 | } else {
392 | unmatchedCount = 0;
393 | }
394 |
395 |
396 | if (onGestureEventListener != null) {
397 | onGestureEventListener.onGestureEvent(matched);
398 | if (unmatchedCount >= unmatchedBoundary) {
399 | onGestureEventListener.onUnmatchedExceedBoundary();
400 | unmatchedCount = 0;
401 | }
402 | }
403 | }
404 |
405 | gestureCursor = 0;
406 | gesturesContainer = negativeGestures.clone();
407 |
408 | lastX = lastPathX;
409 | lastY = lastPathY;
410 |
411 | invalidate();
412 |
413 | break;
414 | }
415 | }
416 |
417 | return true;
418 | }
419 |
420 | public void setMode(int mode){
421 | this.mode = mode;
422 | }
423 |
424 | private int calculateChildIdByCoords(int x, int y){
425 | if(x >= mCenterX - mHalfContentSize && x <= mCenterX + mHalfContentSize && y >= mCenterY - mHalfContentSize && y <= mCenterY + mHalfContentSize){
426 | x -= mCenterX - mHalfContentSize;
427 | y -= mCenterY - mHalfContentSize;
428 |
429 | int rowX = (int) (((float) x / (float) mContentSize) * depth);
430 | int rowY = (int) (((float) y / (float) mContentSize) * depth);
431 |
432 | return rowX + (rowY * depth);
433 | }
434 |
435 | return -1;
436 | }
437 |
438 | private boolean checkChildInCoords(int x, int y, View child){
439 | if(child != null){
440 |
441 | int centerX = child.getLeft() + child.getWidth() / 2;
442 | int centerY = child.getTop() + child.getHeight() / 2;
443 |
444 | int dx = centerX - x;
445 | int dy = centerY - y;
446 |
447 | int radius = child.getWidth() > child.getHeight() ? child.getHeight() : child.getWidth();
448 | radius /= 2;
449 | if(dx * dx + dy * dy < radius * radius) return true;
450 | }
451 |
452 | return false;
453 | }
454 |
455 | @Override
456 | public void dispatchDraw(Canvas canvas){
457 |
458 | if(gesturePath != null){
459 | canvas.drawPath(gesturePath, paint);
460 | }
461 |
462 | if(gesturesContainer[0] != -1) canvas.drawLine(lastPathX, lastPathY, lastX, lastY, paint);
463 |
464 | super.dispatchDraw(canvas);
465 | }
466 | }
467 |
--------------------------------------------------------------------------------