0

I have a RelativeLayout with an ImageView centered:

<?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>

I am trying to animate the view to bottom:

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);

Without the transition layout looks like this:

enter image description here

With the transition the layout looks like this:

enter image description here

The problem is that the animation doesn't actually occur instead the ImageView just appears on the bottom instantly.

EDIT 1:

The following fixes the problem:

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);

    }
});

But now my question is why when using handler it still does not work:

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);

    }
});
JY2k
  • 2,879
  • 1
  • 31
  • 60
  • 1
    Try using postDelayed() - animating while the view is building itself may be tricky – Nati Dykstein Dec 26 '16 at 10:05
  • 2
    are you trying to run that code in onCreate()? If so, it might not be possible for the animation to run as the layout it also running. Posting it with a View works because it gets executed after the view is fully functional. So if you are trying to use it with a handler, delay it for 100ms or so. Also that if you are running it and also your activity is just starting, it won't work before 7.0. Please refer to this article for more info about that. (https://medium.com/@chrisbanes/why-isnt-my-animation-running-34c529e76eaa#.akazwzeia) – Samvid Mistry Dec 26 '16 at 14:52
  • @SamvidMistry Thank you, I had similar issue and that fixed it. – Indrek Kõue Oct 10 '18 at 17:10

0 Answers0