From 739db9278cafd358c26c6611cd5efd650ea2fc8d Mon Sep 17 00:00:00 2001 From: huangshouguo Date: Sun, 23 Jun 2019 10:10:05 +0800 Subject: [PATCH] fix hitCellView null && rename Config to DefaultConfig --- app/build.gradle | 4 +-- app/src/main/AndroidManifest.xml | 4 ++- .../ui/def/DefaultPatternCheckingActivity.kt | 3 +- build.gradle | 6 ++-- .../com/github/ihsg/patternlocker/CellBean.kt | 22 +++++------- .../{Config.kt => DefaultConfig.kt} | 2 +- .../DefaultIndicatorHitCellView.kt | 2 +- .../DefaultIndicatorLinkedLineView.kt | 2 +- .../DefaultIndicatorNormalCellView.kt | 2 +- .../patternlocker/DefaultLockerHitCellView.kt | 2 +- .../DefaultLockerLinkedLineView.kt | 2 +- .../DefaultLockerNormalCellView.kt | 2 +- .../patternlocker/PatternIndicatorView.kt | 26 +++++--------- .../ihsg/patternlocker/PatternLockerView.kt | 36 +++++++------------ 14 files changed, 47 insertions(+), 68 deletions(-) rename library/src/main/java/com/github/ihsg/patternlocker/{Config.kt => DefaultConfig.kt} (98%) 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/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb0a176..7096413 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ + android:theme="@style/AppTheme" + tools:ignore="GoogleAppIndexingWarning"> diff --git a/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternCheckingActivity.kt b/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternCheckingActivity.kt index 83e0acb..a63a6e4 100644 --- a/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternCheckingActivity.kt +++ b/app/src/main/java/com/github/ihsg/demo/ui/def/DefaultPatternCheckingActivity.kt @@ -20,7 +20,8 @@ class DefaultPatternCheckingActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_default_pattern_checking) - + patternLockerView.linkedLineView = null + patternLockerView.hitCellView = null patternLockerView.setOnPatternChangedListener(object : OnPatternChangeListener { override fun onStart(view: PatternLockerView) {} diff --git a/build.gradle b/build.gradle index 173c41e..e9479e1 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,8 @@ buildscript { buildToolsVersion = '28.0.3' supportLibraryVersion = '28.0.0' - constraintLayoutVersion = '1.0.2' - kotlinVersion = '1.3.21' + constraintLayoutVersion = '2.0.0-beta2' + kotlinVersion = '1.3.40' } repositories { @@ -23,7 +23,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.4.0' + classpath 'com.android.tools.build:gradle:3.4.1' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" 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 729f33d..04d2f79 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt @@ -1,22 +1,22 @@ package com.github.ihsg.patternlocker +import kotlin.math.sqrt + /** * Created by hsg on 20/09/2017. * - * @param id each cell id order like this: + * @param id 表示该cell的编号,9个cell的编号如下: * * 0 1 2 * 3 4 5 * 6 7 8 * - * @param x - * @param y - * @param radius - * @param isHit + * @param x 表示该cell的x坐标(相对坐标) + * @param y 表示该cell的y坐标(相对坐标) + * @param radius 表示该cell的半径 + * @param isHit 表示该cell是否被设置的标记 */ -class CellBean(val id: Int, val x: Float, val y: Float, val radius: Float) { - var isHit: Boolean = false - +data class CellBean(val id: Int, val x: Float, val y: Float, val radius: Float, var isHit: Boolean = false) { /** * 是否触碰到该view * @@ -28,10 +28,6 @@ class CellBean(val id: Int, val x: Float, val y: Float, val radius: Float) { val dx = this.x - x val dy = this.y - y val r = if (enableSkip) this.radius else this.radius * 1.5f - return Math.sqrt((dx * dx + dy * dy).toDouble()) <= r.toDouble() - } - - override fun toString(): String { - return "CellBean(id=$id, x=$x, y=$y, radius=$radius, isHit=$isHit)" + return sqrt((dx * dx + dy * dy).toDouble()) <= r.toDouble() } } \ 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/DefaultConfig.kt similarity index 98% rename from library/src/main/java/com/github/ihsg/patternlocker/Config.kt rename to library/src/main/java/com/github/ihsg/patternlocker/DefaultConfig.kt index 5a1ba2d..7414748 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/Config.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultConfig.kt @@ -9,7 +9,7 @@ import android.util.TypedValue /** * Created by hsg on 23/09/2017. */ -internal object Config { +internal object DefaultConfig { private const val DEFAULT_NORMAL_COLOR = "#2196F3" private const val DEFAULT_HIT_COLOR = "#3F51B5" private const val DEFAULT_ERROR_COLOR = "#F44336" 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 e059ad2..d5e32de 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorHitCellView.kt @@ -11,7 +11,7 @@ import android.support.annotation.ColorInt class DefaultIndicatorHitCellView(val styleDecorator: DefaultStyleDecorator) : IHitCellView { private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 3b9301d..f1ec108 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorLinkedLineView.kt @@ -12,7 +12,7 @@ import android.support.annotation.ColorInt class DefaultIndicatorLinkedLineView(val styleDecorator: DefaultStyleDecorator) : IIndicatorLinkedLineView { private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 50922a6..8888842 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultIndicatorNormalCellView.kt @@ -10,7 +10,7 @@ import android.graphics.Paint class DefaultIndicatorNormalCellView(val styleDecorator: DefaultStyleDecorator) : INormalCellView { private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 a3747d3..c7ac9e4 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerHitCellView.kt @@ -11,7 +11,7 @@ import android.support.annotation.ColorInt class DefaultLockerHitCellView(val styleDecorator: DefaultStyleDecorator) : IHitCellView { private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 67d6756..69edb7d 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerLinkedLineView.kt @@ -15,7 +15,7 @@ class DefaultLockerLinkedLineView(val styleDecorator: DefaultStyleDecorator) : I } private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 d1ce1a4..4f0e4b0 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/DefaultLockerNormalCellView.kt @@ -9,7 +9,7 @@ import android.graphics.Paint class DefaultLockerNormalCellView(val styleDecorator: DefaultStyleDecorator) : INormalCellView { private val paint: Paint by lazy { - Config.createPaint() + DefaultConfig.createPaint() } init { 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 daf2c3a..f71e84a 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/PatternIndicatorView.kt @@ -3,8 +3,8 @@ package com.github.ihsg.patternlocker import android.content.Context import android.graphics.Canvas import android.util.AttributeSet -import android.util.Log import android.view.View +import kotlin.math.min /** * Created by hsg on 20/09/2017. @@ -52,7 +52,7 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val a = Math.min(widthMeasureSpec, heightMeasureSpec) + val a = min(widthMeasureSpec, heightMeasureSpec) super.onMeasure(a, a) } @@ -70,11 +70,11 @@ 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) - 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)) + val normalColor = ta.getColor(R.styleable.PatternIndicatorView_piv_color, DefaultConfig.defaultNormalColor) + val fillColor = ta.getColor(R.styleable.PatternIndicatorView_piv_fillColor, DefaultConfig.defaultFillColor) + val hitColor = ta.getColor(R.styleable.PatternIndicatorView_piv_hitColor, DefaultConfig.defaultHitColor) + val errorColor = ta.getColor(R.styleable.PatternIndicatorView_piv_errorColor, DefaultConfig.defaultErrorColor) + val lineWidth = ta.getDimension(R.styleable.PatternIndicatorView_piv_lineWidth, DefaultConfig.getDefaultLineWidth(resources)) ta.recycle() @@ -116,18 +116,8 @@ class PatternIndicatorView @JvmOverloads constructor(context: Context, attrs: At } private fun drawCells(canvas: Canvas) { - if (this.hitCellView == null) { - Log.e(TAG, "drawCells(), hitCellView is null") - return - } - - if (this.normalCellView == null) { - Log.e(TAG, "drawCells(), normalCellView is null") - return - } - this.cellBeanList.forEach { - if (it.isHit) { + if (it.isHit && this.hitCellView != null) { this.hitCellView?.draw(canvas, it, this.isError) } else { this.normalCellView?.draw(canvas, it) 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 e694dc7..0112982 100644 --- a/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt +++ b/library/src/main/java/com/github/ihsg/patternlocker/PatternLockerView.kt @@ -3,10 +3,10 @@ package com.github.ihsg.patternlocker import android.content.Context import android.graphics.Canvas import android.util.AttributeSet -import android.util.Log import android.view.HapticFeedbackConstants import android.view.MotionEvent import android.view.View +import kotlin.math.min /** @@ -126,7 +126,7 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val a = Math.min(widthMeasureSpec, heightMeasureSpec) + val a = min(widthMeasureSpec, heightMeasureSpec) super.onMeasure(a, a) } @@ -165,16 +165,16 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { val ta = context.obtainStyledAttributes(attrs, R.styleable.PatternLockerView, defStyleAttr, 0) - 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)) + val normalColor = ta.getColor(R.styleable.PatternLockerView_plv_color, DefaultConfig.defaultNormalColor) + val hitColor = ta.getColor(R.styleable.PatternLockerView_plv_hitColor, DefaultConfig.defaultHitColor) + val errorColor = ta.getColor(R.styleable.PatternLockerView_plv_errorColor, DefaultConfig.defaultErrorColor) + val fillColor = ta.getColor(R.styleable.PatternLockerView_plv_fillColor, DefaultConfig.defaultFillColor) + val lineWidth = ta.getDimension(R.styleable.PatternLockerView_plv_lineWidth, DefaultConfig.getDefaultLineWidth(resources)) - this.freezeDuration = ta.getInteger(R.styleable.PatternLockerView_plv_freezeDuration, Config.defaultFreezeDuration) - this.enableAutoClean = ta.getBoolean(R.styleable.PatternLockerView_plv_enableAutoClean, Config.defaultEnableAutoClean) - this.enableHapticFeedback = ta.getBoolean(R.styleable.PatternLockerView_plv_enableHapticFeedback, Config.defaultEnableHapticFeedback) - this.enableSkip = ta.getBoolean(R.styleable.PatternLockerView_plv_enableSkip, Config.defaultEnableSkip) + this.freezeDuration = ta.getInteger(R.styleable.PatternLockerView_plv_freezeDuration, DefaultConfig.defaultFreezeDuration) + this.enableAutoClean = ta.getBoolean(R.styleable.PatternLockerView_plv_enableAutoClean, DefaultConfig.defaultEnableAutoClean) + this.enableHapticFeedback = ta.getBoolean(R.styleable.PatternLockerView_plv_enableHapticFeedback, DefaultConfig.defaultEnableHapticFeedback) + this.enableSkip = ta.getBoolean(R.styleable.PatternLockerView_plv_enableSkip, DefaultConfig.defaultEnableSkip) ta.recycle() @@ -186,7 +186,7 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun initData() { - Logger.enable = Config.defaultEnableLogger + Logger.enable = DefaultConfig.defaultEnableLogger this.hitIndexList.clear() } @@ -210,18 +210,8 @@ class PatternLockerView @JvmOverloads constructor(context: Context, attrs: Attri } private fun drawCells(canvas: Canvas) { - if (this.hitCellView == null) { - Log.e(TAG, "drawCells(), hitCellView is null") - return - } - - if (this.normalCellView == null) { - Log.e(TAG, "drawCells(), normalCellView is null") - return - } - this.cellBeanList.forEach { - if (it.isHit) { + if (it.isHit && this.hitCellView != null) { this.hitCellView?.draw(canvas, it, this.isError) } else { this.normalCellView?.draw(canvas, it)