您的位置:首页 > 移动开发 > Android开发

Android 基础动画总结

2015-02-12 16:20 495 查看

1.补间动画 View Animation(Tween Animation)

1.1位移动画

xml定义:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="300"
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="200"/>
<!--
fromXDelta  动画起始位置的横坐标
toXDelta    动画起结束位置的横坐标
fromYDelta  动画起始位置的纵坐标
toYDelta    动画结束位置的纵坐标
duration    动画的持续时间
100%p 表示自身的长度的位置
-->
代码实现:

Animation  animation = new TranslateAnimation(0,50,0,50);
参数1:x轴的起始位置
参数2:x轴的终止位置
参数3: y轴的起始位置
参数4:y轴的终止位置
相对于原图位置的原点(图片的右上角为0,0),如果不想用这个点作为参照点,可以使用其他构造
TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
参数1,参数3,参数5,参数7就是设置参照点的方式

1.2旋转动画

xml定义:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="200"  />
<!--
fromDegrees:表示旋转的起始角度
toDegrees:表示旋转的结束角度
duration    动画的持续时间
from<to:顺时针旋转
from>to:逆时针旋转
-->
代码实现:

Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
参数1:旋转的起始角度
参数2:旋转的终止角度
参数3:旋转中心的x轴取值参照方式
参数4:中心点x轴的取值
参数5:旋转中心的y轴取值参照方式
参数6:中心点y轴的取值

1.3透明度动画

xml定义:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="200"/>
<!--
fromAlpha :起始透明度
toAlpha:结束透明度
1.0表示完全不透明
0.0表示完全透明
-->
代码实现:

Animation animation = new AlphaAnimation(1f,0.1f);
参数1: 起始透明度;
参数2: 目标透明度;

1.4缩放动画

xml定义:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="0.5"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"/>
<!--
fromXScale:表示沿着x轴缩放的起始比例
toXScale:表示沿着x轴缩放的结束比例
fromYScale:表示沿着y轴缩放的起始比例
toYScale:表示沿着y轴缩放的结束比例
1.0表示原来的大小
50%表示图片中心点
-->
代码实现:

Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
参数1:x方向起始大小(1f表示原图大小)
参数2:x方向终止大小(0.2f表示原图的0.2倍)
参数3:y方向起始大小(1f表示原图大小)
参数4:y方向终止大小(0.2f表示原图的0.2倍)
参数5:缩放中心点x轴取值的参照方式
参数6:中心点x轴的取值(0.5f表示相对与原图的0.5倍)
参数7:缩放中心点y轴取值参照方式
参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
(注:参照方式:Animation.RELATIVE_TO_SELF:自身   Animation.RELATIVE_TO_PARENT:父控件   Animation.ABSOLUTE:绝对)

1.5 共有属性

android:duration(播放时间)
android:repeatCount: 重复的次数  默认值是0 代表旋转1次,值为-1或者infinite时,表示动画永不停止
android:repeatMode:  设置重复的模式。默认是restart。(当repeatCount的值大于0或者为infinite时才有效)
设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
对应代码方法:
Animation.setDuration(long durationMillis)
Animation.setRepeatCount(int repeatCount)
Animation.setRepeatMode(int repeatMode)

android:interpolator(动画的渲染器)
accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
decelerate_interpolator(动画减速器) 使动画在开始的时候 最快,然后逐渐减速
accelerate_decelerate_interpolator(动画加速减速器)慢到快(中间位置最快)到慢
anticipate_interpolator(动画前反向器) 先往动画移动的反方向移动一点然后在沿着设定的动画移动
overshoot_interpolator(动画后反向器) 最后超出目的值然后缓慢改变到目的值
anticipate_overshoot_interpolator (动画双反向器) 先往动画的反方向移动一点,然后沿着设定的方向移动到终点之后继续移动一点然后在回弹到最终设定的位置
bounce_interpolator(动画弹跳器) 快到目的值时值会跳跃
cycle_interpolator(动画循环器) 动画循环一定次数
linear_interpolator(动画匀速器) 线性均匀改变
overshoot_interpolator(动画减速器) 快速到达终点并超出一小步最后回到终点
使用:android:interpolator="@android:anim/accelerate_interpolator"

对应代码方法:
Animation.setInterpolator(Context context, int resID) //R.anim.accelerate_interpolator
Animation.setInterpolator(Interpolator i)
TimeInterpolator          一个接口,允许你自定义interpolator,以下几个都是实现了这个接口
AccelerateInterpolator        加速,开始时慢中间加速
DecelerateInterpolator        减速,开始时快然后减速
AccelerateDecelerateInterolator   先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator       反向 ,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator   反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator          跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleInterpolator          循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator         线性,线性均匀改变
OvershottInterpolator        超越,最后超出目的值然后缓慢改变到目的值


2.组合动画

2.1AnimationSet

xml定义:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true" >
<scale  ... />
<rotate ...  />
<translate ...  />
<alpha ...  />
<!--
shareInterpolator:里面动画效果是否共享一个Interpolator
不共享设置android:shareInterpolator="false",每一个动画效果自己添加interpolator。
-->
</set>
代码实现:

AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
AnimationSet animSet = new AnimationSet(false);
animSet.addAnimation(alpha);

2.2 LayoutAnimation(GridLayoutAnimationController)

xml定义:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/alpha"
/>
<!--
delay:延迟多少开始播放
-->
代码实现:

AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
LayoutAnimationController lac=new LayoutAnimationController(alpha);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
ListView.setLayoutAnimation(lac);
LayoutAnimationController.ORDER_NORMAL;    //顺序显示
LayoutAnimationController.ORDER_REVERSE;   //反显示
LayoutAnimationController.ORDER_RANDOM     //随机显示
使用:

<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layoutanimation"/>


2.逐帧动画 Drawable Animation(Frame Animation)

xml定义:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/girl1" android:duration="100" />
<item android:drawable="@drawable/girl2" android:duration="100" />
<item android:drawable="@drawable/girl3" android:duration="100" />
<!--
oneshot:如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放
-->
</animation-list>
代码实现:

AnimationDrawable anim = new AnimationDrawable();
for (int i = 1; i <= 3; i++) {
int id = getResources().getIdentifier("girl" + i, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
anim.addFrame(drawable, 100);
}
播放:

View.setBackgroundResource(R.anim.frame);
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
注:想activity一开始就播放请放在onWindowFocusChanged方法里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: