Skip to content

Commit d459047

Browse files
committed
new anim & customisation
1 parent 4580f70 commit d459047

5 files changed

Lines changed: 65 additions & 8 deletions

File tree

app/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
app:ctv_IconTint="@color/colorPrimary"
1717
app:ctv_IconChecked="true"
1818
app:ctv_Text="@string/app_name"
19+
app:ctv_AnimDuration="2000"
20+
app:ctv_AnimType="translate"
1921
android:layout_width="match_parent"
2022
android:layout_height="wrap_content"/>
2123

@@ -38,6 +40,8 @@
3840
app:ctv_TextStyle="@style/TextAppearance.General"
3941
app:ctv_IconTint="@color/colorAccent"
4042
app:ctv_IconChecked="true"
43+
app:ctv_AnimType="scale"
44+
app:ctv_AnimDuration="2000"
4145
app:ctv_Icon="@drawable/ic_cancel_custom_vector"
4246
app:ctv_Text="@string/app_name"
4347
android:layout_width="match_parent"

checkabletextview/src/main/java/com/devzone/checkabletextview/CheckableTextView.kt

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ import kotlinx.android.synthetic.main.layout_checkable_text.view.*
1818

1919
class CheckableTextView : RelativeLayout {
2020

21+
22+
companion object {
23+
const val SCALE = 0
24+
const val TRANSLATE = 1
25+
}
26+
27+
private val defaultAnimDuration: Long = 250
2128
private var isChecked: Boolean = true
2229
private var listener: CheckedListener? = null
2330
private val defaultCheckIcon = R.drawable.ic_check_circle_vector
2431
private val defaultTextColor = android.R.color.black
2532
private val defaultIconTintColor = android.R.color.transparent
2633
private var checkIcon = defaultCheckIcon
34+
private var animateStyle = SCALE
35+
private var animDuration: Long = defaultAnimDuration
2736

2837
constructor(context: Context) : super(context) {
2938
init(context, null)
@@ -65,13 +74,16 @@ class CheckableTextView : RelativeLayout {
6574
val textStyle = array.getResourceId(R.styleable.CheckableTextView_ctv_TextStyle, 0)
6675
checkIcon = array.getResourceId(R.styleable.CheckableTextView_ctv_Icon, 0)
6776
val gravity = array.getInt(R.styleable.CheckableTextView_ctv_TextGravity, Gravity.CENTER)
77+
animateStyle = array.getInt(R.styleable.CheckableTextView_ctv_AnimType, SCALE)
78+
val animDuration =
79+
array.getInt(R.styleable.CheckableTextView_ctv_AnimDuration, defaultAnimDuration.toInt()).toLong()
80+
setAnimDuration(animDuration)
6881

6982
//giving applied style attrs least preference (colors n text size will be override by ctv_TextColor & ctv_TextSize as applied later)
7083
applyTextStyle(textStyle, context)
7184
validateCheckIcon(context)
72-
checkedTextTV.text = text
73-
checkedTextTV.isSelected = true
74-
checkedTextTV.gravity = gravity
85+
setText(text ?: "")
86+
setTextGravity(gravity)
7587
checkedTextTV.setTextColor(textColor)
7688
checkedIV.setImageResource(checkIcon)
7789

@@ -109,10 +121,27 @@ class CheckableTextView : RelativeLayout {
109121

110122
private fun animateView(view: View, show: Boolean) {
111123
view.clearAnimation()
112-
val scale = if (show) 1f else 0f
113-
val rotation = if (show) 360f else -360f
114-
view.animate().setStartDelay(20).scaleX(scale).scaleY(scale).rotation(rotation).setDuration(250)
115-
.start()
124+
125+
when (animateStyle) {
126+
SCALE -> {
127+
view.translationX = 0f
128+
val scale = if (show) 1f else 0f
129+
val rotation = if (show) 0f else -360f
130+
view.animate().setStartDelay(20).scaleX(scale).scaleY(scale).rotation(rotation)
131+
.setDuration(animDuration)
132+
.start()
133+
}
134+
TRANSLATE -> {
135+
view.scaleX = 1f
136+
view.scaleY = 1f
137+
val translate = if (show) 0f else (view.width.toFloat() + view.width / 2)
138+
val rotation = if (show) 0f else 360f
139+
view.animate().setStartDelay(20).translationX(translate).rotation(rotation).setDuration(animDuration)
140+
.start()
141+
}
142+
143+
}
144+
116145
}
117146

118147
private fun validateCheckIcon(context: Context) {
@@ -209,8 +238,10 @@ class CheckableTextView : RelativeLayout {
209238
}
210239

211240
fun setText(text: String) {
212-
if (emptyNullCheck(text))
241+
if (emptyNullCheck(text)) {
213242
checkedTextTV.text = text
243+
checkedTextTV.isSelected = true
244+
}
214245
}
215246

216247
fun setTextGravity(gravity: Int) {
@@ -229,4 +260,21 @@ class CheckableTextView : RelativeLayout {
229260
if (isValidRes(resId))
230261
applyTextStyle(resId, context)
231262
}
263+
264+
/**
265+
* @param animType should be [SCALE] OR [TRANSLATE]
266+
*/
267+
fun setAnimStyle(animType: Int) {
268+
animateStyle = when (animType) {
269+
SCALE -> SCALE
270+
TRANSLATE -> TRANSLATE
271+
else -> SCALE
272+
}
273+
}
274+
275+
fun setAnimDuration(duration: Long) {
276+
if (duration.toInt() == 0 || duration < 0) return
277+
animDuration = duration
278+
}
279+
232280
}

checkabletextview/src/main/res/values/attrs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<attr name="ctv_TextStyle" format="reference|integer"/>
1010
<attr name="ctv_Icon" format="reference|integer"/>
1111
<attr name="ctv_IconChecked" format="boolean"/>
12+
<attr name="ctv_AnimDuration" format="integer"/>
13+
<attr name="ctv_AnimType" format="enum">
14+
<enum name="scale" value="0"/>
15+
<enum name="translate" value="1"/>
16+
</attr>
1217
<attr name="ctv_TextGravity">
1318
<flag name="bottom" value="80" />
1419
<flag name="center" value="17" />

sample_01.gif

254 KB
Loading

sample_slowmo.gif

217 KB
Loading

0 commit comments

Comments
 (0)