Skip to content

Commit

Permalink
parts bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Masoud Ashrafzadeh committed Feb 1, 2020
1 parent c7eff89 commit 43a68ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 14 additions & 8 deletions cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions cardentry/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<resources>
<declare-styleable name="CardEntry">
<attr name="ce_number_count" format="integer"/>
<attr name="ce_part_count" format="dimension"/>
<attr name="ce_part_count" format="integer"/>
<attr name="ce_line_color" format="color"/>
<attr name="ce_filled_line_color" format="color"/>
<attr name="ce_text_color" format="color"/>
<attr name="ce_has_animation" format="boolean"/>
<attr name="ce_has_line" format="boolean"/>
<attr name="ce_show_lines" format="boolean"/>
<attr name="ce_digit_size" format="dimension"/>
<attr name="ce_digit_space" format="dimension"/>
<attr name="ce_digit_width" format="dimension"/>
Expand Down

0 comments on commit 43a68ec

Please sign in to comment.