Android的animation由四种类型组成:alpha(渐变透明度动画效果)、scale(渐变尺寸伸缩动画效果)、translate(画面转换位置移动动画效果)、rotate(画面转移旋转动画效果)

1.scale

scale标签是缩放动画,可以实现动态调控件尺寸的效果,有下面几个属性:

  • android:fromXScale    起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
  • android:toXScale        结尾的X方向上相对自身的缩放比例,浮点值;
  • android:fromYScale    起始的Y方向上相对自身的缩放比例,浮点值;
  • android:toYScale        结尾的Y方向上相对自身的缩放比例,浮点值;
  • android:pivotX           缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标
  • android:pivotY           缩放起点Y轴坐标,取值及意义跟android:pivotX一样。
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50"
android:pivotY="50"
android:duration="700" />

(1)、pivotX取值数值时(50)

这个控件,宽度和高度都是从0放大到1.4倍,起始点坐标在控件左上角(坐标原点),向x轴正方向和y轴正方向都加上50像素;

根据pivotX,pivotY的意义,控件的左上角即为控件的坐标原点,这里的起始点是在控件的原点的基础上向X轴和Y轴各加上50px,做为起始点,如下图中图二所示

图一                                                             图二
    

(2)、pivotX取值百分数时(50%)
下面再看看当pivotX、pivotY取百分数的时候,起始点又在哪里?

上面我们讲了,pivotX的值,当取50%时,表示在原点坐标的基础上加上的自己宽度的50%,看看效果:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700" />

缩放位置大小仍然从0-1.4,只改变pivotX和pivotY;起始点位置如下图中图二所示:

图一                                                                 图二

   
(3)、pivotX取值50%p时

前面说过,当取值在百分数后面加上一个字母p,就表示,取值的基数是父控件,即在原点的基础上增加的值是父标签的百分值。

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="700" />

效果图,及起始点坐标图如下所示:

        

2.从Animation类继承的属性

Animation类是所有动画(scale、alpha、translate、rotate)的基类,这里以scale标签为例,讲解一下,Animation类所具有的属性及意义。

  • android:duration         动画持续时间,以毫秒为单位
  • android:fillAfter          如果设置为true,控件动画结束时,将保持动画最后时的状态
  • android:fillBefore        如果设置为true,控件动画结束时,还原到开始动画前的状态
  • android:fillEnabled      与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
  • android:repeatCount   重复次数
  • android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作
  • android:interpolator    设定插值器,其实就是指定的动作效果,比如弹跳效果等

(1)android:fillAfter:保持动画结束的状态

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"
android:fillAfter="true"
/>

(2)android:fillBefore  还原初始化状态

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"
android:fillBefore="true"
/>

android:fillBefore="true"                                 android:fillEnable="true"

   

上面顺便列出了,当仅设定fillEanble为true时的效果,这两个的标签的效果完全相同。

(3)、android:repeatMode="restart /reverse"  设定回放类型

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"
android:fillBefore="true"
android:repeatCount="1"
android:repeatMode="restart"
/>

androidRepeatMode设为restart                       androidRepeatMode设为reverse
       

三、alpha标签——调节透明度

1、自身属性

  • android:fromAlpha   动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
  • android:toAlpha       动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillBefore="true">
</alpha>

2、从Animation类继承的属性

    • android:duration         动画持续时间,以毫秒为单位
    • android:fillAfter          如果设置为true,控件动画结束时,将保持动画最后时的状态
    • android:fillBefore        如果设置为true,控件动画结束时,还原到开始动画前的状态
    • android:fillEnabled      与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
    • android:repeatCount   重复次数
    • android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
    • android:interpolator    设定插值器,其实就是指定的动作效果,比如弹跳效果等,不在这小节中讲解,后面会单独列出一单讲解。

四、rotate标签——旋转

1、自身属性

  • android:fromDegrees      开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
  • android:toDegrees          结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
  • android:pivotX               缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
  • android:pivotY               缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-650"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true"> </rotate>

围绕自身从0度逆时针旋转650度                            围绕自身从0度顺时针旋转650度

android:fromDegrees="0"                                       android:fromDegrees="0"

android:toDegrees="-650"                                      android:toDegrees="650"

     

2、从Animation类继承的属性

  • android:duration         动画持续时间,以毫秒为单位
  • android:fillAfter          如果设置为true,控件动画结束时,将保持动画最后时的状态
  • android:fillBefore        如果设置为true,控件动画结束时,还原到开始动画前的状态
  • android:fillEnabled      与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
  • android:repeatCount   重复次数
  • android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作
  • android:interpolator    设定插值器,其实就是指定的动作效果,比如弹跳效果等,不在这小节中讲解,后面会单独列出一单讲解

五、translate标签 —— 平移

1、自身属性

    • android:fromXDelta     起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
    • android:fromYDelta     起始点Y轴从标,可以是数值、百分数、百分数p 三种样式
    • android:toXDelta         结束点X轴坐标
    • android:toYDelta         结束点Y轴坐标
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-80"
android:fromYDelta="0"
android:toYDelta="-80"
android:duration="2000"
android:fillBefore="true">
</translate>

2、从Animation类继承的属性

  • android:duration         动画持续时间,以毫秒为单位
  • android:fillAfter          如果设置为true,控件动画结束时,将保持动画最后时的状态
  • android:fillBefore        如果设置为true,控件动画结束时,还原到开始动画前的状态
  • android:fillEnabled      与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
  • android:repeatCount   重复次数
  • android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作
  • android:interpolator    设定插值器,其实就是指定的动作效果,比如弹跳效果等

六、set标签——定义动作合集

前面我们讲解了各个标签动画的意义及用法,但他们都是独立对控件起作用,假设我现在想上面的textView控件做一个动画——从小到大,旋转出场,而且透明度也要从0变成1,即下面的这个效果,该怎么办?

这就需要对指定的控件定义动作合集,Set标签就可以将几个不同的动作定义成一个组;

属性:

set标签自已是没有属性的,他的属性都是从Animation继承而来,但当它们用于Set标签时,就会对Set标签下的所有子控件都产生作用。

属性有:(从Animation类继承的属性)

  • android:duration         动画持续时间,以毫秒为单位
  • android:fillAfter          如果设置为true,控件动画结束时,将保持动画最后时的状态
  • android:fillBefore        如果设置为true,控件动画结束时,还原到开始动画前的状态
  • android:fillEnabled      与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
  • android:repeatCount   重复次数
  • android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作
  • android:interpolator    设定插值器,其实就是指定的动作效果,比如弹跳效果等
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true"> <alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/> <scale
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"/> <rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"/> </set>

七、实例——如何将动画XML文件应用于控件中

上面我仅仅是列出了每个标签及其属性的意义及应用之后的效果演示,但上面是如何将定义动画的xml应用到textView控件中的却迟迟没说,这一小节,就以scale动画为例,讲述如何将定义好的scle动作添加到指定控件中。

先看最终效果图:

1、新建工程、新建scale动画文件(scaleanim.xml)

新建一个工程,并且在res文件夹下,新建一个anim文件夹,然后再新建一个scaleanim.xml文件,结构如图所示:

scaleanim.xml的代码为:(从TextView中心点,从0放大到1.4倍,反复一次,最后还原到初始化状态)

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"
android:fillBefore="true"
android:repeatCount="1"
android:repeatMode="restart"

2、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.harvic.animation_demo.MainActivity" > <Button android:id="@+id/btn_animation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="scale animation"/>
<TextView
android:id="@+id/tv"
android:layout_width="100dip"
android:layout_height="200dip"
android:background="#ff00ff"
android:text="@string/hello_world"
android:layout_gravity="center_horizontal"/> </LinearLayout>

3、JAVA代码

public class MainActivity extends Activity {  

    Button scaleBtn ;
Animation scaleAnimation; TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scaleanim);
scaleBtn = (Button)findViewById(R.id.btn_animation);
tv =(TextView)findViewById(R.id.tv); scaleBtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.startAnimation(scaleAnimation);
}
}); } }

(1)通过scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scaleanim);从XML文件中获取动画

(2)利用startAnimation将动画传递给指定控件显示

Animation动画(一)的更多相关文章

  1. CSS3 animation 动画

    今天看到一个很酷的logo看了下他用的是animation 动画效果,就拿来做例子 浏览器支持 Internet Explorer 10.Firefox 以及 Opera 支持 animation 属 ...

  2. css3 animation动画特效插件的巧用

    这一个是css3  animation动画特效在线演示的网站 https://daneden.github.io/animate.css/ 下载 animate.css文件,文件的代码很多,不过要明白 ...

  3. Android Property Animation动画

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  4. android Animation 动画绘制逻辑

    参考:http://www.jianshu.com/p/3683a69c38ea 1.View.draw(Canvas) 其中步骤为:/* * Draw traversal performs seve ...

  5. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  6. css3 animation动画技巧

    一,css3 animation动画前言 随着现在浏览器对css3的兼容性越来越好,使用css3动画来制作动画的例子也越来越广泛,也随着而来带来了许多的问题值得我们能思考.css3动画如何让物体运动更 ...

  7. 【Android 基础】Animation 动画介绍和实现

    在前面PopupWindow 实现显示仿腾讯新闻底部弹出菜单有用到Animation动画效果来实现菜单的显示和隐藏,本文就来介绍下吧. 1.Animation 动画类型 Android的animati ...

  8. css3 transition属性变化与animation动画的相似性以及不同点

    下面列子中的2个图片的效果. http://zqtest.e-horse.cn/DongXueImportedCar/assets/mouseOverAnimate.html 第一个为transiti ...

  9. Android中xml设置Animation动画效果详解

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  10. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

随机推荐

  1. linux学习笔记---一些有趣的命令

    一 在说链接之前我们哈需要说明一个东西,就是inode,一个文件的名字可以有多个,但是inode里的i-number却只有一个,(inode是一个数据结构,里面存放文件的各种属性,属主,属组,权限,大 ...

  2. Node.js流

    什么是流? 流是可以从一个源读取或写入数据到连续的目标对象.在Node.js,有四种类型的数据流. Readable - 其是用于读操作. Writable - 用在写操作. Duplex - 其可以 ...

  3. 【Linux】鸟哥的Linux私房菜基础学习篇整理(三)

    1. gzip [-cdtv#] filename:压缩.参数:-c:将压缩的数据输出到屏幕上,可通过数据重定向进行处理:-d:解压缩的参数:-t:可以用来检验一个压缩文件的一致性,查看文件有无错误: ...

  4. 【HDOJ】2369 Broken Keyboard

    字符串. #include <cstdio> #include <cstring> ]; ]; int main() { int n, len; int i, j, k, tm ...

  5. linux内核空间与用户空间信息交互方法

    linux内核空间与用户空间信息交互方法     本文作者: 康华:计算机硕士,主要从事Linux操作系统内核.Linux技术标准.计算机安全.软件测试等领域的研究与开发工作,现就职于信息产业部软件与 ...

  6. (转载)mysql查询今天、昨天、7天、近30天、本月、上一月数据

    (转载)http://blog.163.com/dreamman_yx/blog/static/26526894201053115622827/ 查询 今天 select * from 表名 wher ...

  7. kafka在zookeeper中的存储结构

    参考site:http://kafka.apache.org/documentation.html#impl_zookeeper 1.zookeeper客户端相关命令 在确保zookeeper服务启动 ...

  8. Android开发必知--自定义Toast提示

    开发过Android的童鞋都会遇到一个问题,就是在打印Toast提示时,如果短时间内触发多个提示,就会造成Toast不停的重复出现,直到被触发的Toast全部显示完为止.这虽然不是什么大毛病,但在用户 ...

  9. linux0.12 解决编译问题常用命令

    解决编译问题时,经常需要修改所有的Makefile,特别定义了下面几条命令方便修改. function msed() { find -name "Makefile" -exec s ...

  10. windows下python安装paramiko

    Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...