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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

lrsmiracle   2021-03-28 我要评论
想了解Android使用animator实现fragment的3D翻转效果的相关内容吗,lrsmiracle在本文为您仔细讲解Android实现fragment的3D翻转效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,animator,fragment,3D翻转,下面大家一起来学习吧。

今天老师留的作业,使用俩个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翻转切换已经完成,希望我的经验可以帮助到你们。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们