替换片段的揭示过渡效果

3

我有一个默认加载Fragment StatsDetailFragment的活动。

在按下按钮时,我使用以下代码替换Fragment:

getSupportFragmentManager().beginTransaction()
                .replace(R.id.stats_detail_container, projectEntryListFragment, ProjectEntryListFragment.FRAGMENT_ID)
                .addToBackStack(FRAGMENT_CHART)
                .commit();

我正在使用自定义过渡类为ProjectEntryListFragment设置进入过渡效果。
public class RevealTransition extends Visibility {
    private final Point mEpicenter;
    private final int mSmallRadius;
    private final int mBigRadius;
    private final long mDuration;

    public RevealTransition(Point epicenter, int smallRadius, int bigRadius, long duration) {
        mEpicenter = epicenter;
        mSmallRadius = smallRadius;
        mBigRadius = bigRadius;
        mDuration = duration;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) {
        Animator animator = ViewAnimationUtils.createCircularReveal(view, mEpicenter.x, mEpicenter.y,
                mSmallRadius, mBigRadius);
        animator.setDuration(mDuration);
        return new WrapperAnimator(animator);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) {
        Animator animator = ViewAnimationUtils.createCircularReveal(view, mEpicenter.x, mEpicenter.y,
                mBigRadius, mSmallRadius);
        animator.setDuration(mDuration);
        return new WrapperAnimator(animator);
    }
}

过渡效果很好。但我面临的问题是,当ProjectEntryListFragment执行揭示动画时,StatsDetailFragment已经被移除了,这导致揭示动画在一个空白背景上播放。我希望它能在StatsDetailFragment上播放,这样在ProjectListFragment上播放揭示过渡时,StatsDetailFragment可见。

我尝试添加新片段,但这只会在弹出回退栈时给我一个错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v4.app.Fragment.getAllowReturnTransitionOverlap()' on a null object reference

任何帮助都将不胜感激。
1个回答

0

为您的片段设置一个动画监听器。 在动画运行之前,将您的根视图设置为您想要涟漪的颜色。在onAnimationEnd和onAnimationCanceled中,将您的根视图设置为您想要布局的颜色。

此外,可见性“技巧”运行不太稳定(我个人认为) 尝试使用ViewTreeObserver。

希望这可以帮助您:)


我没有使用动画,而是使用了Transition。但是,我尝试设置TransitionListeners,但似乎不起作用。ViewTreeObserver也不起作用.. :( - Manu Joseph
我曾经遇到过同样的问题。这就是为什么在转换完成后,我对片段进行了一些视觉上的小技巧。它具有相同的视觉效果。在onCreateView中像这样运行此代码:view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { reveal();   ViewUtils.removeGlobalListeners(first, this); } }); - locomain
那么你是将揭示效果实现为动画了?代码中的reveal()方法就是这样做的吧? - Manu Joseph
我已经让它在某种程度上工作了... 我将一个转换监听器附加到揭示转换上,并在onTransitionStart()中使用fragment.getView()获取根视图并将背景设置为颜色。现在的问题是,我能够将任何颜色设置为根视图,但不能设置透明颜色。 - Manu Joseph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接