Android 使用animator实现fragment的3D翻转效果

来源:互联网 发布:万网域名交易 编辑:程序博客网 时间:2024/05/21 17:27

今天老师留的作业,使用俩个Fragment来实现3D翻转效果,遇到了一点点的问题,于是在网上进行了查找,但是发现有些博主的代码不正确,对其他人进行了误导,在网上使用属性动画实现3D效果非常少,所以经过我自己的实验摸索,我将自己的代码和遇到的问题给他讲解一下提供一点点借鉴,并且希望可以帮助到大家。

首先讲解一下主要实现动画的函数:

getFragmentManager().beginTransaction()        .setCustomAnimations(R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit)        .replace(R.id.container, new MainFragment()).commit();
我想信这个函数大家在实现动画时都会使用到,先获得FragmentManager然后进行transaction,主要添加动画的函数是setCustomAnimations(),在网上可以查到的解释,对这个方法有些错误,描述的是当前的Fragment对象的进入和退出时的动画效果,是这个对象的一种属性,但是这个方法真正的解释应该是在当前Activity在切换Fragment时所执行的动画方式,也就是说当前Fragment退出时用的是方法中的退出动画,新的Fragment进入时执行的是进入的动画效果,可以理解为这一次动画效果完全是利用这一个语句来完成,有些博客的记载对我们产生了一些误导。

官方的注释如下:

/** * Set specific animation resources to run for the fragments that are * entering and exiting in this transaction. These animations will not be * played when popping the back stack. */public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
整体的3d翻转效果代码如下:

第二个Fragment,

/** * Created by Liurs on 2016/6/14. **/public class SecondFragment extends Fragment {    private LinearLayout root;    private Button mButton;    public SecondFragment() {    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        root = (LinearLayout) inflater.inflate(R.layout.fragment_second, container, false);        //Set listener;        setListener();        return root;    }    /**     * set listener     */    private void setListener() {        mButton = (Button) root.findViewById(R.id.button);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getFragmentManager().beginTransaction()                        .setCustomAnimations(R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit)                        .replace(R.id.container, new MainFragment()).commit();            }        });    }    @Override    public void onViewCreated(View view, Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);    }}
第一个Fragment,

/** * Created by Liurs on 2016/6/14. **/public class MainFragment extends Fragment {    private LinearLayout root;    private Button mButton;    public MainFragment() {    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        root = (LinearLayout) inflater.inflate(R.layout.content_main, container, false);        //Set listener;        setListener();        return root;    }    /**     * set listener     */    private void setListener() {        mButton = (Button) root.findViewById(R.id.button);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getFragmentManager()                        .beginTransaction()                        .addToBackStack(null)                        .setCustomAnimations(R.animator.fragment_3d_reversal_enter,R.animator.fragment_3d_reversal_exit,R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit)                        .replace(R.id.container, new SecondFragment())                        .commit();            }        });    }    @Override    public void onViewCreated(View view, Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);    }}
逆时针翻转动画进入时配置文件,

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially">    <objectAnimator        android:duration="0"        android:propertyName="rotationY"        android:valueFrom="0f"        android:valueTo="270f" />    <objectAnimator        android:duration="500"        android:propertyName="rotationY"        android:startOffset="500"        android:valueFrom="270f"        android:valueTo="360f" /></set>
退出时动画配置文件,

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially">    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="500"        android:propertyName="rotationY"        android:valueFrom="0f"        android:valueTo="90f">        </objectAnimator></set>
顺时针翻转动画进入时配置文件,

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:ordering="sequentially">    <objectAnimator        android:duration="0"        android:propertyName="rotationY"        android:valueFrom="180f"        android:valueTo="90f" />    <objectAnimator        android:duration="500"        android:propertyName="rotationY"        android:startOffset="500"        android:valueFrom="90f"        android:valueTo="0f" /></set>
退出时动画配置文件,

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially">    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="500"        android:propertyName="rotationY"        android:valueFrom="0f"        android:valueTo="-90f">    </objectAnimator></set>
至此,两个Fragment的3D翻转切换已经完成,希望我的经验可以帮助到你们





0 0
原创粉丝点击