Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画
Activity转场动画
Activity的转场动画是通过overridePendingTransition(int enterAnim, int exitAnim)实现的。
这个方法是API Level 5 加入的。
这个方法在startActivity(Intent) or finish()之后被调用,指定接下来的这个转场动画。
方法的第一个参数:enterAnim,是新的Activity的进入动画的resource ID;
第二个参数exitAnim,是旧的Activity(当前的Activity)离开动画的resource ID。
所以这两个参数的对象是两个Activity。
如果上面两个参数没有动画要设置,则用0作为参数。
动画的资源文件放在res\anim\目录下,是View Animation。
参见本博客博文:Android Animation学习(六) View Animation介绍
View Animation包含了基本的动画类型,基本可以满足一般转场动画的需要。
根节点可以是:<alpha>, <scale>, <translate>, <rotate>, interpolator element, 或者是<set>。
利用set可以进行各种嵌套组合。
代码例子:
Intent intent = new Intent(TestActivities.this,TestActivityFirst.class);
startActivity(intent);
// transaction animation
overridePendingTransition(R.anim.slide_in_bottom,R.anim.slide_out_bottom);
其中两个动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
android:duration="2000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="2000" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="100%p"
android:duration="2000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="2000" />
</set>
Fragment转场动画
Fragment的转场动画实现分为使用v4包和不使用v4包两种情况,不使用v4包的话,最低API Level需要是11。
标准转场动画:
可以给Fragment指定标准的转场动画,通过setTransition(int transit)方法。
该方法可传入的三个参数是:
TRANSIT_NONE,
TRANSIT_FRAGMENT_OPEN,
TRANSIT_FRAGMENT_CLOSE
分别对应无动画、打开形式的动画和关闭形式的动画。
标准动画设置好后,在Fragment添加和移除的时候都会有。
自定义转场动画
自定义转场动画是通过setCustomAnimations()方法,因为Fragment添加时可以指定加入到Back Stack中,所以转场动画有添加、移除、从Back stack中pop出来,还有进入四种情况。
注意setCustomAnimations()方法必须在add、remove、replace调用之前被设置,否则不起作用。
android.app.Fragment
类参考:
Fragment:http://developer.android.com/reference/android/app/Fragment.html
FragmentTransaction:http://developer.android.com/reference/android/app/FragmentTransaction.html
不使用v4包的情况下(min API >=11)所对应的动画类型是Property Animation。
即动画资源文件需要放在res\animator\目录下,且根标签是<set>, <objectAnimator>, or <valueAnimator>三者之一。
这一点也可以从Fragment中的这个方法看出:onCreateAnimator(int transit, boolean enter, int nextAnim),返回值是Animator。
自定义转场动画时,四个参数的形式setCustomAnimations (int enter, int exit, int popEnter, int popExit)是API Level 13才有的,11只引入了两个动画的形式,即无法指定Back Stack栈操作时的转场动画。
代码例子:
private void addFragment() {
if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
// 标准动画
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// fragmentTransaction
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
// 自定义动画
// API LEVEL 11
fragmentTransaction.setCustomAnimations(
R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_right_exit);
// API LEVEL 13
// fragmentTransaction.setCustomAnimations(
// R.animator.fragment_slide_left_enter,
// R.animator.fragment_slide_left_exit,
// R.animator.fragment_slide_right_enter,
// R.animator.fragment_slide_right_exit);
fragmentTransaction.add(R.id.container, mTextFragmentOne);
// 加入到BackStack中
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
其中四个动画是从ApiDemos中拿来的:
fragment_slide_left_enter:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="100dp" android:valueTo="0dp"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="0.0" android:valueTo="1.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
fragment_slide_left_exit:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="0dp" android:valueTo="-100dp"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="1.0" android:valueTo="0.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
fragment_slide_right_enter:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="-100dp" android:valueTo="0dp"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="0.0" android:valueTo="1.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
fragment_slide_right_exit:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="0dp" android:valueTo="100dp"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<objectAnimator
android:interpolator="@android:interpolator/decelerate_quint"
android:valueFrom="1.0" android:valueTo="0.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
android.support.v4.app.Fragment
Fragment:http://developer.android.com/reference/android/support/v4/app/Fragment.html
FragmentTransaction:http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html
使用v4包,Fragment的使用不再局限于API Level 11之上,低等级的API也可以使用,但是这时候转场动画的类型是View Animation。
动画资源放在res\anim\路径下,和Activity的转场动画一样。
Fragment中的方法:onCreateAnimation(int transit, boolean enter, int nextAnim)返回值Animation。
FragmentTransaction中的setCustomAnimations()方法,两参数类型和四参数类型都可用。
所以一般还是用v4包的这个版本,一是兼容性比较好,另外View Animation其实基本可以满足转场动画的需要。
代码例子:
private void addFragment() {
if (null == mFragmentManager) {
mFragmentManager = getSupportFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(
R.anim.push_left_in,
R.anim.push_left_out,
R.anim.push_left_in,
R.anim.push_left_out);
fragmentTransaction.add(R.id.container, mTextFragmentOne);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Demo:
完整的例子项目:https://github.com/mengdd/HelloTransactionAnimations
Android Activity和Fragment的转场动画的更多相关文章
- [转]Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...
- Android Activity和Fragment传递数据
1.Activity与Activity传递数据 UserLoginActivity.java: Intent welcomePage = new Intent(); Bundle dataBundle ...
- Android --Activity与Fragment通讯
参考博客:详解Fragment跟Activity之间的通信 Activity中方法 private OnSearchListener mSearchListener; /** *定义一个借口 **/ ...
- android activity和fragment的生命周期图
Activity的生命周期: Fragment的生命周期:
- 018 Android Activity界面移入与移出的动画效果
1.平移动画 上一页移入动画 (-屏幕宽度,y)------>(0,y) 上一页移出动画 (0,y)-------------->(屏幕宽度,y) 下一页移入动画 (屏幕宽度,y)---- ...
- android activity and fragment活动周期
1.状态 /* 每个活动一共有四种状态 *:1.运行状态,就是栈顶的那个 * 2.暂停状态:就是不处于栈顶,但是依然可见,比如对话框下面的界面 * 3.停止状态:不处于栈顶,并且不可见 * 4.销毁状 ...
- Android Activity和Fragment生命周期图
- Android Activity与Fragment生命周期
- Android Activity与Fragment生命周期 对应关系
随机推荐
- Eclipse快捷键(转)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- 简单几步把LOGO变字体
今天学到一招,所以决定简单写写如何利用图标字体生成器IcoMoon把自己制作的图标变成字体,下面以OSChina的图标为例. 一.确保logo转成纯色,并保存为svg格式 因为OSC的logo是绿色带 ...
- 浏览器桌面通知Notification探究
首先说明,这篇博文不是科普讲解的,而是立flag研究的,是关于浏览器消息自动推送,就是下面这个玩意: 最近常常在浏览器看到这样的消息推送,还有QQ.com的推送,现在我对这个不了解,不知道叫消息自动推 ...
- jQuery1.9及其以上版本中动态元素on绑定事件无效解决方案
jQuery 1.9/2.0/2.1及其以上版本无法使用live函数了,然而jQuery 1.9及其以上版本提供了on函数来代替.本文讲解了jQuery on函数的使用方法,以及在使用jQuery函数 ...
- Python语言特性之4:类变量和实例变量
类变量就是供类使用的变量,实例变量就是供实例使用的.如下面的代码: class Person: name = "Tacey" p1 = Person() p2 = Person() ...
- Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程
初学 Spring MVC , 感觉对于 @ModelAttribute 和 @SessionAttributes 是如何被Spring MVC处理的,这一流程不是很清楚, 经过Google资料,有了 ...
- 【C#】在窗体中水平居中的控件,到了XP下不居中的解决办法
我时不时会遭遇这个操蛋问题,今天得闲研究了一下,解决如下: A.将窗体FormBorderStyle属性改为Fixed系,当然这会导致用户不能拖拉窗口大小,所以你可能需要B计划↓ B.确保在[VS]中 ...
- Debian7安装GCC4.8
参考一 参考二 参考三 参考四 Ubuntu13.04下编译GCC-4.8.2源码并安装成功 CentOS 6编译安装GCC4.8 CentOS 6.4系统编译安装gcc-4.8. ...
- java.lang.NoClassDefFoundError: com/google/gson/Gson错误的解决
SSH返回JSON格式的数据时,需要用到gson,将gson-1.6.jar添加进Build path以后运行,出错: 后来把gson-1.6.jar复制到WEB-INF/lib/下再运行,就没再出这 ...
- Tigase数据库结构(1)
Tigase数据库有很多张表,其中最主要的是3张表:tig_users,tig_nodes和tig_pairs. 1.tig_users tig_users存储用户信息,有uid(主键,用户ID),u ...