我是靠谱客的博主 平常大地,这篇文章主要介绍Activity之间跳转的动画———淡入淡出,左移右移,缩放res文件的准备,现在分享给大家,希望可以做个参考。

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 其实有多种任意的组合方式,可以做出很多奇奇怪怪的效果。

最后

以上就是平常大地最近收集整理的关于Activity之间跳转的动画———淡入淡出,左移右移,缩放res文件的准备的全部内容,更多相关Activity之间跳转内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部