diff --git a/app/build.gradle b/app/build.gradle index c98832c..85483ee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,8 +35,8 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) -// implementation project(':library') - implementation "com.github.ihsg:PatternLocker:$rootProject.ext.versionName" + implementation project(':library') +// implementation "com.github.ihsg:PatternLocker:$rootProject.ext.versionName" implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion" implementation "com.android.support.constraint:constraint-layout:$rootProject.ext.constraintLayoutVersion" diff --git a/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternSettingActivity.kt b/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternSettingActivity.kt index c1be176..198e329 100644 --- a/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternSettingActivity.kt +++ b/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternSettingActivity.kt @@ -10,6 +10,7 @@ import com.github.ihsg.demo.util.PatternHelper import com.github.ihsg.patternlocker.OnPatternChangeListener import com.github.ihsg.patternlocker.PatternLockerView import kotlinx.android.synthetic.main.activity_default_pattern_checking.* +import java.util.* class DefaultPatternSettingActivity : AppCompatActivity() { private var patternHelper: PatternHelper? = null @@ -17,6 +18,7 @@ class DefaultPatternSettingActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_default_pattern_setting) + patternIndicatorView.updateState(Arrays.asList(1, 2), false) patternLockerView.setOnPatternChangedListener(object : OnPatternChangeListener { override fun onStart(view: PatternLockerView) {} diff --git a/app/src/main/java/com/github/ihsg/demo/ui/simple/SimplePatternCheckingActivity.kt b/app/src/main/java/com/github/ihsg/demo/ui/simple/SimplePatternCheckingActivity.kt index 9c30b33..041c31f 100644 --- a/app/src/main/java/com/github/ihsg/demo/ui/simple/SimplePatternCheckingActivity.kt +++ b/app/src/main/java/com/github/ihsg/demo/ui/simple/SimplePatternCheckingActivity.kt @@ -9,6 +9,7 @@ import android.util.TypedValue import com.github.ihsg.demo.R import com.github.ihsg.demo.util.PatternHelper +import com.github.ihsg.patternlocker.DefaultLockerNormalCellView import com.github.ihsg.patternlocker.OnPatternChangeListener import com.github.ihsg.patternlocker.PatternLockerView import kotlinx.android.synthetic.main.activity_default_pattern_checking.* @@ -21,21 +22,21 @@ class SimplePatternCheckingActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_simple_pattern_checking) - this.patternIndicatorView!!.setFillColor(ContextCompat.getColor(this, R.color.color_blue)) - .setNormalColor(ContextCompat.getColor(this, R.color.colorWhite)) - .setHitColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)) - .setErrorColor(ContextCompat.getColor(this, R.color.color_red)) - .setLineWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, - resources.displayMetrics)) - .buildWithDefaultStyle() - - this.patternLockerView!!.setFillColor(ContextCompat.getColor(this, R.color.color_blue)) - .setNormalColor(ContextCompat.getColor(this, R.color.colorWhite)) - .setHitColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)) - .setErrorColor(ContextCompat.getColor(this, R.color.color_red)) - .setLineWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, - resources.displayMetrics)) - .buildWithDefaultStyle() + val pivStyle = (this.patternIndicatorView.normalCellView as DefaultLockerNormalCellView).styleDecorator + pivStyle.normalColor = ContextCompat.getColor(this, R.color.colorWhite) + pivStyle.fillColor = ContextCompat.getColor(this, R.color.color_blue) + pivStyle.hitColor = ContextCompat.getColor(this, R.color.colorPrimaryDark) + pivStyle.errorColor = ContextCompat.getColor(this, R.color.color_red) + pivStyle.lineWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, + resources.displayMetrics) + + val plvStyle = (this.patternLockerView.normalCellView as DefaultLockerNormalCellView).styleDecorator + plvStyle.normalColor = ContextCompat.getColor(this, R.color.colorWhite) + plvStyle.fillColor = ContextCompat.getColor(this, R.color.color_blue) + plvStyle.hitColor = ContextCompat.getColor(this, R.color.colorPrimaryDark) + plvStyle.errorColor = ContextCompat.getColor(this, R.color.color_red) + plvStyle.lineWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, + resources.displayMetrics) this.patternLockerView!!.setOnPatternChangedListener(object : OnPatternChangeListener { override fun onStart(view: PatternLockerView) {} diff --git a/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternCheckingActivity.kt b/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternCheckingActivity.kt index 8838035..30aeaef 100644 --- a/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternCheckingActivity.kt +++ b/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternCheckingActivity.kt @@ -7,6 +7,7 @@ import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity import com.github.ihsg.demo.R import com.github.ihsg.demo.util.PatternHelper +import com.github.ihsg.patternlocker.DefaultLockerNormalCellView import com.github.ihsg.patternlocker.OnPatternChangeListener import com.github.ihsg.patternlocker.PatternLockerView import kotlinx.android.synthetic.main.activity_simple_pattern_checking.* @@ -19,12 +20,11 @@ class WholePatternCheckingActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_whole_pattern_checking) - val hitCellView = RippleLockerHitCellView() - .setHitColor(this.patternLockerView.getHitColor()) - .setErrorColor(this.patternLockerView.getErrorColor()) + val decorator = (this.patternLockerView.normalCellView as DefaultLockerNormalCellView).styleDecorator - this.patternLockerView.setHitCellView(hitCellView) - .build() + this.patternLockerView.hitCellView = RippleLockerHitCellView() + .setHitColor(decorator.hitColor) + .setErrorColor(decorator.errorColor) this.patternLockerView.setOnPatternChangedListener(object : OnPatternChangeListener { override fun onStart(view: PatternLockerView) {} diff --git a/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternSettingActivity.kt b/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternSettingActivity.kt index 4e7219b..779a210 100644 --- a/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternSettingActivity.kt +++ b/app/src/main/java/com/github/ihsg/demo/ui/whole/WholePatternSettingActivity.kt @@ -8,6 +8,7 @@ import android.support.v7.app.AppCompatActivity import android.view.View import com.github.ihsg.demo.R import com.github.ihsg.demo.util.PatternHelper +import com.github.ihsg.patternlocker.DefaultLockerNormalCellView import com.github.ihsg.patternlocker.OnPatternChangeListener import com.github.ihsg.patternlocker.PatternLockerView import kotlinx.android.synthetic.main.activity_simple_pattern_checking.* @@ -20,13 +21,12 @@ class WholePatternSettingActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_whole_pattern_setting) - val hitCellView = RippleLockerHitCellView() - .setHitColor(this.patternLockerView!!.getHitColor()) - .setErrorColor(this.patternLockerView!!.getErrorColor()) - - this.patternLockerView!!.setHitCellView(hitCellView) - .build() + val decorator = (this.patternLockerView.normalCellView as DefaultLockerNormalCellView).styleDecorator + this.patternLockerView.hitCellView = RippleLockerHitCellView() + .setHitColor(decorator.hitColor) + .setErrorColor(decorator.errorColor) + this.patternLockerView!!.setOnPatternChangedListener(object : OnPatternChangeListener { override fun onStart(view: PatternLockerView) {} diff --git a/app/src/main/res/layout/activity_whole_pattern_setting.xml b/app/src/main/res/layout/activity_whole_pattern_setting.xml index 7fe9f0c..d568e69 100644 --- a/app/src/main/res/layout/activity_whole_pattern_setting.xml +++ b/app/src/main/res/layout/activity_whole_pattern_setting.xml @@ -40,7 +40,7 @@ android:layout_marginLeft="50dp" android:layout_marginTop="20dp" android:layout_marginRight="50dp" - app:plv_canSkip="true" + app:plv_enableSkip="true" app:plv_enableHapticFeedback="true" app:plv_color="@color/color_gray" app:plv_enableAutoClean="false" diff --git a/build.gradle b/build.gradle index d061d34..85eaf23 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { // versions ext { versionCode = 14 - versionName = '2.4.5' + versionName = '2.5.0' minSdkVersion = 15 targetSdkVersion = 28 @@ -23,7 +23,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:3.4.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0217f0e..44dca29 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Nov 01 10:15:42 CST 2018 +#Fri Apr 19 11:09:52 CST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip diff --git a/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt b/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt index 53cd0d5..2169bf9 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt @@ -2,8 +2,18 @@ package com.github.ihsg.patternlocker /** * Created by hsg on 20/09/2017. + * + * @param id each cell id order like this: + * + * 0 1 2 + * 3 4 5 + * 6 7 8 + * + * @param x + * @param y + * @param radius + * @param isHit */ - class CellBean(val id: Int, val x: Float, val y: Float, val radius: Float) { var isHit: Boolean = false @@ -24,5 +34,4 @@ class CellBean(val id: Int, val x: Float, val y: Float, val radius: Float) { override fun toString(): String { return "CellBean(id=$id, x=$x, y=$y, radius=$radius, isHit=$isHit)" } - } \ No newline at end of file diff --git a/library/src/main/java/com/github/ihsg/patternlocker/Config.kt b/library/src/main/java/com/github/ihsg/patternlocker/Config.kt index 5ae8b11..5a1ba2d 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/Config.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/Config.kt @@ -19,24 +19,20 @@ internal object Config { const val defaultFreezeDuration = 1000//ms const val defaultEnableAutoClean = true const val defaultEnableHapticFeedback = false - const val defaultCanSkip = false + const val defaultEnableSkip = false const val defaultEnableLogger = false - val defaultNormalColor: Int - @ColorInt - get() = Color.parseColor(DEFAULT_NORMAL_COLOR) + @ColorInt + val defaultNormalColor: Int = Color.parseColor(DEFAULT_NORMAL_COLOR) - val defaultHitColor: Int - @ColorInt - get() = Color.parseColor(DEFAULT_HIT_COLOR) + @ColorInt + val defaultHitColor: Int = Color.parseColor(DEFAULT_HIT_COLOR) - val defaultErrorColor: Int - @ColorInt - get() = Color.parseColor(DEFAULT_ERROR_COLOR) + @ColorInt + val defaultErrorColor: Int = Color.parseColor(DEFAULT_ERROR_COLOR) - val defaultFillColor: Int - @ColorInt - get() = Color.parseColor(DEFAULT_FILL_COLOR) + @ColorInt + val defaultFillColor: Int = Color.parseColor(DEFAULT_FILL_COLOR) fun getDefaultLineWidth(resources: Resources): Float { return convertDpToPx(DEFAULT_LINE_WIDTH.toFloat(), resources) diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt index 2139375..e059ad2 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt @@ -8,11 +8,8 @@ import android.support.annotation.ColorInt * Created by hsg on 22/02/2018. */ -class DefaultIndicatorHitCellView : IHitCellView { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 +class DefaultIndicatorHitCellView(val styleDecorator: DefaultStyleDecorator) : IHitCellView { + private val paint: Paint by lazy { Config.createPaint() } @@ -21,26 +18,6 @@ class DefaultIndicatorHitCellView : IHitCellView { this.paint.style = Paint.Style.FILL } - @ColorInt - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(@ColorInt normalColor: Int): DefaultIndicatorHitCellView { - this.normalColor = normalColor - return this - } - - @ColorInt - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(@ColorInt errorColor: Int): DefaultIndicatorHitCellView { - this.errorColor = errorColor - return this - } - override fun draw(canvas: Canvas, cellBean: CellBean, isError: Boolean) { val saveCount = canvas.save() @@ -52,6 +29,6 @@ class DefaultIndicatorHitCellView : IHitCellView { @ColorInt private fun getColor(isError: Boolean): Int { - return if (isError) this.getErrorColor() else this.getNormalColor() + return if (isError) this.styleDecorator.errorColor else this.styleDecorator.hitColor } } \ No newline at end of file diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt index 5528107..3b9301d 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt @@ -9,12 +9,8 @@ import android.support.annotation.ColorInt * Created by hsg on 22/02/2018. */ -class DefaultIndicatorLinkedLineView : IIndicatorLinkedLineView { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 - private var lineWidth: Float = 0f +class DefaultIndicatorLinkedLineView(val styleDecorator: DefaultStyleDecorator) : IIndicatorLinkedLineView { + private val paint: Paint by lazy { Config.createPaint() } @@ -23,35 +19,6 @@ class DefaultIndicatorLinkedLineView : IIndicatorLinkedLineView { this.paint.style = Paint.Style.STROKE } - @ColorInt - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(@ColorInt normalColor: Int): DefaultIndicatorLinkedLineView { - this.normalColor = normalColor - return this - } - - @ColorInt - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(@ColorInt errorColor: Int): DefaultIndicatorLinkedLineView { - this.errorColor = errorColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): DefaultIndicatorLinkedLineView { - this.lineWidth = lineWidth - return this - } - override fun draw(canvas: Canvas, hitIndexList: List, cellBeanList: List, isError: Boolean) { if (hitIndexList.isEmpty() || cellBeanList.isEmpty()) { return @@ -74,13 +41,13 @@ class DefaultIndicatorLinkedLineView : IIndicatorLinkedLineView { } this.paint.color = this.getColor(isError) - this.paint.strokeWidth = this.getLineWidth() + this.paint.strokeWidth = this.styleDecorator.lineWidth canvas.drawPath(path, this.paint) canvas.restoreToCount(saveCount) } @ColorInt private fun getColor(isError: Boolean): Int { - return if (isError) this.getErrorColor() else this.getNormalColor() + return if (isError) this.styleDecorator.errorColor else this.styleDecorator.hitColor } } diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt index 40f9ba1..50922a6 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt @@ -2,18 +2,13 @@ package com.github.ihsg.patternlocker import android.graphics.Canvas import android.graphics.Paint -import android.support.annotation.ColorInt /** * Created by hsg on 22/02/2018. */ -class DefaultIndicatorNormalCellView : INormalCellView { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var fillColor: Int = 0 - private var lineWidth: Float = 0f +class DefaultIndicatorNormalCellView(val styleDecorator: DefaultStyleDecorator) : INormalCellView { + private val paint: Paint by lazy { Config.createPaint() } @@ -22,45 +17,16 @@ class DefaultIndicatorNormalCellView : INormalCellView { this.paint.style = Paint.Style.FILL } - @ColorInt - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(@ColorInt normalColor: Int): DefaultIndicatorNormalCellView { - this.normalColor = normalColor - return this - } - - @ColorInt - fun getFillColor(): Int { - return fillColor - } - - fun setFillColor(@ColorInt fillColor: Int): DefaultIndicatorNormalCellView { - this.fillColor = fillColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): DefaultIndicatorNormalCellView { - this.lineWidth = lineWidth - return this - } - override fun draw(canvas: Canvas, cellBean: CellBean) { val saveCount = canvas.save() //outer circle - this.paint.color = this.getNormalColor() + this.paint.color = this.styleDecorator.normalColor canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint) //inner circle - this.paint.color = this.getFillColor() - canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.getLineWidth(), this.paint) + this.paint.color = this.styleDecorator.fillColor + canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint) canvas.restoreToCount(saveCount) } diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt index a477057..a3747d3 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt @@ -8,14 +8,8 @@ import android.support.annotation.ColorInt * Created by hsg on 22/02/2018. */ -class DefaultLockerHitCellView : IHitCellView { - @ColorInt - private var hitColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 - @ColorInt - private var fillColor: Int = 0 - private var lineWidth: Float = 0f +class DefaultLockerHitCellView(val styleDecorator: DefaultStyleDecorator) : IHitCellView { + private val paint: Paint by lazy { Config.createPaint() } @@ -24,45 +18,6 @@ class DefaultLockerHitCellView : IHitCellView { this.paint.style = Paint.Style.FILL } - @ColorInt - fun getHitColor(): Int { - return hitColor - } - - fun setHitColor(@ColorInt hitColor: Int): DefaultLockerHitCellView { - this.hitColor = hitColor - return this - } - - @ColorInt - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(@ColorInt errorColor: Int): DefaultLockerHitCellView { - this.errorColor = errorColor - return this - } - - @ColorInt - fun getFillColor(): Int { - return fillColor - } - - fun setFillColor(@ColorInt fillColor: Int): DefaultLockerHitCellView { - this.fillColor = fillColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): DefaultLockerHitCellView { - this.lineWidth = lineWidth - return this - } - override fun draw(canvas: Canvas, cellBean: CellBean, isError: Boolean) { val saveCount = canvas.save() @@ -71,8 +26,8 @@ class DefaultLockerHitCellView : IHitCellView { canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint) // draw fill circle - this.paint.color = this.getFillColor() - canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.getLineWidth(), this.paint) + this.paint.color = this.styleDecorator.fillColor + canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint) // draw inner circle this.paint.color = this.getColor(isError) @@ -83,6 +38,6 @@ class DefaultLockerHitCellView : IHitCellView { @ColorInt private fun getColor(isError: Boolean): Int { - return if (isError) this.getErrorColor() else this.getHitColor() + return if (isError) this.styleDecorator.errorColor else this.styleDecorator.hitColor } } diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt index c4f2562..67d6756 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt @@ -9,50 +9,19 @@ import android.support.annotation.ColorInt * Created by hsg on 22/02/2018. */ -class DefaultLockerLinkedLineView : ILockerLinkedLineView { +class DefaultLockerLinkedLineView(val styleDecorator: DefaultStyleDecorator) : ILockerLinkedLineView { + companion object { + private const val TAG = "DefaultLockerLinkedLineView" + } - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 - private var lineWidth: Float = 0f private val paint: Paint by lazy { Config.createPaint() } - private val TAG = "DefaultLockerLinkedLineView" - init { this.paint.style = Paint.Style.STROKE } - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(normalColor: Int): DefaultLockerLinkedLineView { - this.normalColor = normalColor - return this - } - - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(errorColor: Int): DefaultLockerLinkedLineView { - this.errorColor = errorColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): DefaultLockerLinkedLineView { - this.lineWidth = lineWidth - return this - } - override fun draw(canvas: Canvas, hitIndexList: List, cellBeanList: List, endX: Float, endY: Float, isError: Boolean) { Logger.d(TAG, "hitIndexList = $hitIndexList, cellBeanList = $cellBeanList") if (hitIndexList.isEmpty() || cellBeanList.isEmpty()) { @@ -80,7 +49,7 @@ class DefaultLockerLinkedLineView : ILockerLinkedLineView { } this.paint.color = this.getColor(isError) - this.paint.strokeWidth = this.getLineWidth() + this.paint.strokeWidth = this.styleDecorator.lineWidth canvas.drawPath(path, this.paint) canvas.restoreToCount(saveCount) @@ -88,6 +57,6 @@ class DefaultLockerLinkedLineView : ILockerLinkedLineView { @ColorInt private fun getColor(isError: Boolean): Int { - return if (isError) this.getErrorColor() else this.getNormalColor() + return if (isError) this.styleDecorator.errorColor else this.styleDecorator.hitColor } } diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt index 6fa9d1c..d1ce1a4 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt @@ -2,18 +2,12 @@ package com.github.ihsg.patternlocker import android.graphics.Canvas import android.graphics.Paint -import android.support.annotation.ColorInt /** * Created by hsg on 22/02/2018. */ -class DefaultLockerNormalCellView : INormalCellView { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var fillColor: Int = 0 - private var lineWidth: Float = 0f +class DefaultLockerNormalCellView(val styleDecorator: DefaultStyleDecorator) : INormalCellView { private val paint: Paint by lazy { Config.createPaint() } @@ -22,43 +16,16 @@ class DefaultLockerNormalCellView : INormalCellView { this.paint.style = Paint.Style.FILL } - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(normalColor: Int): DefaultLockerNormalCellView { - this.normalColor = normalColor - return this - } - - fun getFillColor(): Int { - return fillColor - } - - fun setFillColor(fillColor: Int): DefaultLockerNormalCellView { - this.fillColor = fillColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): DefaultLockerNormalCellView { - this.lineWidth = lineWidth - return this - } - override fun draw(canvas: Canvas, cellBean: CellBean) { val saveCount = canvas.save() // draw outer circle - this.paint.color = this.getNormalColor() + this.paint.color = this.styleDecorator.normalColor canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint) // draw fill circle - this.paint.color = this.getFillColor() - canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.getLineWidth(), this.paint) + this.paint.color = this.styleDecorator.fillColor + canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint) canvas.restoreToCount(saveCount) } diff --git a/library/src/main/java/com/github/ihsg/patternlocker/DefaultStyleDecorator.kt b/library/src/main/java/com/github/ihsg/patternlocker/DefaultStyleDecorator.kt new file mode 100644 index 0000000..fa5920f --- /dev/null +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultStyleDecorator.kt @@ -0,0 +1,15 @@ +package com.github.ihsg.patternlocker + +import android.support.annotation.ColorInt + +/** + * Created by hsg on 28/04/2019. + */ + +data class DefaultStyleDecorator( + @ColorInt var normalColor: Int, + @ColorInt var fillColor: Int, + @ColorInt var hitColor: Int, + @ColorInt var errorColor: Int, + var lineWidth: Float +) \ No newline at end of file diff --git a/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt b/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt index 36045db..362196f 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt @@ -2,7 +2,6 @@ package com.github.ihsg.patternlocker import android.content.Context import android.graphics.Canvas -import android.support.annotation.ColorInt import android.util.AttributeSet import android.util.Log import android.view.View @@ -12,15 +11,14 @@ import android.view.View */ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var fillColor: Int = 0 - @ColorInt - private var hitColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 - private var lineWidth: Float = 0f + companion object { + private const val TAG = "PatternIndicatorView" + } + + var linkedLineView: IIndicatorLinkedLineView? = null + var normalCellView: INormalCellView? = null + var hitCellView: IHitCellView? = null + private var isError: Boolean = false private val hitIndexList: MutableList by lazy { mutableListOf() @@ -31,148 +29,26 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At CellFactory(w, h).cellBeanList } - private var linkedLineView: IIndicatorLinkedLineView? = null - private var normalCellView: INormalCellView? = null - private var hitCellView: IHitCellView? = null - init { init(context, attrs, defStyleAttr) } - @ColorInt - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(@ColorInt normalColor: Int): PatternIndicatorView { - this.normalColor = normalColor - return this - } - - @ColorInt - fun getFillColor(): Int { - return fillColor - } - - fun setFillColor(@ColorInt fillColor: Int): PatternIndicatorView { - this.fillColor = fillColor - return this - } - - @ColorInt - fun getHitColor(): Int { - return hitColor - } - - fun setHitColor(@ColorInt hitColor: Int): PatternIndicatorView { - this.hitColor = hitColor - return this - } - - @ColorInt - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(@ColorInt errorColor: Int): PatternIndicatorView { - this.errorColor = errorColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): PatternIndicatorView { - this.lineWidth = lineWidth - return this - } - - fun getLinkedLineView(): IIndicatorLinkedLineView? { - return linkedLineView - } - - fun setLinkedLineView(linkedLineView: IIndicatorLinkedLineView): PatternIndicatorView { - this.linkedLineView = linkedLineView - return this - } - - fun getNormalCellView(): INormalCellView? { - return normalCellView - } - - fun setNormalCellView(normalCellView: INormalCellView): PatternIndicatorView { - this.normalCellView = normalCellView - return this - } - - fun getHitCellView(): IHitCellView? { - return hitCellView - } - - fun setHitCellView(hitCellView: IHitCellView): PatternIndicatorView { - this.hitCellView = hitCellView - return this - } - - fun buildWithDefaultStyle() { - this.setNormalCellView(DefaultIndicatorNormalCellView() - .setNormalColor(this.getNormalColor()) - .setFillColor(this.getFillColor()) - .setLineWidth(this.getLineWidth()) - ).setHitCellView(DefaultIndicatorHitCellView() - .setErrorColor(this.getErrorColor()) - .setNormalColor(this.getHitColor()) - ).setLinkedLineView(DefaultIndicatorLinkedLineView() - .setNormalColor(this.getHitColor()) - .setErrorColor(this.getErrorColor()) - .setLineWidth(this.getLineWidth()) - ).build() - } - - fun build() { - if (getNormalCellView() == null) { - Log.e(TAG, "in build() function, normalCellView is null") - return - } - - if (getHitCellView() == null) { - Log.e(TAG, "in build() function, hitCellView is null") - return - } - - if (getLinkedLineView() == null) { - Log.w(TAG, "in build() function, linkedLineView is null") - } - postInvalidate() - } - fun updateState(hitIndexList: List?, isError: Boolean) { - //1. reset to default state - if (!this.hitIndexList.isEmpty()) { - this.hitIndexList.clear() - } - this.cellBeanList.forEach { - it.isHit = false - } - - //2. update hit state - if (hitIndexList != null && !hitIndexList.isEmpty()) { - this.hitIndexList.addAll(hitIndexList) - - this.hitIndexList.forEach { - if (0 <= it && it < this.cellBeanList.size) { - this.cellBeanList[it].isHit = true - } + hitIndexList?.let { + //1. clear pre state + if (this.hitIndexList.isNotEmpty()) { + this.hitIndexList.clear() } - } + //2. record new state + this.hitIndexList.addAll(it) - //3. update result - this.isError = isError + //3. update result + this.isError = isError - //4. update view - postInvalidate() + //4. update view + invalidate() + } } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { @@ -181,8 +57,9 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At } override fun onDraw(canvas: Canvas) { - drawLinkedLine(canvas) - drawCells(canvas) + this.updateHitState() + this.drawLinkedLine(canvas) + this.drawCells(canvas) } private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { @@ -193,29 +70,45 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { val ta = context.obtainStyledAttributes(attrs, R.styleable.PatternIndicatorView, defStyleAttr, 0) - this.normalColor = ta.getColor(R.styleable.PatternIndicatorView_piv_color, Config.defaultNormalColor) - this.fillColor = ta.getColor(R.styleable.PatternIndicatorView_piv_fillColor, Config.defaultFillColor) - this.hitColor = ta.getColor(R.styleable.PatternIndicatorView_piv_hitColor, Config.defaultHitColor) - this.errorColor = ta.getColor(R.styleable.PatternIndicatorView_piv_errorColor, Config.defaultErrorColor) - this.lineWidth = ta.getDimension(R.styleable.PatternIndicatorView_piv_lineWidth, Config.getDefaultLineWidth(resources)) + val normalColor = ta.getColor(R.styleable.PatternIndicatorView_piv_color, Config.defaultNormalColor) + val fillColor = ta.getColor(R.styleable.PatternIndicatorView_piv_fillColor, Config.defaultFillColor) + val hitColor = ta.getColor(R.styleable.PatternIndicatorView_piv_hitColor, Config.defaultHitColor) + val errorColor = ta.getColor(R.styleable.PatternIndicatorView_piv_errorColor, Config.defaultErrorColor) + val lineWidth = ta.getDimension(R.styleable.PatternIndicatorView_piv_lineWidth, Config.getDefaultLineWidth(resources)) ta.recycle() - this.setNormalColor(this.normalColor) - this.setFillColor(this.fillColor) - this.setHitColor(this.hitColor) - this.setErrorColor(this.errorColor) - this.setLineWidth(this.lineWidth) + val decorator = DefaultStyleDecorator(normalColor, fillColor, hitColor, errorColor, lineWidth) + this.normalCellView = DefaultIndicatorNormalCellView(decorator) + this.hitCellView = DefaultIndicatorHitCellView(decorator) + this.linkedLineView = DefaultIndicatorLinkedLineView(decorator) } private fun initData() { - this.buildWithDefaultStyle() this.hitIndexList.clear() } + private fun updateHitState() { + //1. clear pre state + this.cellBeanList.forEach { + it.isHit = false + } + + //2. update hit state + this.hitIndexList.let { it -> + if (it.isNotEmpty()) { + it.forEach { + if (0 <= it && it < this.cellBeanList.size) { + this.cellBeanList[it].isHit = true + } + } + } + } + } + private fun drawLinkedLine(canvas: Canvas) { - if (!this.hitIndexList.isEmpty() && this.getLinkedLineView() != null) { - this.getLinkedLineView()!!.draw(canvas, + if (this.hitIndexList.isNotEmpty()) { + this.linkedLineView?.draw(canvas, this.hitIndexList, this.cellBeanList, this.isError) @@ -223,26 +116,22 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At } private fun drawCells(canvas: Canvas) { - if (this.getHitCellView() == null) { + if (this.hitCellView == null) { Log.e(TAG, "drawCells(), hitCellView is null") return } - if (this.getNormalCellView() == null) { + if (this.normalCellView == null) { Log.e(TAG, "drawCells(), normalCellView is null") return } this.cellBeanList.forEach { if (it.isHit) { - this.getHitCellView()!!.draw(canvas, it, this.isError) + this.hitCellView?.draw(canvas, it, this.isError) } else { - this.getNormalCellView()!!.draw(canvas, it) + this.normalCellView?.draw(canvas, it) } } } - - companion object { - private const val TAG = "PatternIndicatorView" - } } \ No newline at end of file diff --git a/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt b/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt index addb4c2..e694dc7 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt @@ -2,7 +2,6 @@ package com.github.ihsg.patternlocker import android.content.Context import android.graphics.Canvas -import android.support.annotation.ColorInt import android.util.AttributeSet import android.util.Log import android.view.HapticFeedbackConstants @@ -15,42 +14,88 @@ import android.view.View */ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { - @ColorInt - private var normalColor: Int = 0 - @ColorInt - private var hitColor: Int = 0 - @ColorInt - private var errorColor: Int = 0 - @ColorInt - private var fillColor: Int = 0 - private var freezeDuration: Int = 0 - private var hitSize: Int = 0 - private var lineWidth: Float = 0f + companion object { + private const val TAG = "PatternLockerView" + } + + /** + * 绘制完后是否自动清除标志位,如果开启了该标志位,延时@freezeDuration毫秒后自动清除已绘制图案 + */ + var enableAutoClean: Boolean = false + + /** + * 能否跳过中间点标志位,如果开启了该标志,则可以不用连续 + */ + var enableSkip: Boolean = false + + /** + * 是否开启触碰反馈,如果开启了该标志,则每连接一个cell则会震动 + */ + var enableHapticFeedback: Boolean = false + + /** + * 绘制完成后多久可以清除(单位ms),只有在@enableAutoClean = true 时有效 + */ + var freezeDuration: Int = 0 + + /** + * 绘制连接线 + */ + var linkedLineView: ILockerLinkedLineView? = null + + /** + * 绘制未操作时的cell样式 + */ + var normalCellView: INormalCellView? = null + + /** + * 绘制操作时的cell样式 + */ + var hitCellView: IHitCellView? = null + + /** + * 是否是错误的图案 + */ + private var isError: Boolean = false + + /** + * 终点x坐标 + */ private var endX: Float = 0f + + /** + * 终点y坐标 + */ private var endY: Float = 0f - private var isError: Boolean = false - private var enableAutoClean: Boolean = false - private var canSkip: Boolean = false - private var enableHapticFeedback: Boolean = false + + /** + * 记录绘制多少个cell,用于判断是否调用OnPatternChangeListener + */ + private var hitSize: Int = 0 + + /** + * 真正的cell数组 + */ private lateinit var cellBeanList: List + + /** + * 记录已绘制的cell的id + */ private val hitIndexList: MutableList by lazy { mutableListOf() } - private var listener: OnPatternChangeListener? = null - private var linkedLineView: ILockerLinkedLineView? = null - private var normalCellView: INormalCellView? = null - private var hitCellView: IHitCellView? = null - private val action = Runnable { - this.isEnabled = true - this.clearHitState() - } + /** + * 监听器 + */ + private var listener: OnPatternChangeListener? = null init { this.initAttrs(context, attrs, defStyleAttr) this.initData() } + fun enableDebug() { Logger.enable = true } @@ -59,140 +104,26 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri this.listener = listener } + /** + * 更改状态 + */ fun updateStatus(isError: Boolean) { this.isError = isError - postInvalidate() + invalidate() } + /** + * 清除已绘制图案 + */ fun clearHitState() { - clearHitData() + this.clearHitData() this.isError = false if (this.listener != null) { this.listener!!.onClear(this) } - - postInvalidate() - } - - @ColorInt - fun getNormalColor(): Int { - return normalColor - } - - fun setNormalColor(@ColorInt normalColor: Int): PatternLockerView { - this.normalColor = normalColor - return this - } - - @ColorInt - fun getHitColor(): Int { - return hitColor - } - - fun setHitColor(@ColorInt hitColor: Int): PatternLockerView { - this.hitColor = hitColor - return this - } - - @ColorInt - fun getErrorColor(): Int { - return errorColor - } - - fun setErrorColor(@ColorInt errorColor: Int): PatternLockerView { - this.errorColor = errorColor - return this - } - - @ColorInt - fun getFillColor(): Int { - return fillColor - } - - fun setFillColor(@ColorInt fillColor: Int): PatternLockerView { - this.fillColor = fillColor - return this - } - - fun getLineWidth(): Float { - return lineWidth - } - - fun setLineWidth(lineWidth: Float): PatternLockerView { - this.lineWidth = lineWidth - return this - } - - fun getLinkedLineView(): ILockerLinkedLineView? { - return linkedLineView - } - - fun setLinkedLineView(linkedLineView: ILockerLinkedLineView): PatternLockerView { - this.linkedLineView = linkedLineView - return this - } - - fun getNormalCellView(): INormalCellView? { - return normalCellView - } - - fun setNormalCellView(normalCellView: INormalCellView): PatternLockerView { - this.normalCellView = normalCellView - return this - } - - fun getHitCellView(): IHitCellView? { - return hitCellView - } - - fun setHitCellView(hitCellView: IHitCellView): PatternLockerView { - this.hitCellView = hitCellView - return this - } - - fun setFreezeDuration(freezeDuration: Int): PatternLockerView { - this.freezeDuration = freezeDuration - return this + invalidate() } - fun getFreezeDuration(): Int { - return this.freezeDuration - } - - fun buildWithDefaultStyle() { - this.setNormalCellView(DefaultLockerNormalCellView() - .setNormalColor(this.getNormalColor()) - .setFillColor(this.getFillColor()) - .setLineWidth(this.getLineWidth()) - ).setHitCellView(DefaultLockerHitCellView() - .setHitColor(this.getHitColor()) - .setErrorColor(this.getErrorColor()) - .setFillColor(this.getFillColor()) - .setLineWidth(this.getLineWidth()) - ).setLinkedLineView(DefaultLockerLinkedLineView() - .setNormalColor(this.getHitColor()) - .setErrorColor(this.getErrorColor()) - .setLineWidth(this.getLineWidth()) - ).build() - } - - fun build() { - if (getNormalCellView() == null) { - Log.e(TAG, "in build() function, normalCellView is null") - return - } - - if (getHitCellView() == null) { - Log.e(TAG, "in build() function, hitCellView is null") - return - } - - if (getLinkedLineView() == null) { - Log.w(TAG, "in build() function, linkedLineView is null") - } - - postInvalidate() - } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val a = Math.min(widthMeasureSpec, heightMeasureSpec) @@ -201,8 +132,8 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri override fun onDraw(canvas: Canvas) { this.initCellBeanList() - drawLinkedLine(canvas) - drawCells(canvas) + this.drawLinkedLine(canvas) + this.drawCells(canvas) } override fun onTouchEvent(event: MotionEvent): Boolean { @@ -213,49 +144,49 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri var isHandle = false when (event.action) { MotionEvent.ACTION_DOWN -> { - handleActionDown(event) + this.handleActionDown(event) isHandle = true } MotionEvent.ACTION_MOVE -> { - handleActionMove(event) + this.handleActionMove(event) isHandle = true } MotionEvent.ACTION_UP -> { - handleActionUp(event) + this.handleActionUp(event) isHandle = true } else -> { } } - postInvalidate() + invalidate() return if (isHandle) true else super.onTouchEvent(event) } private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { val ta = context.obtainStyledAttributes(attrs, R.styleable.PatternLockerView, defStyleAttr, 0) - this.normalColor = ta.getColor(R.styleable.PatternLockerView_plv_color, Config.defaultNormalColor) - this.hitColor = ta.getColor(R.styleable.PatternLockerView_plv_hitColor, Config.defaultHitColor) - this.errorColor = ta.getColor(R.styleable.PatternLockerView_plv_errorColor, Config.defaultErrorColor) - this.fillColor = ta.getColor(R.styleable.PatternLockerView_plv_fillColor, Config.defaultFillColor) + val normalColor = ta.getColor(R.styleable.PatternLockerView_plv_color, Config.defaultNormalColor) + val hitColor = ta.getColor(R.styleable.PatternLockerView_plv_hitColor, Config.defaultHitColor) + val errorColor = ta.getColor(R.styleable.PatternLockerView_plv_errorColor, Config.defaultErrorColor) + val fillColor = ta.getColor(R.styleable.PatternLockerView_plv_fillColor, Config.defaultFillColor) + val lineWidth = ta.getDimension(R.styleable.PatternLockerView_plv_lineWidth, Config.getDefaultLineWidth(resources)) + this.freezeDuration = ta.getInteger(R.styleable.PatternLockerView_plv_freezeDuration, Config.defaultFreezeDuration) - this.lineWidth = ta.getDimension(R.styleable.PatternLockerView_plv_lineWidth, Config.getDefaultLineWidth(resources)) this.enableAutoClean = ta.getBoolean(R.styleable.PatternLockerView_plv_enableAutoClean, Config.defaultEnableAutoClean) this.enableHapticFeedback = ta.getBoolean(R.styleable.PatternLockerView_plv_enableHapticFeedback, Config.defaultEnableHapticFeedback) - this.canSkip = ta.getBoolean(R.styleable.PatternLockerView_plv_canSkip, Config.defaultCanSkip) + this.enableSkip = ta.getBoolean(R.styleable.PatternLockerView_plv_enableSkip, Config.defaultEnableSkip) ta.recycle() - this.setNormalColor(this.normalColor) - this.setHitColor(this.hitColor) - this.setErrorColor(this.errorColor) - this.setFillColor(this.fillColor) - this.setLineWidth(this.lineWidth) + // style + val styleDecorator = DefaultStyleDecorator(normalColor, fillColor, hitColor, errorColor, lineWidth) + this.normalCellView = DefaultLockerNormalCellView(styleDecorator) + this.hitCellView = DefaultLockerHitCellView(styleDecorator) + this.linkedLineView = DefaultLockerLinkedLineView(styleDecorator) } private fun initData() { Logger.enable = Config.defaultEnableLogger - this.buildWithDefaultStyle() this.hitIndexList.clear() } @@ -268,8 +199,8 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun drawLinkedLine(canvas: Canvas) { - if (!this.hitIndexList.isEmpty() && getLinkedLineView() != null) { - getLinkedLineView()!!.draw(canvas, + if (this.hitIndexList.isNotEmpty()) { + this.linkedLineView?.draw(canvas, this.hitIndexList, this.cellBeanList, this.endX, @@ -279,31 +210,31 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun drawCells(canvas: Canvas) { - if (getHitCellView() == null) { + if (this.hitCellView == null) { Log.e(TAG, "drawCells(), hitCellView is null") return } - if (getNormalCellView() == null) { + if (this.normalCellView == null) { Log.e(TAG, "drawCells(), normalCellView is null") return } this.cellBeanList.forEach { if (it.isHit) { - getHitCellView()!!.draw(canvas, it, this.isError) + this.hitCellView?.draw(canvas, it, this.isError) } else { - getNormalCellView()!!.draw(canvas, it) + this.normalCellView?.draw(canvas, it) } } } private fun handleActionDown(event: MotionEvent) { //1. reset to default state - clearHitData() + this.clearHitData() //2. update hit state - updateHitState(event) + this.updateHitState(event) //3. notify listener this.listener?.onStart(this) @@ -311,8 +242,9 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri private fun handleActionMove(event: MotionEvent) { printLogger() + //1. update hit state - updateHitState(event) + this.updateHitState(event) //2. update end point this.endX = event.x @@ -327,10 +259,10 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun handleActionUp(event: MotionEvent) { - printLogger() + this.printLogger() //1. update hit state - updateHitState(event) + this.updateHitState(event) //2. update end point this.endX = 0f @@ -342,13 +274,13 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri //4. startTimer if needed if (this.enableAutoClean && this.hitIndexList.size > 0) { - startTimer() + this.startTimer() } } private fun updateHitState(event: MotionEvent) { this.cellBeanList.forEach { - if (!it.isHit && it.of(event.x, event.y, this.canSkip)) { + if (!it.isHit && it.of(event.x, event.y, this.enableSkip)) { it.isHit = true this.hitIndexList.add(it.id) this.hapticFeedback() @@ -357,7 +289,7 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun clearHitData() { - if (!this.hitIndexList.isEmpty()) { + if (this.hitIndexList.isNotEmpty()) { this.hitIndexList.clear() this.hitSize = 0 this.cellBeanList.forEach { it.isHit = false } @@ -370,6 +302,11 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri super.onDetachedFromWindow() } + private val action = Runnable { + this.isEnabled = true + this.clearHitState() + } + private fun startTimer() { this.isEnabled = false this.postDelayed(this.action, this.freezeDuration.toLong()) @@ -388,8 +325,4 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri Logger.d(TAG, "cellBeanList = ${this.cellBeanList}, hitIndexList = ${this.hitIndexList}") } } - - companion object { - private const val TAG = "PatternLockerView" - } } \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 1049b57..285444f 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -16,7 +16,7 @@ - + \ No newline at end of file