class MainActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityMainBinding; lateinit var objectAnimator: ObjectAnimator;
@SuppressLint("ObjectAnimatorBinding") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) val viewWrapper = ViewWrapper(mBinding.vc) objectAnimator = ObjectAnimator.ofInt(viewWrapper, "trueHeight", dp2px(500f), dp2px(400f)).setDuration(1000)
objectAnimator.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { super.onAnimationEnd(animation) }
override fun onAnimationStart(animation: Animator?) { super.onAnimationStart(animation) } }) mBinding.vc?.setOnClickListener {
objectAnimator.start() }
}
private fun dp2px(dpValue: Float): Int { val scale: Float = resources.displayMetrics.density return (dpValue * scale + 0.5f).toInt() }
fun pxToDp( px: Int): Int { val displayMetrics: DisplayMetrics = resources.displayMetrics return (px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)).roundToInt() }
private class ViewWrapper(view: View) { private val mTarget: View = view var trueHeight: Int get() = mTarget.layoutParams.height set(height) { mTarget.layoutParams.height = height mTarget.requestLayout() } } }
|