[英]Translation and scale layout Animation Problem in Android
I have the view with layout below.我有下面布局的视图。
What I want is moving View B to the position of View A and at the same time move RelativeLayout C up with the same height of View B. The two actions will be done with the animations.我想要的是将视图 B 移动到视图 A 的 position 并同时将 RelativeLayout C 向上移动到视图 B 的相同高度。这两个动作将通过动画完成。 Like the picture shown below.如下图所示。
I am using ObjectAnimation to implement this feature, but when I am using the我正在使用 ObjectAnimation 来实现此功能,但是当我使用
float viewBY = viewB.getTranslationY();
ObjectAnimator viewBMoveUp
= ObjectAnimator.ofFloat(viewB, "translationY",
viewBY, viewBY - viewB.getHeight());
float layoutCCurrentY = layoutC.getY();
ObjectAnimator layoutCMoveUp
= ObjectAnimator.ofFloat(layoutC, "Y",
layoutCCurrentY, layoutCCurrentY - viewB.getHeight());
AnimatorSet animSet = new AnimatorSet();
animSet.play(viewBMoveUp).with(layoutCMoveUp);
animSet.setDuration(150);
animSet.start();
I find the LayoutC's bottom is also up with viewB.getHeight(), which is not I expected.我发现 LayoutC 的底部也与 viewB.getHeight() 一起上升,这不是我所期望的。 Like the picture below:如下图所示:
So anybody can help about this?所以有人可以帮忙吗?
One simpler way to achieve the expected result is to use animateLayoutChanges
attribute in the parent layout.实现预期结果的一种更简单的方法是在父布局中使用animateLayoutChanges
属性。
<!-- Defines whether changes in layout (caused by adding and removing items) should cause
a LayoutTransition to run. When this flag is set to true, a default LayoutTransition object
will be set on the ViewGroup container and default animations will run when these layout
changes occur.-->
<attr name="animateLayoutChanges" format="boolean" />
With this, you don't need to manually animate each movement.有了这个,您不需要手动为每个动作设置动画。 When you set viewA
visibility to View.GONE
, the parent layout will fade out viewA
and translate position of viewB
and layoutC
.当您将 viewA 的可见性设置为viewA
View.GONE
,父布局将淡出viewA
并转换 viewB 和layoutC
viewB
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<View
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="60dp" />
<View
android:id="@+id/viewB"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/viewA" />
<RelativeLayout
android:id="@+id/layoutC"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewB"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.