前一章中总结了android animation中property animation的知识和用法,这一章总结View animation和Drawable animation的有关知识:

View animation就是网上常讲的tweened animation,android.view.animation包包括了tweened animation用到的所有的类,Demo请看例子,地址:http://download.csdn.net/detail/u010966622/5664895

不论是xml的动画还是android code编写的动画,都可以定义一个连续播放的动画.建议用xml文件动画,因为它更容易完成,更容易重运用,更容易修改. xml动画文件放在res/anim文件夹当中.这个文件必须有一个跟元素(<alpha/scale/translate/rotateinterpolator element/set>),默认所有的动画是同时进行的,为了让他们一个接一个进行,你可以定义startoffset属性来控制,就像下边的代码一样:

<set android:shareInterpolator="false">    <scale        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromXScale="1.0"        android:toXScale="1.4"        android:fromYScale="1.0"        android:toYScale="0.6"        android:pivotX="50%"        android:pivotY="50%"        android:fillAfter="false"        android:duration="700" />    <set android:interpolator="@android:anim/decelerate_interpolator">        <scale           android:fromXScale="1.4"           android:toXScale="0.0"           android:fromYScale="0.6"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400"           android:fillBefore="false" />        <rotate           android:fromDegrees="0"           android:toDegrees="-45"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400" />    </set></set>

屏幕坐标为左上角是(0,0),往下往右依次增加.有一些值,比如pivoitX可以被赋值为相对值,比如50%,意思是相对于自己的50%.50意思是想对于父控件的50%.也可以定义Interpolator,就是速度插入器,在上篇property animation中有详细介绍.

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); spaceshipImage.startAnimation(hyperspaceJumpAnimation); 

也可以不调用startAnimation函数,直接定义:animation.setStarttime(),当到达那个时间点后,此动画会自动进行.

Drawable Animation可以帮助你将一个一个Drawable resources一个接一个的播放出来,就像传统的动画一样.AnimationDrawable类是Drawable animation的基础.

Drawable Animation虽然是动画,但是还是由Drawable resources组合而成的,所以其xml文件一般放在res/drawable,xml文件是由<animation-list>元素组成其根节点,<item>组成其框架,下面是例子:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true">    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /></animation-list>

android:oneshot="true"表示其只进行一次播放,然后停在最后一个drawable,oneshot被设为false的时候,它将会一直循环播放.下边是一个例子,当屏幕被触摸时,会将此动画加载在这个imageview上并且运行
AnimationDrawable rocketAnimation;public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();}public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) {    rocketAnimation.start();    return true;  }  return super.onTouchEvent(event);}

在上面的例子中,我们可以看到,方法start()的调用不能在onCreate()方法中调用,因为要动画的view还未被添加到窗口当中,如果想立刻运行动画的话,你可以重写onWindowfocuschanged()方法,当此activity被放到最前端的时候自动执行.

更多相关文章

  1. Android用Sax,Pull,Dom解析xml
  2. mono for android之文件系统与应用程序首选项
  3. android之简单数据存储Preference
  4. Android编译大全(六、七)
  5. layout设计 屏幕适配
  6. Android(安卓)Animations动画使用详解
  7. Android(安卓)播放 Gif 图片控件
  8. animation_
  9. android apkbuilder.bat批处理

随机推荐

  1. Python3利用Qt5实现简易的五子棋游戏
  2. 5个数组函数实例演示
  3. CRM开源系统源码|客户管理系统PHP源码
  4. Java程序设计之12个经典样例
  5. python实现多线程并得到返回值的示例代码
  6. python实现hao看的时钟效果
  7. 文件包含与类操作
  8. MYSQL -(二)文件上传/分页展示
  9. PHP中实例演示单文件上传与多文件上传
  10. 简单模态框演示