在活动事务之后不调用onEnterAnimationComplete()

时间:2015-10-31 00:22:47

标签: android android-activity android-transitions

我有两个活动,只有一个共享对象ImageView。这两个活动都是AppCompatActivity的子类,它们共享相同的主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowExitTransition">@transition/transition_slide</item>
    <item name="android:windowEnterTransition">@transition/transition_slide</item>
    <!-- specify shared element transitions -->
    <item name="android:windowSharedElementEnterTransition">
        @transition/obj_transition</item>
    <item name="android:windowSharedElementExitTransition">
        @transition/obj_transition</item>
</style>

活动A按以下方式调用活动B:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
ActivityOptionsCompat options = ActivityOptionsCompat
    .makeSceneTransitionAnimation(ActivityA.this, view, "common_tag");
ActivityCompat
    .startActivityForResult(ActivityA.this, intent, ACT_B_TAG, options.toBundle());

在Activity B上,我想在同一个共享的ImageView上启动一个简单的摆动动画。如果我在onCreate方法上启动动画,结果非常难看,因为它在活动转换结束之前启动。所以我覆盖了活动B中的onEnterAnimationComplete()方法:

@Override
public void onEnterAnimationComplete() {
    Log.d(TAG, "ANIMATION COMPLETE");
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.wobble);
    findViewById(R.id.SharedView).startAnimation(anim);
}

问题是,没有任何反应。活动和共享元素转换播放,但当它们结束时,其他动画将无法启动。检查logcat输出我发现甚至没有调用该方法。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

有一个解决方法。你可以推迟活动输入转换,为你的图像添加一个preDraw监听器(或者其他什么,取决于你),设置你的动画,然后开始推迟的输入过渡。

postponeEnterTransition();  //  wait until all animations are set up
imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);  //  only run once
                enterAnimation();  //  your animations here
                startPostponedEnterTransition();  //  all animations are ran

                return true;
            }
        });

我不知道onEnterAnimationComplete()是否被调用是预期的行为,但如果是,那么这是框架团队的一个很大的疏忽。

答案 1 :(得分:0)

我已经测试了代码并且它运行良好。

向MainActivity添加一个按钮以导航到ActivityB。

public class MainActivity extends AppCompatActivity {

private Button button;

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

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), ActivityB.class);
            startActivity(intent);
        }
    });
}

覆盖ActivityB的动画以正确理解

public class ActivityB extends AppCompatActivity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.slide_in_up, R.anim.still);
    setContentView(R.layout.activity_b);

    textView = (TextView) findViewById(R.id.textView);
}

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.still, R.anim.slide_out_down);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.still, R.anim.slide_out_down);
}

@Override
public void onEnterAnimationComplete() {
    super.onEnterAnimationComplete();
    Toast.makeText(this, "Animation Completed", Toast.LENGTH_SHORT).show();
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_clockwise);
    textView.startAnimation(anim);
}

slide_in_up.xml

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100%"
    android:toYDelta="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="3000" />

slide_out_down.xml

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="100%"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="3000" />

still.xml

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="3000" />

rotate_clockwise.xml

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="3000"/>

enter image description here