OverridePendingTransition无效

时间:2016-10-21 06:42:15

标签: android animation material-design

我已经看到了这个问题的其他主题,但没有一个对我有用。

我有一个活动A,它调用活动B.我以编程方式在进入和退出时进行淡入淡出动画,并将共享FAB转换为其新位置。这种翻译有效,但淡出没有。

活动代码A

profilePic.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // Activity A starting Activity B
        Activity activity = MainScreenUI.this;

        Intent intent = new Intent(MainScreenUI.this, ProfileUI.class);

        int[] imageScreenLocation = new int[2];
        profilePic.getLocationInWindow(imageScreenLocation);

        intent.putExtra(Properties.PACKAGE + ".left", imageScreenLocation[0])
              .putExtra(Properties.PACKAGE + ".top", imageScreenLocation[1])
              .putExtra(Properties.PACKAGE + ".width", profilePic.getWidth())
              .putExtra(Properties.PACKAGE + ".height", profilePic.getHeight());

        startActivity(intent);

        activity.overridePendingTransition(0, 0);
    }
});

活动代码B

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    ...

    mTopLevelLayout = (CoordinatorLayout)findViewById(R.id.profile_coordinator);

    mBackground = new ColorDrawable(Color.WHITE);
    mTopLevelLayout.setBackground(mBackground);

    if (savedInstanceState == null)
    {
        final ViewTreeObserver observer = mProfileFAB.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
        {
            @Override
            public boolean onPreDraw()
            {
                mProfileFAB.getViewTreeObserver().removeOnPreDrawListener(this);

                ...

                _runEnterAnimation();

                return true;
            }
        });
    }
}

private void _runEnterAnimation()
{
    mProfileFAB.setPivotX(0);
    mProfileFAB.setPivotY(0);
    mProfileFAB.setScaleX(mWidthScaleImage);
    mProfileFAB.setScaleY(mHeightScaleImage);
    mProfileFAB.setTranslationX(mLeftDeltaImage);
    mProfileFAB.setTranslationY(mTopDeltaImage);

    mProfileFAB.animate()
               .setDuration(ANIM_DURATION)
               .scaleX(1).scaleY(1)
               .translationX(0).translationY(0)
               .setInterpolator(ACCELERATE_DECELERATE_INTERPOLATOR);

    // This animation is not applied
    ObjectAnimator bgAnim = ObjectAnimator.ofInt(mBackground, "alpha", 0, 255);
    bgAnim.setDuration(ANIM_DURATION);
    bgAnim.start();
}




@Override
public void onBackPressed()
{
    _runExitAnimation(new Runnable() {
        public void run() {
            finish();
        }
    });    
}

@Override
public void finish()
{
    super.finish();

    overridePendingTransition(0, 0);
}

private void _runExitAnimation(final Runnable endAction)
{
    int[] currentLocation = new int[2];
    mProfileFAB.getLocationOnScreen(currentLocation);
    mTopDeltaImage = mThumbnailTop - currentLocation[1];

    mProfileFAB.animate()
               .setDuration(ANIM_DURATION)
               .setStartDelay(0)
               .scaleX(mWidthScaleImage).scaleY(mHeightScaleImage)
               .translationX(mLeftDeltaImage).translationY(mTopDeltaImage)
               .withEndAction(endAction);

    // This animation is not working either
    ObjectAnimator bgAnim = ObjectAnimator.ofInt(mBackground, "alpha", 0);
    bgAnim.setDuration(ANIM_DURATION);
    bgAnim.start();
}

活动B的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/profile_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.ProfileUI"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/profile_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:background="@drawable/filter_bottom_border">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/profile_collapsing_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            ...

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        ...

    </android.support.v4.widget.NestedScrollView>

    <com.wallakoala.wallakoala.Views.FloatingActionImageView
        android:id="@+id/profile_floating_pic"
        android:layout_width="84dp"
        android:layout_height="84dp"
        android:src="@drawable/female_icon"
        app:borderWidth="0dp"
        app:layout_anchor="@id/profile_appbar_layout"
        app:layout_anchorGravity="bottom|center"/>

</android.support.design.widget.CoordinatorLayout>

奇怪的是,这种逻辑适用于其他地方并且工作正常,我不知道我在这里搞砸了什么。

提前致谢,

2 个答案:

答案 0 :(得分:0)

您应该在尝试打开overridePendingTransition()的{​​{1}}方法中使用onCreate(),而不是在提出相同意图后使用Activity

如果保留参数0只是为了隐藏你的问题中的代码就好了,但如果没有,让我告诉你它希望动画资源文件作为输入参数。您可以使用支持库中提供的淡入淡出动画:

overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);

它将第一个参数作为正在打开的Activity的输入动画,第二个参数作为前一个参数的退出动画。

答案 1 :(得分:0)

在活动A&gt;中的overridePendingTransition()方法内提供退出并输入动画。 onclick方法。在那里你提供了overridePendingTransition(0,0)。