layout_gravity上的动画
在Android中,是否可以在layout_gravity
上应用动画?例如,假设我想将View
(例如Button
)的layout_gravity
从右更改为左
In Android, is it possible to apply an animation on layout_gravity
? for example suppose I want to change the layout_gravity
of a View
(e.g. Button
) from right to left
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="right|top" />
</FrameLayout>
在上述布局文件中,我想在运行时通过动画将布局重力从right|top
更改为left|top
,这有可能吗?谢谢
In the above layout file I want to change layout gravity from right|top
to left|top
at run-time with animation, is it possible at all? Thanks
您可以在父级ViewGroup上使用LayoutTransition使其动画化对子级视图的更改,例如布局更改.在下面的示例中,我为从屏幕右下角到右上角的浮动动作按钮设置了动画,同时旋转了它(因此"+"图标变成了"x"图标).
You can use a LayoutTransition on the parent ViewGroup to make it animate changes to child views, such as layout changes. In the example below, I animate a floating action button from bottom right of the screen to top right, whilst also rotating it (so the '+' icon becomes an 'x' icon).
在包含的布局上将animateLayoutChanges设置为true:
Set animateLayoutChanges to true on your containing layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pnlFabContainer"
android:animateLayoutChanges="true"
android:clipChildren="false">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:id="@+id/lstWhatever"
android:divider="@color/DividerDarkGrey"
android:dividerHeight="1dp"
android:choiceMode="singleChoice"
android:groupIndicator="@null"
android:padding="20dp"/>
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fabMultiSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="24dp"
android:src="@drawable/button_plus"
android:scaleType="center"
fab:fab_type="normal"
fab:fab_colorNormal="@color/FloatingActionButton"
fab:fab_colorPressed="@color/FloatingActionButtonPressed"
fab:fab_colorRipple="@color/FloatingActionButtonRipple" />
</FrameLayout>
在您的容器上启用更改"过渡:
Enable the 'changing' transition on your container:
FrameLayout pnlFabContainer = (FrameLayout)view.findViewById(R.id.pnlFabContainer);
LayoutTransition transition = pnlFabContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
animateFabButton(fab);
然后以编程方式更改布局的重力-我同时也在做旋转动画:
Then change your layout gravity programmatically - I'm also doing a rotate animation at the same time:
protected void animateFabButton(FloatingActionButton fab) {
if (fab.isSelected()) {
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)fab.getLayoutParams();
layoutParams.gravity = Gravity.TOP | Gravity.END;
layoutParams.topMargin = -(fab.getMeasuredHeight() / 2);
fab.setLayoutParams(layoutParams);
Animation rotate = new RotateAnimation(0, 45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setFillBefore(true);
rotate.setFillAfter(true);
rotate.setFillEnabled(true);
rotate.setDuration(750);
rotate.setRepeatCount(0);
rotate.setInterpolator(new LinearInterpolator());
fab.startAnimation(rotate);
}
else {
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)fab.getLayoutParams();
layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
layoutParams.topMargin = layoutParams.bottomMargin;
fab.setLayoutParams(layoutParams);
Animation rotate = new RotateAnimation(45, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setFillBefore(true);
rotate.setFillAfter(true);
rotate.setFillEnabled(true);
rotate.setDuration(750);
rotate.setRepeatCount(0);
rotate.setInterpolator(new LinearInterpolator());
fab.startAnimation(rotate);
}
}