在Android中,是否可以在layout_gravity
上应用动画?例如,假设我想从右到左更改layout_gravity
的{{1}}(例如View
)
Button
在上面的布局文件中,我想在动画运行时将布局重力从<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
,是否可以?感谢
答案 0 :(得分:6)
您可以在父ViewGroup上使用LayoutTransition,使其对子视图的更改进行动画处理,例如布局更改。在下面的示例中,我将屏幕右下方的浮动操作按钮设置为右上角,同时还旋转它(因此&#39; +&#39;图标变为&#39; x&#39;图标)
在包含的布局上将animateLayoutChanges设置为true:
<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>
启用&#39;更改&#39;容器上的转换:
FrameLayout pnlFabContainer = (FrameLayout)view.findViewById(R.id.pnlFabContainer);
LayoutTransition transition = pnlFabContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
animateFabButton(fab);
然后以编程方式更改布局重力 - 我也同时进行旋转动画:
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);
}
}
答案 1 :(得分:1)
layout_gravity属性无法实现。您可以通过永久运行的平移动画来实现效果。
Animation animation = new TranslateAnimation(0, 500,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
myImage.startAnimation(animation);
此代码仅在x轴上将位置更改为+500