代码之家  ›  专栏  ›  技术社区  ›  Martin Lund Andreas Engedal

带有动画的片段事务屏蔽了谷歌地图

  •  2
  • Martin Lund Andreas Engedal  · 技术社区  · 8 年前

    这是我启动交易的代码

        public void startTransactionAnimated(BaseFragment fragment, int startEnter, int startExit, int endEnter, int endExit) {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.setCustomAnimations(startEnter, startExit, endEnter, endExit);
        transaction.replace(R.id.content_frame, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    
    fragmentHandler.startTransactionAnimated(toiletPageFragment,
                R.anim.enter_from_right, R.anim.no_animation, R.anim.no_animation, R.anim.exit_to_right);
    

    Normal View

    黑色地图:

    Black Map

    我已经试过了 getSupportFragmentManager().executePendingTransactions();

    1 回复  |  直到 8 年前
        1
  •  2
  •   Andreas Engedal    8 年前

    好的,我真的做到了。这是相当多的工作,但它已经完成了!

    我有一个扩展片段的BaseFragment类。

    protected void onAnimationStarted () {}
    
    protected void onAnimationEnded () {}
    
    protected void onAnimationRepeated () {}
    
        @Override
    public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) {
        //Check if the superclass already created the animation
        Animation anim = super.onCreateAnimation(transit, enter, nextAnim);
    
        //If not, and an animation is defined, load it now
        if (anim == null && nextAnim != 0) {
            anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        }
        //If there is an animation for this fragment, add a listener.
        if (anim != null) {
            anim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart (Animation animation) {
                    onAnimationStarted();
                    Log.i("TESTTAG", "ANIM STARTED: " );
                }
                @Override
                public void onAnimationEnd (Animation animation) {
                    Log.i("TESTTAG", "ANIM END: " );
                    onAnimationEnded();
                }
                @Override
                public void onAnimationRepeat (Animation animation) {
                    onAnimationRepeated();
                }
            });
        }
        return anim;
    }
    

    在这里我添加了这个。这使得可以覆盖 onAnimationStart OnAnimation结束

        @Override
    protected void onAnimationStarted() {
        super.onAnimationStarted();
        isAnimationDone = false;
    }
    
    @Override
    protected void onAnimationEnded() {
        isAnimationDone = true;
        super.onAnimationEnded();
    }
    

    在这里,我设置了一个布尔值,以查看动画是否已实际结束。

    反压 在fragment中,我添加了以下代码,fragment是我创建的用于捕捉backPress事件的自定义方法。

        @Override
    public void doBack() {
        if (isAnimationDone){
            getActivity().getSupportFragmentManager().popBackStack();
        }
    }
    

    推荐文章