我想一种可能的替代方法是使用 ConstraintLayout 的转换,如此处所示。
要实现,它似乎必须使用 2 个相似的布局,每个视图具有相同的 id,然后您可以在各个阶段之间切换,如下所示:
class MainActivity : AppCompatActivity() {
private var show = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.circuit)
backgroundImage.setOnClickListener {
if(show)
hideComponents() // if the animation is shown, we hide back the views
else
showComponents() // if the animation is NOT shown, we animate the views
}
}
private fun showComponents(){
show = true
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit_detail)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
private fun hideComponents(){
show = false
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
}