Skip to content

Commit

Permalink
修复跳过中间点问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huangshouguo committed Dec 31, 2020
1 parent f0177b4 commit 18b2e13
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 96 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 "androidx.appcompat:appcompat:$rootProject.ext.appcompatVersion"
implementation "androidx.core:core-ktx:$rootProject.ext.coreKtxVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class RippleLockerHitCellView : IHitCellView {
val saveCount = canvas.save()

this.paint.color = getColor(isError) and 0x14FFFFFF
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius, this.paint)

this.paint.color = getColor(isError) and 0x43FFFFFF
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius * 2f / 3f, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius * 2f / 3f, this.paint)

this.paint.color = getColor(isError)
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius / 3f, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius / 3f, this.paint)

canvas.restoreToCount(saveCount)
}
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
buildscript {
// versions
ext {
versionCode = 20
versionName = '2.5.6'
versionCode = 21
versionName = '2.5.7'

minSdkVersion = 15
targetSdkVersion = 29
compileSdkVersion = 29
buildToolsVersion = '29.0.3'

appcompatVersion = '1.1.0'
coreKtxVersion = '1.2.0'
appcompatVersion = '1.2.0'
coreKtxVersion = '1.3.2'
constraintLayoutVersion = '1.1.3'
kotlinVersion = '1.3.71'
kotlinVersion = '1.4.21'
}

repositories {
Expand All @@ -23,7 +23,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Apr 19 11:09:52 CST 2019
#Tue Dec 29 15:08:36 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
33 changes: 25 additions & 8 deletions library/src/main/java/com/github/ihsg/patternlocker/CellBean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,40 @@ import kotlin.math.sqrt
* 3 4 5
* 6 7 8
*
* @param x 表示该cell的x坐标(相对坐标)
* @param y 表示该cell的y坐标(相对坐标)
* @param x 表示该cell的x坐标(点坐标)
* @param y 表示该cell的y坐标(点坐标)
* x,y 点坐标编号如下:
* (0,0) (1,0) (2,0)
* (0,1) (1,1) (2,1)
* (0,2) (1,2) (2,2)
*
* @param centerX 表示该cell的圆心x坐标(相对坐标)
* @param centerY 表示该cell的圆心y坐标(相对坐标)
* centerX, centerY 圆心坐标如下:
* (radius, radius) (4radius, radius) (7radius, radius)
* (radius, 4radius) (4radius, 4radius) (7radius, 4radius)
* (radius, 7radius) (4radius, 7radius) (7radius, 7radius)
*
* @param radius 表示该cell的半径
* @param isHit 表示该cell是否被设置的标记
*/
data 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: Int,
val y: Int,
val centerX: Float,
val centerY: Float,
val radius: Float,
var isHit: Boolean = false) {
/**
* 是否触碰到该view
*
* @param x
* @param y
* @return
*/
fun of(x: Float, y: Float, enableSkip: Boolean): Boolean {
val dx = this.x - x
val dy = this.y - y
val r = if (enableSkip) this.radius else this.radius * 1.5f
return sqrt((dx * dx + dy * dy).toDouble()) <= r.toDouble()
fun of(x: Float, y: Float): Boolean {
val dx = this.centerX - x
val dy = this.centerY - y
return sqrt((dx * dx + dy * dy)).toDouble() <= this.radius.toDouble()
}
}
29 changes: 15 additions & 14 deletions library/src/main/java/com/github/ihsg/patternlocker/CellFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ package com.github.ihsg.patternlocker
* Created by hsg on 20/09/2017.
*/

internal class CellFactory(private val width: Int, private val height: Int) {
val cellBeanList: List<CellBean> by lazy {
val result = ArrayList<CellBean>()
result.clear()
object CellFactory {

val pWidth = this.width / 8f
val pHeight = this.height / 8f
fun buildCells(width: Int, height: Int): List<CellBean> {
val result = ArrayList<CellBean>()
val pWidth = width / 8f
val pHeight = height / 8f

for (i: Int in 0..2) {
for (j: Int in 0..2) {
val id = (i * 3 + j) % 9
val x = (j * 3 + 1) * pWidth
val y = (i * 3 + 1) * pHeight
result.add(CellBean(id, x, y, pWidth))
}
for (i in 0..8) {
result.add(CellBean(i,
i % 3,
i / 3,
(i % 3 * 3 + 1) * pWidth,
(i / 3 * 3 + 1) * pHeight,
pWidth))
}

Logger.d("CellFactory", "result = $result")
return@lazy result

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal object DefaultConfig {
return paint
}

private fun convertDpToPx(dp: Float, resources: Resources): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)
private fun convertDpToPx(dpValue: Float, resources: Resources): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, resources.displayMetrics)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class DefaultIndicatorHitCellView(val styleDecorator: DefaultStyleDecorator
val saveCount = canvas.save()

this.paint.color = this.getColor(isError)
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius, this.paint)

canvas.restoreToCount(saveCount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ open class DefaultIndicatorLinkedLineView(val styleDecorator: DefaultStyleDecora
if (0 <= it && it < cellBeanList.size) {
val c = cellBeanList[it]
if (first) {
path.moveTo(c.x, c.y)
path.moveTo(c.centerX, c.centerY)
first = false
} else {
path.lineTo(c.x, c.y)
path.lineTo(c.centerX, c.centerY)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ open class DefaultIndicatorNormalCellView(val styleDecorator: DefaultStyleDecora

//outer circle
this.paint.color = this.styleDecorator.normalColor
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius, this.paint)

//inner circle
this.paint.color = this.styleDecorator.fillColor
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius - this.styleDecorator.lineWidth, this.paint)

canvas.restoreToCount(saveCount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ open class DefaultLockerHitCellView(val styleDecorator: DefaultStyleDecorator) :

// draw outer circle
this.paint.color = this.getColor(isError)
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius, this.paint)

// draw fill circle
this.paint.color = this.styleDecorator.fillColor
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius - this.styleDecorator.lineWidth, this.paint)

// draw inner circle
this.paint.color = this.getColor(isError)
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius / 5f, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius / 5f, this.paint)

canvas.restoreToCount(saveCount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ open class DefaultLockerLinkedLineView(val styleDecorator: DefaultStyleDecorator
if (0 <= it && it < cellBeanList.size) {
val c = cellBeanList[it]
if (first) {
path.moveTo(c.x, c.y)
path.moveTo(c.centerX, c.centerY)
first = false
} else {
path.lineTo(c.x, c.y)
path.lineTo(c.centerX, c.centerY)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ open class DefaultLockerNormalCellView(val styleDecorator: DefaultStyleDecorator

// draw outer circle
this.paint.color = this.styleDecorator.normalColor
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius, this.paint)

// draw fill circle
this.paint.color = this.styleDecorator.fillColor
canvas.drawCircle(cellBean.x, cellBean.y, cellBean.radius - this.styleDecorator.lineWidth, this.paint)
canvas.drawCircle(cellBean.centerX, cellBean.centerY, cellBean.radius - this.styleDecorator.lineWidth, this.paint)

canvas.restoreToCount(saveCount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,20 @@ open class PatternIndicatorView @JvmOverloads constructor(context: Context, attr
var hitCellView: IHitCellView? = null

private var isError: Boolean = false
private val hitIndexList: MutableList<Int> by lazy {
mutableListOf<Int>()
}
private var hitIndexList = emptyList<Int>()
private val cellBeanList: List<CellBean> by lazy {
val w = this.width - this.paddingLeft - this.paddingRight
val h = this.height - this.paddingTop - this.paddingBottom
CellFactory(w, h).cellBeanList
CellFactory.buildCells(w, h)
}

init {
init(context, attrs, defStyleAttr)
}

fun updateState(hitIndexList: List<Int>?, isError: Boolean) {
//1. clear pre state
if (this.hitIndexList.isNotEmpty()) {
this.hitIndexList.clear()
}

//2. record new state
hitIndexList?.let {
this.hitIndexList.addAll(it)
}

//3. update result
this.hitIndexList = hitIndexList ?: emptyList()
this.isError = isError

//4. update view
invalidate()
}

Expand All @@ -64,7 +50,6 @@ open class PatternIndicatorView @JvmOverloads constructor(context: Context, attr

private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
this.initAttrs(context, attrs, defStyleAttr)
this.initData()
}

private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
Expand All @@ -84,40 +69,32 @@ open class PatternIndicatorView @JvmOverloads constructor(context: Context, attr
this.linkedLineView = DefaultIndicatorLinkedLineView(decorator)
}

private fun initData() {
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
}
}
this.hitIndexList.forEach {
if (0 <= it && it < this.cellBeanList.size) {
this.cellBeanList[it].isHit = true
}
}
}

private fun drawLinkedLine(canvas: Canvas) {
if (this.hitIndexList.isNotEmpty()) {
this.linkedLineView?.draw(canvas,
this.hitIndexList,
this.cellBeanList,
this.isError)
this.hitIndexList,
this.cellBeanList,
this.isError)
}
}

private fun drawCells(canvas: Canvas) {
this.cellBeanList.forEach {
if (it.isHit && this.hitCellView != null) {
if (it.isHit) {
this.hitCellView?.draw(canvas, it, this.isError)
} else {
this.normalCellView?.draw(canvas, it)
Expand Down
Loading

0 comments on commit 18b2e13

Please sign in to comment.