Last active
May 21, 2019 09:36
-
-
Save hwd6190128/caab0c53daed985eedced13c4d3b1215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import android.view.animation.Animation | |
import android.view.animation.Transformation | |
/** | |
* Created by Howard Chang on 2018/10/25. | |
*/ | |
object AnimUtils { | |
fun expand(v: View, itemCount: Int) { | |
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) | |
, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) | |
val targetHeight = v.measuredHeight * itemCount | |
// Older versions of android (pre API 21) cancel animations for views with a height of 0. | |
v.layoutParams.height = 1 | |
v.visibility = View.VISIBLE | |
val a = object : Animation() { | |
override fun applyTransformation(interpolatedTime: Float, t: Transformation) { | |
v.layoutParams.height = if (interpolatedTime == 1f) | |
targetHeight | |
else | |
(targetHeight * interpolatedTime).toInt() | |
v.requestLayout() | |
} | |
override fun willChangeBounds(): Boolean { | |
return true | |
} | |
} | |
// 1dp/ms | |
a.duration = ((targetHeight / v.context.resources.displayMetrics.density).toInt()) * 2.toLong() | |
v.startAnimation(a) | |
} | |
fun collapse(v: View) { | |
val initialHeight = v.measuredHeight | |
val a = object : Animation() { | |
override fun applyTransformation(interpolatedTime: Float, t: Transformation) { | |
if (interpolatedTime == 1f) { | |
v.visibility = View.GONE | |
} else { | |
v.layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt() | |
v.requestLayout() | |
} | |
} | |
override fun willChangeBounds(): Boolean { | |
return true | |
} | |
} | |
// 1dp/ms | |
a.duration = ((initialHeight / v.context.resources.displayMetrics.density).toInt()) * 2.toLong() | |
v.startAnimation(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment