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. Mozilla对HTML5规范支持列表

    翻译自Mozilla Developer Network 在2009年10月28日,HTML 5规范草稿在网络超文本应用技术工作组(WHATWG)中基本出于最后定稿阶段,这意味着HTML 5标准基本定 ...

  2. 又爱又恨的BOOTSTRAP

    搞本书,看了一天,确实,,UIKIT比它好用... 但,艺多不压身吧. 今天自己抄了个大概的,不用其它插件,,但那手风琴,真的找了很多,没有中意的... <!DOCTYPE html> & ...

  3. http://blog.csdn.net/dyllove98/article/details/7706218

    http://blog.csdn.net/dyllove98/article/details/7706218

  4. Class类文件结构、类加载机制以及字节码执行

    一.Class类文件结构 Class类文件严格按照顺序紧凑的排列,由无符号数和表构成,表是由多个无符号数或其他数据项构成的符合数据结构. Class类文件格式按如下顺序排列:   类型 名称 数量 u ...

  5. 简析LIVE555中的延时队列

    http://www.cnblogs.com/nightwatcher/archive/2011/04/10/2011158.html 最近在看LIVE555的源码,感觉其中的延时队列写的不错,于是就 ...

  6. void (*fun)(void);什么意思?

    2440test程序中的Main.c中在结构体中有这么一句: void (*fun)(void); 后查阅资料得知这句代码的意思是: 定义一个函数指针. 比如:定义一个指向函数的指针,该函数有一个整形 ...

  7. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

  8. Redundant Call to Object.ToString()

    Redundant Call to Object.ToString() The + operator for string is overloaded to call String.Concat pa ...

  9. 基于B/S模式的推送

    基于B/S模式的推送 https://www.ibm.com/developerworks/cn/web/wa-lo-comet/ 传统模式的 Web 系统以客户端发出请求.服务器端响应的方式工作.这 ...

  10. WordPress Sharebar ‘page’参数跨站脚本漏洞

    漏洞名称: WordPress Sharebar ‘page’参数跨站脚本漏洞 CNNVD编号: CNNVD-201309-468 发布时间: 2013-09-26 更新时间: 2013-09-26 ...