一手遮天 Android - Fragment: Fragment 动画

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - Fragment: Fragment 动画

示例如下:

/fragment/FragmentDemo4.java

/**
 * Fragment 动画
 *
 * 通过 setCustomAnimations() 来指定 fragment 的切换动画
 *     第 1 个参数:新增的 fragment 的动画
 *     第 2 个参数:移除的 fragment 的动画
 *     第 3 个参数:popBackStack 时,显示的 fragment 的动画
 *     第 4 个参数:popBackStack 时,移除的 fragment 的动画
 */

package com.webabcd.androiddemo.fragment;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.webabcd.androiddemo.R;

public class FragmentDemo4 extends AppCompatActivity {

    private Button mButton1;
    private Button mButton2;
    private Button mButton3;

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

        mButton1 = findViewById(R.id.button1);
        mButton2 = findViewById(R.id.button2);
        mButton3 = findViewById(R.id.button3);

        sample();
    }

    private void sample() {
        // add a fragment
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                Fragment fragment = fragmentManager.findFragmentByTag("myTag");
                if (fragment == null) {
                    fragmentManager
                            .beginTransaction()
                            .setCustomAnimations(R.anim.activity_push_left_in, R.anim.activity_push_left_out, R.anim.activity_zoom_enter, R.anim.activity_zoom_exit)
                            .add(R.id.container, new Fragment4_1(), "myTag")
                            .addToBackStack(null)
                            .commit();
                }
            }
        });

        // replace the fragment
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                Fragment fragment = fragmentManager.findFragmentByTag("myTag");
                if (fragment != null) {
                    fragmentManager
                            .beginTransaction()
                            .setCustomAnimations(R.anim.fragment_push_left_in, R.anim.fragment_push_left_out, R.anim.fragment_zoom_enter, R.anim.fragment_zoom_exit)
                            .replace(R.id.container, new Fragment4_1(), "myTag")
                            .addToBackStack(null)
                            .commit();
                }
            }
        });

        // popBackStack()
        mButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().popBackStack();
            }
        });
    }
}

/layout/activity_fragment_fragmentdemo4.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="add a fragment"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="replace the fragment"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="popBackStack()"/>

</LinearLayout>

/anim/activity_push_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0">
    </translate>
</set>

/anim/activity_push_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p">
    </translate>
</set>

/anim/activity_zoom_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1.0">
    </alpha>
    <scale
        android:duration="500"
        android:fromXScale="2.0"
        android:fromYScale="2.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="1.0"
        android:toYScale="1.0">
    </scale>
</set>

/anim/activity_zoom_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0">
    </alpha>
    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="0"
        android:toYScale="0">
    </scale>
</set>

/anim/fragment_push_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0">
    </translate>
</set>

/anim/fragment_push_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p">
    </translate>
</set>

/anim/fragment_zoom_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1.0">
    </alpha>
    <scale
        android:duration="500"
        android:fromXScale="2.0"
        android:fromYScale="2.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="1.0"
        android:toYScale="1.0">
    </scale>
</set>

/anim/fragment_zoom_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0">
    </alpha>
    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="0"
        android:toYScale="0">
    </scale>
</set>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-06-02 09:29  webabcd  阅读(99)  评论(0编辑  收藏  举报