OverridePendingTransition无法正常工作

问题描述:

我已经看到有关此问题的其他线索,但是他们都没有为我工作。OverridePendingTransition无法正常工作

我有一个活动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> 

奇怪的是,这种逻辑是在其他地方施加和它工作正常,我不知道我在这里搞砸了什么。

由于提前,

你还是在ActivityonCreate()方法,您正试图比养了相同的意向后,打开使用overridePendingTransition()

如果你保留参数0只是为了隐藏你的代码在问题中很好,但如果没有,让我告诉你,它期望动画资源文件作为输入参数。你也许可以利用现有的渐变动画的支持库为:

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

它需要在第一个参数为Activity的进入动画被打开,为前一个出口动画的第二个参数。

+0

但我没有使用XML动画,我有意地这样做,因此overridePendingTransition(0,0) – cuoka

+0

我忘了,我尝试在onCreate,OnPause中使用overridePendingTransition(),并且没有任何结果。 – cuoka

+0

如果您没有使用资源中的动画,它将无法工作,并且如果您不想使用来自资源的动画,“overridePendingTransition”将无法满足您的目的。 –

提供退出并在您的活动A> onclick方法中的overridePendingTransition()方法内输入动画。你提供了overridePendingTransition(0,0)。

+0

我没有使用XML动画,我有意地这样做,因此overridePendingTransition(0,0) – cuoka

+0

请参阅此链接的ActivitySwitcher选项。这可能会帮助你。 http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-animation-activityswitcher/comment-page-1/#comment-12025 –

+0

您是否看到此链接http://stackoverflow.com/问题/ 22325975 /如何到创建-的活动 - 过渡编程。没有检查。 –