首先,动画一共分两种四类,渐变动画和旋转动画。
动画的实现方式有两种,xml实现和javaCode实现
xml实现:
animation=AnimationUtils.loadAnimation(AnimationActicity.this, R.anim.rotate);
network_iv.startAnimation(animation);
javaCode实现:
AnimationSet animationset = new AnimationSet(true);
rotateAnimation = new RotateAnimation(0.0f, 360.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, // 注意参数是百分比
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
animationset.addAnimation(rotateAnimation); //添加多个动画,动画组合
network_iv.startAnimation(animationset);
Animation类中最重要的方法是applyTransformation(float interpolatedTime, Transformation t)。动画实现的过程中,参数interpolatedTime的变化外围0~1,动画时间通过setDuring(int time)设定。根据interpolatedTime的变化实现自定义的一些动画效果,eg:为了达到的目的是 在收缩动画实现的同时其分类控件长度的变化。因为普通的动画执行的只是动画而已,控件占据的大小不变,而且默认情况下,动画结束后会回到初始状态中。
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime<1f) {
layoutParams.height=0+(int)(llHeight*interpolatedTime);
}else {
layoutParams.height=llHeight;
}
layout.setLayoutParams(layoutParams);
}
layout这个View的高度从0~llHeight。ps:代码中setLayoutParams控制高度。
接下来介绍一下animation的常用属性:
1,duration:设置动画时长
2,fillAfter: 设置为true,动画结束时,停留在最后一帧。默认false
3,fillBefore: 设置为true,动画结束时,停留在第一帧。默认true。只有在fillAfter
为false时,才有效。
fillafter和fillbefore这两个属性是不能够在<set>节点下的</alpha>,</scale>,</translate>,</rotate>中设置的,错误例子:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:fillAfter="true"//这是不生效的
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
/>
</set>
正确的打开方式是在<set>中进行设置,如果没有<set>节点则可以直接在
</alpha>,</scale>,</translate>,</rotate>等中设置:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillBefore="false"
android:fillEnabled="true"
>
<rotate
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:fillBefore="true" >
</rotate>
4,interpolator:设置插值器,为动画添加效果,加速、减速、弹跳等等。
5,repeatMode:重复模式,1,restart 2,reverse
6,repeatCount:重复次数(模式1,原方向执行算一次。模式2,反方向执行一次也算一次),
7,startOffset: 动画时间间隔