我有一个以ImageView为中心的RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/splashActivity_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yarkoni.anybody.activities.SplashScreenActivity">
<ImageView
android:id="@+id/splashActivity_logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="top"
android:layout_margin="@dimen/spacing_medium"
android:layout_centerInParent="true"
android:background="@color/colorAccent" />
</RelativeLayout>
我正在尝试将视图设置为底部:
TransitionManager.beginDelayedTransition(mRoot, new ChangeBounds().addTarget(mLogoIV).setStartDelay(1000).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(5000));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLogoIV.getLayoutParams();
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLogoIV.setLayoutParams(params);
没有过渡布局,如下所示:
通过转换,布局如下所示:
问题在于动画并没有实际发生,而ImageView只是立即出现在底部。
编辑1:
以下解决了问题:
mRoot.post(new Runnable() {
@Override
public void run() {
TransitionManager.beginDelayedTransition(mRoot, new ChangeBounds().addTarget(mLogoIV).setStartDelay(1000).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(5000));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLogoIV.getLayoutParams();
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLogoIV.setLayoutParams(params);
}
});
但现在我的问题是为什么当使用处理程序时它仍然不起作用:
new Handler().post(new Runnable() {
@Override
public void run() {
TransitionManager.beginDelayedTransition(mRoot, new ChangeBounds().addTarget(mLogoIV).setStartDelay(1000).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(5000));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLogoIV.getLayoutParams();
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLogoIV.setLayoutParams(params);
}
});