由于某种原因,我无法使constraintset动画执行任何操作。调用代码没有任何反应。即使我更改tab
的ID
private fun moveNavigationBall(tab: BottombarItemView) {
val constraintSet = ConstraintSet()
constraintSet.clone(cl_bottombar_view_main)
constraintSet.connect(v_bottom_bar_circle_indicator.id, ConstraintSet.TOP, tab.id, ConstraintSet.TOP,0);
constraintSet.connect(v_bottom_bar_circle_indicator.id, ConstraintSet.BOTTOM, tab.id, ConstraintSet.BOTTOM,0);
constraintSet.connect(v_bottom_bar_circle_indicator.id, ConstraintSet.LEFT, tab.id, ConstraintSet.LEFT,0);
constraintSet.connect(v_bottom_bar_circle_indicator.id, ConstraintSet.RIGHT, tab.id, ConstraintSet.RIGHT,0);
constraintSet.applyTo(cl_bottombar_view_main)
val transition = ChangeBounds()
transition.interpolator = OvershootInterpolator()
TransitionManager.beginDelayedTransition(cl_bottombar_view_main, transition)
}
按下按钮时调用。选项卡因按下哪个按钮而异。我想做的是将
v_bottom_bar_circle_indicator
移动到每个选项卡的中间风景
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/cl_bottombar_view_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
>
<com.main.toucanmvp.mvp.components.bottombar.bottombaritem.BottombarItemView
android:id="@+id/biv_home"
android:layout_width="0dp"
android:layout_height="56dp"
app:bivBackground="@drawable/drawable_navigation_background_left"
app:bivIcon="@drawable/ic_home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/biv_search"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.main.toucanmvp.mvp.components.bottombar.bottombaritem.BottombarItemView
android:id="@+id/biv_search"
android:layout_width="0dp"
android:layout_height="56dp"
app:bivIcon="@drawable/ic_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/biv_clan"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/biv_home"
app:layout_constraintTop_toTopOf="parent"/>
<com.main.toucanmvp.mvp.components.bottombar.bottombaritem.BottombarItemView
android:id="@+id/biv_clan"
android:layout_width="0dp"
android:layout_height="56dp"
app:bivIcon="@drawable/ic_crown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/biv_news"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/biv_search"
app:layout_constraintTop_toTopOf="parent"/>
<com.main.toucanmvp.mvp.components.bottombar.bottombaritem.BottombarItemView
android:id="@+id/biv_news"
android:layout_width="0dp"
android:layout_height="56dp"
app:bivIcon="@drawable/ic_news"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/biv_profile"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/biv_clan"
app:layout_constraintTop_toTopOf="parent"/>
<com.main.toucanmvp.mvp.components.bottombar.bottombaritem.BottombarItemView
android:id="@+id/biv_profile"
android:layout_width="0dp"
android:layout_height="56dp"
app:bivBackground="@drawable/drawable_navigation_background_right"
app:bivIcon="@drawable/ic_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/biv_news"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:id="@+id/v_bottom_bar_circle_indicator"
android:layout_width="34dp"
android:layout_height="34dp"
android:background="@drawable/drawable_circle_navigation_bar"
app:layout_constraintBottom_toBottomOf="@+id/biv_home"
app:layout_constraintEnd_toEndOf="@+id/biv_home"
app:layout_constraintStart_toStartOf="@+id/biv_home"
app:layout_constraintTop_toTopOf="@+id/biv_home"/>
</android.support.constraint.ConstraintLayout>
最佳答案
首先,为了动画化约束的变化,您需要放置
constraintSet.applyTo(cl_bottombar_view_main)
后
TransitionManager.beginDelayedTransition(cl_bottombar_view_main, transition)
其次,在XML布局文件中,对要设置动画的视图(
start
)使用end
和v_bottom_bar_circle_indicator
约束,但是在代码中,您使用的是ConstraintSet.LEFT
和ConstraintSet.RIGHT
,这是不同的。您需要将它们分别更改为ConstraintSet.START
和ConstraintSet.END
。第三件事,在这种情况下可能会或可能无关紧要,是您将
0
的边距设置为每个connect()
函数的最后一个参数。 ConstraintSet
文档说,边距必须为正数,因此将其设置为0
时可能无法达到预期的效果。我建议在这种情况下使用不带margin参数的签名。