Skip to content

Instantly share code, notes, and snippets.

@karishmaagr
Created March 12, 2021 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karishmaagr/40d59f40837e92394cbd7a39f249a72b to your computer and use it in GitHub Desktop.
Save karishmaagr/40d59f40837e92394cbd7a39f249a72b to your computer and use it in GitHub Desktop.
private fun setupLoadingStart(
view: View,
icon: Icon?,
text1: TextView,
text2: TextView,
animate: Boolean = true,
combine: (() -> Unit)? = null
) {
try {
view.visibility = View.VISIBLE
view.tv_ob_loading.text = ""
view.iv_ob_loading.visibility = View.VISIBLE
view.iv_ob_loading.animate().alpha(1f).setDuration(if (animate) 1000 else 0)
.setInterpolator(interpolator).start()
val iconRes = icon?.iconRes ?: 0
if (iconRes != 0)
view.iv_ob_loading.setImageResource(iconRes)
else
BaseImageLoader.loadImage(this, icon?.icon, view.iv_ob_loading)
val pbObLoading = view.pb_ob_loading
val bounds = pbObLoading.indeterminateDrawable.copyBounds()
progressDrawable?.bounds = bounds
progressFinalDrawable?.bounds = bounds
pbObLoading.indeterminateDrawable = progressDrawable
text1.visibility = View.VISIBLE
text1.text = icon?.loadingText
text1.animate().alpha(1f).setDuration(if (animate) 1100 else 0)
.setInterpolator(interpolator).start()
text2.visibility = View.VISIBLE
text2.text = icon?.finalText
text2.animate().alpha(1f).setDuration(if (animate) 1100 else 0)
.setInterpolator(interpolator).start()
text1.post {
try {
val textAlpha = ObjectAnimator.ofFloat(text1, TextView.ALPHA, 1f, 0f)
textAlpha.duration = if (animate) 600 else 0
val textScale = ValueAnimator.ofFloat(1f, 0f)
val width = text1.width
var doOnce = true
textScale.addUpdateListener {
try {
val animatedFraction = it.animatedValue as Float
val layoutParams = text1.layoutParams
layoutParams.width = (animatedFraction * width).toInt()
text1.layoutParams = layoutParams
if (animatedFraction <= 0.03 && doOnce) {
val lp = text2.layoutParams as ConstraintLayout.LayoutParams
lp.startToEnd = view.id
text2.layoutParams = lp
doOnce = false
}
} catch (e: Exception) {
}
}
textScale.duration = if (animate) 600 else 0
textScale.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
textScaleAnimationEnd()
}
override fun onAnimationCancel(animation: Animator?) {
super.onAnimationCancel(animation)
textScaleAnimationEnd()
}
private fun textScaleAnimationEnd() {
try {
text1.visibility = View.GONE
pbObLoading.indeterminateDrawable = progressFinalDrawable
view.postDelayed({
try {
combine?.invoke()
} catch (e: Exception) {
}
}, if (animate) 600 else 0)
} catch (e: Exception) {
}
}
})
val animator = AnimatorSet()
animator.startDelay = if (animate) 1500 else 0
animator.interpolator = interpolator
animator.playSequentially(textAlpha, textScale)
animator.start()
} catch (e: Exception) {
}
}
} catch (e: Exception) {
//handle exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment