Android使用animator实现fragment的3D翻转效果
作者:lrsmiracle 时间:2021-10-12 06:20:42
今天老师留的作业,使用俩个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翻转切换已经完成,希望我的经验可以帮助到你们。
来源:http://blog.csdn.net/lrsmiracle/article/details/51683077
标签:Android,3D翻转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jmeter添加自定函数的实例(jmeter5.3+IntelliJ IDEA)
2023-04-14 00:20:54
![](https://img.aspxhome.com/file/2023/2/129242_0s.png)
C#自定义控件实现TextBox禁止粘贴的方法
2023-08-03 06:00:22
Feign如何自定义注解翻译器
2022-11-09 07:05:04
Android开发之使用ExifInterface获取拍照后的图片属性
2021-05-29 23:36:36
Kafka 日志存储实现过程
2021-11-01 05:04:00
![](https://img.aspxhome.com/file/2023/7/62337_0s.png)
Java实现的RSA加密解密算法示例
2022-05-03 09:20:38
Java实现读取163邮箱,qq邮箱的邮件内容
2021-09-27 14:26:31
![](https://img.aspxhome.com/file/2023/9/63159_0s.png)
Java 集合框架之List 的使用(附小游戏练习)
2023-11-24 10:33:40
![](https://img.aspxhome.com/file/2023/7/59947_0s.png)
springboot整合security和vue的实践
2021-09-17 20:39:28
![](https://img.aspxhome.com/file/2023/5/97985_0s.jpg)
SpringBoot使用MyBatis-Plus解决Invalid bound statement异常
2022-12-23 12:13:13
Spring MVC接口防数据篡改和重复提交
2023-11-29 15:02:11
C#中遍历Hashtable的4种方法
2023-01-18 12:17:51
![](https://img.aspxhome.com/file/2023/8/125898_0s.png)
ViewPager实现带引导小圆点与自动跳转的引导界面
2021-07-11 12:25:18
![](https://img.aspxhome.com/file/2023/2/138732_0s.jpg)
C#中判断、验证字符串是否为日期格式的实现代码
2021-06-05 01:57:26
Android实现京东首页效果
2023-10-16 23:10:10
![](https://img.aspxhome.com/file/2023/0/120140_0s.jpg)
Android Studio用genymotion运行后小图标无法显示问题
2021-08-17 03:00:22
![](https://img.aspxhome.com/file/2023/7/89537_0s.png)
Android播放assets文件里视频文件相关问题分析
2021-08-10 20:04:41
Windows系统中Java调用cmd命令及执行exe程序的方法
2021-11-27 23:00:02
IDEA中项目集成git提交代码的详细步骤
2021-09-08 04:33:39
![](https://img.aspxhome.com/file/2023/0/97810_0s.jpg)
C# lambda表达式原理定义及实例详解
2021-07-26 21:44:44