From 43a68eceece18e3d355907b36a4b4eca91abb759 Mon Sep 17 00:00:00 2001 From: Masoud Ashrafzadeh Date: Sat, 1 Feb 2020 14:40:19 +0330 Subject: [PATCH] parts bug fix --- app/src/main/res/layout/activity_main.xml | 3 ++- .../java/com/sma6871/cardentry/CardEntry.kt | 22 ++++++++++++------- cardentry/src/main/res/values/attrs.xml | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4126203..430715d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -12,7 +12,8 @@ android:layout_height="wrap_content" android:text="6104337804957072" app:ce_number_count="16" - app:ce_has_animation="true" + app:ce_digit_size="24sp" + app:ce_part_count="4" app:ce_text_color="@color/coal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" diff --git a/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt b/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt index dd364de..a489a60 100644 --- a/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt +++ b/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt @@ -20,14 +20,17 @@ import java.util.* class CardEntry : AppCompatEditText { var maxLength = 16 // default length - var partsCount = 4 // AAAA BBBB CCCC DDDD + var partCount = 4 // AAAA BBBB CCCC DDDD private var mSpace = toPxF(16) private var mCharSize = toPxF(16) - private var mPartLength = maxLength / partsCount - private var mPartSize = mCharSize * mPartLength + private val mPartLength + get() = maxLength / partCount + private val mPartSize + get() = mCharSize * mPartLength private var mLineSpacing = toPxF(12) private var mLineSpacingAnimated = toPxF(12) + private var textWidths = FloatArray(maxLength) var hasAnimation = false @@ -88,9 +91,12 @@ class CardEntry : AppCompatEditText { val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CardEntry, 0, 0) if (typedArray.hasValue(R.styleable.CardEntry_ce_number_count)) { - maxLength = typedArray.getInt(R.styleable.CardEntry_ce_number_count, 4) + maxLength = typedArray.getInt(R.styleable.CardEntry_ce_number_count, 16) textWidths = FloatArray(maxLength) } + if (typedArray.hasValue(R.styleable.CardEntry_ce_part_count)) { + partCount = typedArray.getInt(R.styleable.CardEntry_ce_part_count, 4) + } if (typedArray.hasValue(R.styleable.CardEntry_ce_line_color)) { lineColor = typedArray.getColor( @@ -114,8 +120,8 @@ class CardEntry : AppCompatEditText { if (typedArray.hasValue(R.styleable.CardEntry_ce_has_animation)) hasAnimation = typedArray.getBoolean(R.styleable.CardEntry_ce_has_animation, false) - if (typedArray.hasValue(R.styleable.CardEntry_ce_has_line)) - hasLine = typedArray.getBoolean(R.styleable.CardEntry_ce_has_line, true) + if (typedArray.hasValue(R.styleable.CardEntry_ce_show_lines)) + hasLine = typedArray.getBoolean(R.styleable.CardEntry_ce_show_lines, true) if (typedArray.hasValue(R.styleable.CardEntry_ce_digit_size)) textPaint.textSize = typedArray.getDimension(R.styleable.CardEntry_ce_digit_size, textPaint.textSize) @@ -202,7 +208,7 @@ class CardEntry : AppCompatEditText { //draw lines var i = 0 if (hasLine) { - while (i < mPartLength) { + while (i < partCount) { linePaint.color = when { i < textLength / mPartLength -> filledLineColor else -> lineColor @@ -271,7 +277,7 @@ class CardEntry : AppCompatEditText { super.onMeasure(widthMeasureSpec, heightMeasureSpec) setMeasuredDimension( - (maxLength * mCharSize).toInt() + ((partsCount - 1) * mSpace).toInt() + paddingLeft + paddingRight, + (maxLength * mCharSize).toInt() + ((partCount - 1) * mSpace).toInt() + paddingLeft + paddingRight, measuredHeight ) diff --git a/cardentry/src/main/res/values/attrs.xml b/cardentry/src/main/res/values/attrs.xml index b2f5bc7..e35c77e 100644 --- a/cardentry/src/main/res/values/attrs.xml +++ b/cardentry/src/main/res/values/attrs.xml @@ -2,12 +2,12 @@ - + - +