android中一共提供了两种动画,其一便是tween动画,tween动画通过对view的内容进行一系列的图像变换(包括平移,缩放,旋转,改变透明度)来实现动画效果,动画效果的定义可以使用xml,也可以使用编码来实现。 下面我们逐一查看tween能够实现的动画效果。

先看看工程的整体结构吧:

我们要实现的效果图如图

点击按钮则执行相应的动画操作。

布局文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tweentest.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="透明" /> <Button
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="缩放" /> <Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="旋转" /> <Button
android:id="@+id/translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="移动" /> <Button
android:id="@+id/combo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="组合" />
</LinearLayout> <Button
android:id="@+id/go_other_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doanim"
android:text="GO!" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/jiafeimao" />
</LinearLayout> </LinearLayout>

好了,先来看第一个动画效果。

要实现让图片变成透明的动画效果,先在anim文件夹中新建一个名为alpha的xml文件,关于透明的动画效果都写在这个文件中:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <!-- 花1000毫秒,从不透明(1)变为透明(0) -->
<!--
repeatCount表示重复次数
repeatMode表示开始的模式,restart表示每次重新开始,reverse表示接着来,即不透明->透明->不透明
-->
<alpha
android:duration="2000"
android:fromAlpha="1"
android:repeatCount="5"
android:repeatMode="reverse"
android:toAlpha="0" /> </set>

duration属性表示动画执行的时间,以毫秒为单位,repeatCount表示动画重复的次数,repeatMode属性有两个值,一个是restart,表示动画每次执行完之后都重新开始执行(不透明->透明,不透明->透明….),reverse表示动画每次执行完之后不重新开始而是接着反向执行(不透明->透明->不透明->透明….),换句话说,如果是reverse,则表示动画从不透明变为透明后再慢慢变回不透明,如果是restart则表示动画从不透明变为透明后,然后快速恢复原状。以上这三个属性在所有的tween动画中都有,也是最常用的属性。fromAlpha表示初始透明度,1表示不透明,0表示完全透明;toAlpha表示动画结束时的透明度。

这是动画的xml文件,再看看Java代码是怎样的。

public class MainActivity extends Activity {

    private ImageView iv = null;
private Animation animation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv = (ImageView) this.findViewById(R.id.image1);
}
public void doanim(View v){
switch (v.getId()) {
case R.id.alpha:
//读取动画文件
animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
//动画结束之后使用什么页面填充
//使用动画结束后的样子填充,即保持结束时的画面
animation.setFillAfter(true);
//应用动画特效
iv.startAnimation(animation);
break;
case R.id.scale:
animation = AnimationUtils.loadAnimation(this, R.anim.scale);
iv.startAnimation(animation);
break;
case R.id.rotate:
animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
iv.startAnimation(animation);
break;
case R.id.translate:
animation = AnimationUtils.loadAnimation(this, R.anim.translate);
iv.startAnimation(animation);
break;
case R.id.combo:
animation = AnimationUtils.loadAnimation(this, R.anim.combo);
iv.startAnimation(animation);
break;
case R.id.go_other_activity:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
//设置让MainActivity从左边出,SecondActivity从右边进
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
break;
}
}
}

先拿到ImageView,然后在点击按钮时读取动画文件,并给imageview设置动画效果。

缩放动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <!--
两个1表示开始时图像大小不变,两个2表示图像放大一倍,
如果小于1则表示图像缩小。两个0表示基于左上角进行放大
pivotX如果是50%,表示基于图像中心放大
pivotX如果是50%p,表示基于父级中心放大
infinite如它的英文意思,无限循环 -->
<scale
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="0"
android:pivotY="0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXScale="2"
android:toYScale="2" /> </set>

fromXScale和fromYScale分别表示初始时图像的缩放比例,1表示不缩放,toXScale和toYScale表示动画结束时的缩放比例,2表示动画放大一倍,如果是0.5则表示缩小一倍。pivotX和pivotY表示执行缩放时的参考点,两个值都为0表示是基于图像左上角来执行缩放操作,如果都是50%表示基于图像中心来执行缩放操作,如果是100%表示基于图像右下角执行缩放操作,如果是50%p,表示基于屏幕的中心执行缩放操作,如果是100%p表示基于屏幕的右下角执行缩放操作。

java代码见上文。

旋转动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <!-- fromDegrees表示开始的角度
toDegrees结束的角度,180表示旋转半圈
两个100%表示基于自己的右下角旋转
想让倒着转,把180改为-180即可
-->
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="100%"
android:pivotY="100%"
android:toDegrees="180" /> </set>

这里有两个参数解释,fromDegrees表示初始角度,0表示正常,toDegrees表示旋转角度,180表示顺时针旋转180度,-180表示逆时针旋转180度pivotX参数的意义和缩放动画中的参数意义相同。

Java代码同上文。

移动动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<!-- from表示从哪里开始移动
to表示移动到那里
100%表示移动到自己的右下角
100%p表示移动到屏幕右下角
-->
<translate
android:repeatCount="infinite"
android:repeatMode="reverse"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="100%" /> </set>

fromXDelta表示初始时图像在x轴的位置toXDelta表示结束时图像在X轴的位置。

四种动画效果都已经说完,如果要实现组合效果呢?

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="100%"
android:pivotY="100%"
android:toDegrees="180" /> <alpha
android:duration="2000"
android:fromAlpha="1"
android:repeatCount="5"
android:repeatMode="reverse"
android:toAlpha="0" /> <scale
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="0"
android:pivotY="0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXScale="2"
android:toYScale="2" /> <translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXDelta="100%"
android:toYDelta="100%" /> </set>

把之前所有的动画效果放在一个文件中就可以了。

android中的动画效果可以对任何组件使用,因为组件都是继承自View,而startAnimation(Animation animation)方法就是View中的方法。那么两个Activity之间切换能不能使用动画效果呢?当然可以。上文中的Java代码中,有这么一句:

case R.id.go_other_activity:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
//设置让MainActivity从左边出,SecondActivity从右边进
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
break;

想要实现activity之间切换的动画,使用overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);方法即可,该方法要两个参数,分别表示新activity进入时的动画和旧activity出去时的动画。

进入时动画:

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

出去时动画:

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

效果如图:

MainActivity逐渐移出,SecondActivity逐渐从右边进来。

android之tween动画详解的更多相关文章

  1. Android中的动画详解系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自定义动画,这一篇我们来看看如何将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 如 ...

  2. Android 三种动画详解

    [工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.点我开始Android技术交流] 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让 ...

  3. android之frame动画详解

    上一篇我们说了android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张图按预先设定好的时间依次连续显示. 新建一个android项 ...

  4. Android之Lottie动画详解

    文章大纲 一.Lottie介绍二.Lottie实战三.项目源码下载四.参考文章   一.Lottie介绍 1. 什么是Lottie   Lottie是Android和iOS的移动库,用于解析Adobe ...

  5. Android 开发之动画详解

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...

  6. Android中的动画详解系列【3】——自定义动画研究

    在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的: /* * Copyright (C ...

  7. Android中的动画详解系列【2】——飞舞的蝴蝶

    这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包括下面几个子类: AlphaAnimation ...

  8. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  9. [转]ANDROID L——Material Design详解(动画篇)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 转自:http://blog.csdn.net/a396901990/article/de ...

随机推荐

  1. HTML5解决跨域问题

    HTML5解决跨域问题 由于浏览器的同源策略,网络连接的跨域访问是不被允许的,XHR对象不能直接与非同源的网站处理数据交互.而同源指的是什么呢?同源的范畴包括:规则(协议),主机号(域名.ip等),端 ...

  2. Linux find命令详解

    转自Linux find命令详解 一.find 命令格式 1.find命令的一般形式为: find pathname -options [-print -exec -ok ...] 2.find命令的 ...

  3. Java 声明和访问控制(二) this关键字的访问

    this可以引用本类的静态变量和实例变量,而在静态方法中不能引用实例变量(因为当静态方法加载时,实例变量还没有被定义和初始化) this不可以引用局部变量.例如方法的参数变量,以及在方法中定义的局部变 ...

  4. QT类库与Delphi类库的体系结构对比——两者十分类似!

    今天在看QT对象内存管理的一篇文章时:http://blog.csdn.net/dbzhang800/article/details/6300025想到了一个问题:就是QT类库体系结构与Delphi类 ...

  5. [置顶] 【Git入门之一】Git是神马?

    1.Git是神马? 一个开源的分布式版本控制系统,可以有效的高速的控制管理各种从小到大的项目版本.他的作者就是大名鼎鼎的Linux系统创始人Linus. 2.分布式又是神马? 先看看集中式.简单说来, ...

  6. USACO3.35A Game(记忆化)

    刚开始理解有点误,想成每步都是最优的 ,结果卡在第六组数据上,, 无奈瞧了下别人的 发现自己理解错了,,我看的还是中文翻译.. 简单的记忆化 /* ID: shangca2 LANG: C++ TAS ...

  7. Unity3D-基本导航(NavMesh)功能实现

    1: 打开场景 2:打开Navgation窗口 菜单中: Window --> Navgation, 在Inspector旁边会出现导航界面 这个Objcet的面板是对应当前选择的物体的,旁边的 ...

  8. Muduo源码库研究(笔记汇总)

    声明: 本人学习Muduo源码, 有些代码会对其进行精简, 加上本人的一些理解, 所以与作者的代码可能有些不同. 如有理解错误的地方欢指出. Muduo基础库-时间戳类 http://www.cnbl ...

  9. Android 侧边栏(使用Support Library 4提供的扩展组件)

    本文转自:http://www.apkbus.com/android-117148-1-1.html 写在前面的话:接触Android已经有一段时间了,自己积累的东西也算蛮多的.总结以往的经验,凡是关 ...

  10. (转)Ubuntu下彻底卸载mysql

    感谢原作者,文章内容很实用.原文链接:http://www.blogjava.net/yjhmily/articles/336926.html ============================ ...