怎么在Android中利用animator实现一个3D翻转效果

本文章向大家介绍怎么在Android中利用animator实现一个3D翻转效果的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Android是什么

Android是一种基于Linux内核的*及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

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

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>

以上就是小编为大家带来的怎么在Android中利用animator实现一个3D翻转效果的全部内容了,希望大家多多支持亿速云!