我正试图在RadioButton上使用translationX动画向右推动ImageView。这就是我创建的布局:
<android.support.constraint.ConstraintLayout
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"
>
<RadioButton
android:id="@+id/radio_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="10dp"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="15dp"
android:background="@drawable/my_image"
app:layout_constraintStart_toEndOf="@id/radio_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="8:5"
/>
哪个开始是这样的:
然后我以编程方式启动这个动画:
radioButton.animate().translationX(50).setDuration(500).start();
期待类似:(请注意,在此预览中宽度和高度都减小了,尊重layout_constraintDimensionRatio =“8:5”)
但不幸的是,只有RadioButton翻译(并在图像后面结束),而ImageView保持相同的大小和相同的位置。你能帮我理解为什么会这样吗?
我认为关键指令是ImageView中的app:layout_constraintStart_toEndOf="@id/radio_button"
,但我认为我错过了一些东西。
感谢任何帮助,谢谢大家
你看到的是translatioX
如何适用于Android视图:
public void setTranslationX(float translationX)
设置此视图相对于其左侧位置的水平位置。除了对象的布局放置它之外,这有效地定位了布局后的对象。
斜体是我的。这里的关键词是“后布局”。设置translationX
只会影响视图的位置,并且不会更改任何其他视图的位置。换句话说,所有其他视图的行为就像translationX == 0
。
你仍然可以达到你想要的效果,但你必须使用边距或者TransitionManager以不同的方式接近它。搜索一些想法的“ConstraintLayout动画”。