Android Activity切换动画overridePendingTransition

前面说一下:100%p中的p相当于是parent,相对于父view的位置,不过100%p和100%是一个效果的。

Activity在切换或者是退出的时候可以使用渐入,滑动,缩放等动态效果。使用的就是方法overridePendingTransition,可以直在Activity当中直接调用。

overridePendingTransition(R.anim.zoomin, R.anim.zoomout) 第一个参数是其实动画,第二个参数是结束动画。此方法在startActivity()或者是finish()后调用,在切换或是退出时就会调用此动画。

      

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Intent phoneIntent=new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);  
  2.                 startActivityForResult(phoneIntent, 1);  
  3.                 overridePendingTransition(R.anim.zoomin, R.anim.zoomout);  
1,淡入淡出效果

淡入淡出的效果Android的包中已经提供了,overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);使用的就是系统自带的淡入淡出的效果。也可以使用自定义的进出动画,假设淡入的为fade_in,淡出为fade_out。

fade_in.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <alpha  
  5.         android:duration="1500"  
  6.         android:fromAlpha="0.0"  
  7.         android:toAlpha="1.0" />  
  8.   
  9. </set>  
fade_out:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <alpha  
  5.         android:duration="1500"  
  6.         android:fromAlpha="1.0"  
  7.         android:toAlpha="0.0" />  
  8.   
  9. </set>  
android:duration="1500"设置的是动画持续的事件,其他设置的透明度的变化就实现了渐入的效果。在Activity当中调用overridePendingTransition(R.anim.fade_in, R.anim.fade_out);就实现了渐入渐出的效果。

2,左右滑动效果

左右滑动的效果Android的效果也提供了,overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);使用的系统的从左向右滑动的效果,系统现在也只支持这一种左右滑动的效果。同样也可以使用自定义动画实现从左向右滑动的效果同时也可以实现从右向左滑动。

1)从左向右滑动

滑动的两个动画分别为slide_left_in和slide_right_out

slide_left_in:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="300"  
  6.         android:fromXDelta="-100.0%p"  
  7.         android:toXDelta="0.0" />  
  8.   
  9. </set>  
slide_right_out:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="300"  
  6.         android:fromXDelta="0.0"  
  7.         android:toXDelta="100.0%p" />  
  8.   
  9. </set>  
在Activity当中调用overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);就实现了从左向右滑动的效果。

2)从右向左滑动

滑动的两个动画分别是slide_right_in和slide_left_out

slide_right_in:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="300"  
  6.         android:fromXDelta="100.0%p"  
  7.         android:toXDelta="0.0" />  
  8.   
  9. </set>  
slide_left_out:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="300"  
  6.         android:fromXDelta="0.0"  
  7.         android:toXDelta="-100.0%p" />  
  8.   
  9. </set>  
在Activity当中调用overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);就实现了从右向左滑动的效果。

3,缩放效果

在Android的包中没有提供相应的动画效果只能自定义动画,自定义动画分别是zoomin和zoomout。

zoomin:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/decelerate_interpolator" >  
  4. <scale  
  5. android:duration="@android:integer/config_mediumAnimTime"  
  6. android:fromXScale="2.0"  
  7. android:fromYScale="2.0"  
  8. android:pivotX="50%p"  
  9. android:pivotY="50%p"  
  10. android:toXScale="1.0"  
  11. android:toYScale="1.0" />  
  12. </set>  
zoomout:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/decelerate_interpolator"  
  4. android:zAdjustment="top" >  
  5. <scale  
  6. android:duration="@android:integer/config_mediumAnimTime"  
  7. android:fromXScale="1.0"  
  8. android:fromYScale="1.0"  
  9. android:pivotX="50%p"  
  10. android:pivotY="50%p"  
  11. android:toXScale=".5"  
  12. android:toYScale=".5" />  
  13. <alpha  
  14. android:duration="@android:integer/config_mediumAnimTime"  
  15. android:fromAlpha="1.0"  
  16. android:toAlpha="0" />  
  17. </set>  
在Activity当中overridePendingTransition(R.anim.zoomin, R.anim.zoomout);就可以实现类似缩放的效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值