11

我创建了一个自定义循环显示转换,用作 Activity 的进入转换的一部分(具体来说,我通过调用将转换设置为窗口的进入转换Window#setEnterTransition()):

public class CircularRevealTransition extends Visibility {
    private final Rect mStartBounds = new Rect();

    /**
     * Use the view's location as the circular reveal's starting position.
     */
    public CircularRevealTransition(View v) {
        int[] loc = new int[2];
        v.getLocationInWindow(loc);
        mStartBounds.set(loc[0], loc[1], loc[0] + v.getWidth(), loc[1] + v.getHeight());
    }

    @Override
    public Animator onAppear(ViewGroup sceneRoot, final View v, TransitionValues startValues, TransitionValues endValues) {
        if (endValues == null) {
            return null;
        }
        int halfWidth = v.getWidth() / 2;
        int halfHeight = v.getHeight() / 2;
        float startX = mStartBounds.left + mStartBounds.width() / 2 - halfWidth;
        float startY = mStartBounds.top + mStartBounds.height() / 2 - halfHeight;
        float endX = v.getTranslationX();
        float endY = v.getTranslationY();
        v.setTranslationX(startX);
        v.setTranslationY(startY);

        // Create a circular reveal animator to play behind a shared
        // element during the Activity Transition.
        Animator revealAnimator = ViewAnimationUtils.createCircularReveal(v, halfWidth, halfHeight, 0f,
                FloatMath.sqrt(halfWidth * halfHeight + halfHeight * halfHeight));
        revealAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Set the view's visibility to VISIBLE to prevent the
                // reveal from "blinking" at the end of the animation.
                v.setVisibility(View.VISIBLE);
            }
        });

        // Translate the circular reveal into place as it animates.
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("translationX", startX, endX);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("translationY", startY, endY);
        Animator translationAnimator = ObjectAnimator.ofPropertyValuesHolder(v, pvhX, pvhY);

        AnimatorSet anim = new AnimatorSet();
        anim.setInterpolator(getInterpolator());
        anim.playTogether(revealAnimator, translationAnimator);
        return anim;
    }
}

这可以正常工作。但是,当我在转换中间单击“后退按钮”时,出现以下异常:

Process: com.adp.activity.transitions, PID: 13800
java.lang.UnsupportedOperationException
        at android.view.RenderNodeAnimator.pause(RenderNodeAnimator.java:251)
        at android.animation.AnimatorSet.pause(AnimatorSet.java:472)
        at android.transition.Transition.pause(Transition.java:1671)
        at android.transition.TransitionSet.pause(TransitionSet.java:483)
        at android.app.ActivityTransitionState.startExitBackTransition(ActivityTransitionState.java:269)
        at android.app.Activity.finishAfterTransition(Activity.java:4672)
        at com.adp.activity.transitions.DetailsActivity.finishAfterTransition(DetailsActivity.java:167)
        at android.app.Activity.onBackPressed(Activity.java:2480)

我收到此错误是否有任何具体原因?应该如何避免?

4

3 回答 3

10

您将需要创建一个忽略对and的Animator调用的子类,以避免此异常。pause()resume()

有关更多详细信息,我刚刚完成了以下有关此主题的帖子:

于 2014-11-07T01:35:02.127 回答
3

我收到此错误是否有任何具体原因?

ViewAnimationUtils.createCircularReveal是创建新的快捷方式RevealAnimator,它是 的子类RenderNodeAnimator。默认情况下,RenderNodeAnimator.pause抛出一个UnsupportedOperationException. 您会在堆栈跟踪中看到这种情况:

java.lang.UnsupportedOperationException
        at android.view.RenderNodeAnimator.pause(RenderNodeAnimator.java:251)

Activity.onBackPressed在 Lollipop 中被调用时,它会重新调用Activity.finishAfterTransition,最终调用回Animator.pausein Transition.pause(android.view.View),也就是你UnsupportedOperationException最终被抛出的时候。

转换完成后使用“后退”按钮时没有抛出它的原因是由于完成后EnterTransitionCoordinator处理输入Transition的方式。

应该如何避免?

我想你有几个选择,但都不是很理想:

选项1

TransitionListener在您调用时附加一个,Window.setEnterTransition以便您可以监控何时调用“返回”按钮。所以,像:

public class YourActivity extends Activity {

    /** True if the current window transition is animating, false otherwise */
    private boolean mIsAnimating = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Get the Window and enable Activity transitions
        final Window window = getWindow();
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        // Call through to super
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);

        // Set the window transition and attach our listener
        final Transition circularReveal = new CircularRevealTransition(yourView);
        window.setEnterTransition(circularReveal.addListener(new TransitionListenerAdapter() {

            @Override
            public void onTransitionEnd(Transition transition) {
                super.onTransitionEnd(transition);
                mIsAnimating = false;
            }

        }));

        // Restore the transition state if available
        if (savedInstanceState != null) {
            mIsAnimating = savedInstanceState.getBoolean("key");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the current transition state
        outState.putBoolean("key", mIsAnimating);
    }

    @Override
    public void onBackPressed() {
        if (!mIsAnimating) {
            super.onBackPressed();
        }
    }

}

选项 2

使用反射调用ActivityTransitionState.clear,将停止Transition.pause(android.view.View)调用ActivityTransitionState.startExitBackTransition

@Override
public void onBackPressed() {
    if (!mIsAnimating) {
        super.onBackPressed();
    } else {
        clearTransitionState();
        super.onBackPressed();
    }
}

private void clearTransitionState() {
    try {
        // Get the ActivityTransitionState Field
        final Field atsf = Activity.class.getDeclaredField("mActivityTransitionState");
        atsf.setAccessible(true);
        // Get the ActivityTransitionState
        final Object ats = atsf.get(this);
        // Invoke the ActivityTransitionState.clear Method
        final Method clear = ats.getClass().getDeclaredMethod("clear", (Class[]) null);
        clear.invoke(ats);
    } catch (final Exception ignored) {
        // Nothing to do
    }
}

显然每个都有缺点。选项 1 基本上禁用“后退”按钮,直到转换完成。选项 2 允许您使用“后退”按钮进行中断,但会清除转换状态并使用反射。

这是结果的gfy。您可以看到它是如何从“A”完全过渡到“M”并再次返回的,然后“返回”按钮会中断过渡并返回到“A”。如果你看它会更有意义。

无论如何,我希望对你有所帮助。

于 2014-11-05T12:37:05.777 回答
0

transitionInProgress您可以添加侦听器以输入在方法onTransitionStart()/中设置标志的转换onTransitionEnd()。然后,您可以覆盖方法finishAfterTransition(),然后检查transitionInProgress标志,super只有在转换完成时才调用。否则你可以只是finish()你的Activity或什么都不做。

override fun finishAfterTransition() {
    if (!transitionInProgress){
        super.finishAfterTransition()
    } else {
        finish()
    }
}
于 2017-10-10T09:21:17.170 回答