Activity之间跳转的动画———淡入淡出,左移右移,缩放

res文件的准备

Activity之间的跳转需要一些动画,让画面不要那么尴尬。

下面是一些效果:
- 淡入、淡出
- 左平移入,左平移出
- 右平移入,右平移出
- 缩,放

在res文件下新建 Resource Directory文件Resource type选择anim类型

这里写图片描述
文件目录
这里写图片描述

主要是三个属性:


  1. fromXDelta:你从X轴哪里来。
  2. toXDelta:你要到X轴哪去。
  3. duration:最最重要的属性,任何动画效果少了它都实现不了,你想这个效果多久实现完毕,总得告诉它一下吧。
  4. Y轴与X轴相似

fade_in.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" >
</alpha>

fade_out.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="2000">
</alpha>

left_tran_in.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromXDelta="-100%"
      android:toXDelta="0"
      android:duration="2000"
      android:fromYDelta="-100%"
      android:toYDelta="0"/>
</set>

left_tran_out.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0"
        android:toXDelta="-100%"
        android:duration="2000"
        android:fromYDelta="0"
        android:toYDelta="-100%"/>
</set>

right_tran_in.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%"
    android:duration="2000"
    android:toXDelta="0" />
</set>

right_tran_out.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
    android:duration="2000"
    android:toXDelta="100%" />
</set>

这里也有几个重要的属性:


  1. pivotX/Y 设置缩放的中心点(默认还是屏幕的左上角)
  2. 这里设置alpha是为了让这个缩放动作不会太死板
  3. 其他属性有与前面效果相似

scale_in.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="200%"
        android:toXScale="100%"
        android:fromYScale="200%"
        android:toYScale="100%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"/>
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000"/>
</set>

scale_out.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="100%"
        android:toXScale="200%"
        android:fromYScale="100%"
        android:toYScale="200%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000" />
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="2000"/>
</set>

MainActivity 中的实现:

使用overridePendingTransition(int enterAnim, int exitAnim) 实现动画效果,在跳转界面的Intent语句下面加入这一句,并传入动画效果就可以了。

  * @param enterAnim A resource ID of the animation resource to use for
     * the incoming activity.  Use 0 for no animation.
     * @param exitAnim A resource ID of the animation resource to use for
     * the outgoing activity.  Use 0 for no animation.

观察其源码就可以发现,需要传入两个animation resource 给这个方法

第一个是 enterAnim 是对于进入的Activity 的动画效果,第二个是 exitAnim 是对于消失的Activity 的动画效果

由此可见其实可以对于这个overridePendingTransition 其实有多种任意的组合方式,可以做出很多奇奇怪怪的效果。

猜你喜欢

转载自blog.csdn.net/aichilubiantan/article/details/79998484