在Android中,可以在layout_gravity上应用动画吗?例如,假设我想要将View(例如Button)的layout_gravity从右改为左
<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,这是可能的吗?谢谢
发布于 2015-10-18 06:39:50
您可以在父ViewGroup上使用LayoutTransition,使其具有对子视图的更改的动画效果,例如布局更改。在下面的例子中,我从屏幕的右下角到右上角制作了一个浮动的动作按钮,同时也旋转了它(这样'+‘图标就变成了'x’图标)。
在包含布局上将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>在您的容器上启用“更改”转换:
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);
}
}发布于 2014-01-25 19:47:23
这在layout_gravity属性中是不可能的。你可以在永远运行的平移动画的帮助下达到这个效果。
Animation animation = new TranslateAnimation(0, 500,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
myImage.startAnimation(animation);此代码将仅将x轴上的位置更改为+500
https://stackoverflow.com/questions/21349989
复制相似问题