【Android基础知识】Drawable Animation和View Animation

来源:互联网 发布:win7数据库安装失败 编辑:程序博客网 时间:2024/06/02 08:30

Android中的动画主要分为三类

1.Drawable Animation

2.View Animation

3.Property Animation

这里介绍其中的两类,Drawable Animation(逐帧动画)和View Animation

Drawable Animation 逐帧播放每一张图片,就好像动画播放一样。

View Animation  包括 平移动画 translate 、缩放动画 scale 、旋转动画 rotate 、渐变动画 alpha

Drawable Animation (逐帧动画)

我们使用 animation-list 来定义动画xml文件,在工程res的anim文件夹或者drawable文件夹下建立我们的动画文件

anim_list.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false"><!-- onshot 为true表示只播放一次,false表示循环播放 -->    <item        android:drawable="@drawable/one"        android:duration="500"/>    <item        android:drawable="@drawable/two"        android:duration="500"/>    <item        android:drawable="@drawable/three"        android:duration="500"/>    <item        android:drawable="@drawable/four"        android:duration="500"/>    <item        android:drawable="@drawable/five"        android:duration="500"/>    <item        android:drawable="@drawable/six"        android:duration="500"/></animation-list>
使用AnimationDrawable 启动动画,代码如下,调用stop可以停止动画

private AnimationDrawable walkDrawable;image.setImageResource(R.drawable.anim_list);walkDrawable = (AnimationDrawable)image.getDrawable();walkDrawable.start();


View Animation

在代码中定义动画这里不做介绍,主要介绍在xml中定义动画
渐变动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="1000"        android:fromAlpha="0.1"        android:toAlpha="1.0" >    </alpha></set>
缩放动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <scale        android:duration="2000"        android:fillAfter="false"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.0"        android:toYScale="1.0" /></set>
平移动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="1000"        android:fromXDelta="10"        android:fromYDelta="10"        android:toXDelta="100"        android:toYDelta="100" /></set>
旋转动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="1000"        android:fromDegrees="0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="+360" /></set>
加载xml动画的方式为
Animation loadAnimation;loadAnimation = AnimationUtils.loadAnimation(this, R.anim.xxx);view.startAnimation(loadAnimation);
缩放效果


旋转效果

透明度效果
平移效果

动画续播1,动画1播放完毕后播放动画2,这里使用了动画监听器,监听到动画1播放完毕的时候开始播放动画2
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);image.startAnimation(loadAnimation);final Animation loadAnimation2 = AnimationUtils.loadAnimation(this,R.anim.rotate);//给动画1设置监听loadAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation arg0) {}@Overridepublic void onAnimationRepeat(Animation arg0) {}@Overridepublic void onAnimationEnd(Animation arg0) {//动画1播放完毕播放动画2image.startAnimation(loadAnimation2);}});
动画续播2:直接在xml文件中进行动画的定义
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="3000"        android:fromAlpha="0.2"        android:toAlpha="1.0" />    <alpha        android:duration="3000"        android:fromAlpha="1.0"        android:startOffset="3000"        android:toAlpha="0.2" /></set>
调用
loadAnimation = AnimationUtils.loadAnimation(this,R.anim.continue_anim);image.startAnimation(loadAnimation);


闪烁效果,有透明变为不透明,设置倒序重复模式
//闪烁效果AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);alphaAnimation.setDuration(100);alphaAnimation.setRepeatCount(10);//倒序重复REVERSE  正序重复RESTARTalphaAnimation.setRepeatMode(Animation.REVERSE);image.startAnimation(alphaAnimation);

抖动动画,就是左右平移,和闪烁效果一样,也是设置反向重复模式
TranslateAnimation translate = new TranslateAnimation(-50, 50,0, 0);translate.setDuration(1000);translate.setRepeatCount(Animation.INFINITE);translate.setRepeatMode(Animation.REVERSE);image.startAnimation(translate);
overridePendingTransition(设置界面切换动画)
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/decelerate_interpolator" >    <scale        android:duration="1000"        android:fromXScale="0.1"        android:fromYScale="0.1"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.0"        android:toYScale="1.0" />  <alpha        android:duration="1000"        android:fromAlpha="0"        android:toAlpha="1.0" /></set>
zoom_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/decelerate_interpolator"    android:zAdjustment="top" >    <scale        android:duration="@android:integer/config_mediumAnimTime"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:pivotX="50%p"        android:pivotY="50%p"        android:toXScale="0.1"        android:toYScale="0.1" />    <alpha        android:duration="@android:integer/config_mediumAnimTime"        android:fromAlpha="1.0"        android:toAlpha="0" /></set>
设置界面跳转动画
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);


ListView布局动画,多个item同时执行一种效果。如果我们需要一个界面中的多个控件按照相同的动画方式但是每个控件完成该动画的时刻不同的话,就可采用LayoutAnimationController
LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));    //顺序播放    lac.setOrder(LayoutAnimationController.ORDER_NORMAL);    listView.setLayoutAnimation(lac);    listView.startLayoutAnimation();



0 0