forked from Blue-knife/Android-EasyTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialEditText.kt
130 lines (102 loc) · 3.65 KB
/
MaterialEditText.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.example.ui.customView
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.example.core.ToolsUtils.dip2px
import com.example.ui.R
class MaterialEditText(context: Context?, attrs: AttributeSet?) :
AppCompatEditText(context, attrs) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
//文字高度
private val mTextSize = dip2px(12f)
//文字和输入框间距
private val mTextMargin = dip2px(8f)
//垂直偏移
private val mTextVerticalOffset = dip2px(22f)
//纵向偏移
private val mTextHorizontalOffset = dip2px(5f)
//文字动画偏移量
private val mTextAnimationOffset = dip2px(16f)
private var isFloatingLabel: Boolean = true
val backgroundPadding = Rect()
val animator: ObjectAnimator by lazy {
ObjectAnimator.ofFloat(
this@MaterialEditText, "floatingLabelFaction", 0f, 1f
)
}
//动画是否显示
private var floatingLabelShown = false
private var floatingLabelFaction = 0f
set(value) {
field = value
invalidate()
}
init {
val typeArray = context?.obtainStyledAttributes(attrs, R.styleable.MaterialEditText)
isFloatingLabel = typeArray!!.getBoolean(R.styleable.MaterialEditText_floatingLabel, true)
typeArray.recycle()
init()
}
fun init() {
background.getPadding(backgroundPadding)
onFloatingLabelChange()
paint.textSize = mTextSize.toFloat()
addChangeListener()
}
private fun addChangeListener() {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) =
Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (isFloatingLabel) {
if (floatingLabelShown && TextUtils.isEmpty(s)) {
animator.reverse()
floatingLabelShown = false
} else if (!floatingLabelShown && !TextUtils.isEmpty(s)) {
floatingLabelShown = true
animator.start()
}
}
}
})
}
fun isFloatingLabel(isFloatingLabel: Boolean) {
if (this.isFloatingLabel != isFloatingLabel) {
this.isFloatingLabel = isFloatingLabel
onFloatingLabelChange()
}
}
private fun onFloatingLabelChange() {
if (isFloatingLabel) {
setPadding(
paddingLeft, backgroundPadding.top + (mTextSize + mTextMargin).toInt(),
paddingRight, paddingBottom
)
} else {
setPadding(
paddingLeft, backgroundPadding.top,
paddingRight, paddingBottom
)
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
paint.alpha = (floatingLabelFaction * 0xff).toInt()
//计算偏移量
val extraOffset = mTextAnimationOffset * (1 - floatingLabelFaction)
//绘制提示信息
canvas.drawText(
hint as String, mTextHorizontalOffset.toFloat(),
mTextVerticalOffset + (-extraOffset),
paint
)
}
}