BackStack에서 단편 애니메이션을 반전하는 방법은 무엇입니까?


114

다음 코드를 사용하여 조각을 사용할 때 뒤로 버튼을 누르면 시스템이 백 스택에서 애니메이션을 반전시킬 것이라고 생각했습니다.

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);
ft.replace(R.id.viewContainer, new class(), "layout").addToBackStack(null).commit();

답변:


266

사용자 정의 애니메이션에 대한 Android 문서에 따르면 :

변화:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);

에:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out );

이제 백 스택이 애니메이션됩니다.


2
btw, 이것이 귀하의 질문 및 답변과 관련이 없다는 것을 알고 있지만 customAnimations를 약간 설명하는 것에 저를 연결해 주시겠습니까? : P
— AreusAstarte 2013-04-13

2
AreusAstarte : developer.android.com/reference/android/app/… 참조 , int, int, int)
— mDroidd

안녕하세요, 실제로 콘텐츠 전환을 사용하고 있습니다. 잘 작동하지만 뒤로 누르고 이전 조각으로 이동하면 배경이 사라져서 뷰를 애니메이션으로 표시 할뿐 아니라 예리한 뷰와 겹쳐서 표시됩니다.이를 피할 수있는 방법이 있습니까?
— user3497504

23

올바른 애니메이션을 사용하여 다음을 사용하고 매력처럼 작동합니다.

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="1000"
        android:valueTo="0"
        android:valueType="floatType" />
</set>

slide_in_right.xml

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="1000"
        android:valueType="floatType" />

</set>

slide_out_left.xml

   <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="-1000"
        android:valueType="floatType" />

</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="-1000"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

그런 다음 조각을 추가하는 동안 다음을 사용하십시오.

setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
                                R.anim.slide_out_right, R.anim.slide_in_right)

그리고 그것은 100 % 작동 할 것입니다


2
지원 프래그먼트 관리자를 사용 중이거나 프래그먼트가 프래그먼트의 지원 버전을 확장하는 경우에는 작동하지 않습니다
— w3bshark

w3bshark @ 어떻게 같은 애니메이션을 사용하여 작업을 진행합니다 FragmentManager및 Fragment지원 라이브러리에서?
— 다니엘 Shatz

2
@DanielShatz objectAnimators보다는 번역을 사용해야합니다. 예를 들어, slide_in_left.xml은 다음과 같습니다. <translate android:fromXDelta="100%" android:startOffset="25" android:toXDelta="0" />이 답변보기 : stackoverflow.com/a/5151774/1738090
— w3bshark

1
나는 이것을 시도하고 있습니다 (Marshmallow 장치에서-다른 버전을 시도하지 않았습니다). 작동하지 않습니다. 최종 FragmentTransaction fragmentTransaction = getFragmentManager (). beginTransaction (); fragmentTransaction.setCustomAnimations (R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); fragmentTransaction.replace (R.id.fl_right_container, detailFragment); fragmentTransaction.replace (R.id.fl_left_container, subcategoriesFragment, TestActivity.TAG_SUBCATEGORIES_FRAGMENT); fragmentTransaction.commit ();
— techtinkerer

13

나의 경우에는

fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, 
                       R.anim.slide_in_right, R.anim.slide_out_left);

완벽한 애니메이션을 만들 것입니다.

slide_in_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
               android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
           android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
               android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
           android:duration="@android:integer/config_mediumAnimTime" />
</set>

1
스스로 할 생각을했는데 너무 게으르다. 그리고 누군가가 이것을 StackOverflow에 게시해야한다고 말했고 여기 있습니다! haha
— F.Mysir

1
아무도 전에 이것을 게시하지 않았고 나는 시간이이 답변을 게시 할 차례라고 믿었습니다. 누가 나와 같은 입장이 될 수 있는지 ... lol @ F.Mysir
— Hoang Nguyen Huu

3
.setCustomAnimations(R.animator.fragment_fade_in,
        R.animator.fragment_fade_out,
        R.animator.fragment_fade_p_in,
        R.animator.fragment_fade_p_out)

위의 내용을 다음으로 바꿉니다.

mFragmentManager.beginTransaction()
    .setCustomAnimations(R.animator.fragment_fade_in,
            R.animator.fragment_fade_out,
            R.animator.fragment_fade_p_in,
            R.animator.fragment_fade_p_out)
    .replace(R.id.main_container, FragmentPlayerInfo.getInstance(data))
    .addToBackStack(FragmentPlayerInfo.TAG)
    .commit();

1
귀하의 추천이 어떻게 도움이되는지에 대한 설명을 추가하는 것이 좋습니다.
— Wtower

2
나는 왜이 일을 (알고하지 않습니다,하지만 이후 애니메이션을 추가 할 때 replace와 addToBackstack, 작동하지 않습니다
— TarikW

2
@TarikW 조금 늦었지만 순서가 중요합니다. 교체하기 전에 setCostomAnimations를 호출해야합니다. addToBackStack 메서드
— MD Husnain Tahir

1

이것은 Fragment Transaction 클래스에서 언급 한 바와 같습니다.

/**
     * Set specific animation resources to run for the fragments that are
     * entering and exiting in this transaction. The <code>popEnter</code>
     * and <code>popExit</code> animations will be played for enter/exit
     * operations specifically when popping the back stack.
     *
     * @param enter An animation or animator resource ID used for the enter animation on the
     *              view of the fragment being added or attached.
     * @param exit An animation or animator resource ID used for the exit animation on the
     *             view of the fragment being removed or detached.
     * @param popEnter An animation or animator resource ID used for the enter animation on the
     *                 view of the fragment being readded or reattached caused by
     *                 {@link FragmentManager#popBackStack()} or similar methods.
     * @param popExit An animation or animator resource ID used for the enter animation on the
     *                view of the fragment being removed or detached caused by
     *                {@link FragmentManager#popBackStack()} or similar methods.
     */
    @NonNull
    public abstract FragmentTransaction setCustomAnimations(@AnimatorRes @AnimRes int enter,
            @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter,
            @AnimatorRes @AnimRes int popExit);

그래서 마지막으로 다음과 같은 방법을 사용할 수 있습니다.

 mFragmentManager.beginTransaction()
                        .replace(R.id.container, fragment)
                        .setCustomAnimations(R.anim.slide_left,//enter
                                             R.anim.slide_out_left,//exit
                                             R.anim.slide_right,//popEnter
                                             R.anim.slide_out_right)//popExit
                        .addToBackStack(fragment.toString())
                        .commit();

0

이 작품은 나를 위해 !! 조각에 대한이 코드! 이 코드를 활동에 사용하려면 처음부터 삭제하십시오 getActivity()!!

getActivity().getSupportFragmentManager()
        .beginTransaction()
        .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.fade_out,android.R.anim.slide_in_left, android.R.anim.fade_out)
        .replace(R.id.fragment_container, new YourFragment)
        .addToBackStack(null)
        .commit();

행운을 빕니다 !!

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.