1、AnimationSet用来做什么的?
AnimationSet类是Android系统中的动画集合类,用于控制View对象进行多个动作的组合,该类继承于Animation类。
2、AnimationSet有哪些公有方法?
void |
addAnimation(
Animation a)
Add a child animation to this animation set.添加动画到AnimationSet
|
long |
computeDurationHint()
The duration hint of an animation set is the maximum of the duration hints of all of its component animations.
|
List< Animation> | getAnimations() |
long |
getDuration()
The duration of an AnimationSet is defined to be the duration of the longest child animation.
|
long |
getStartTime()
When this animation should start.
|
boolean |
getTransformation(long currentTime,
Transformation t)
The transformation of an animation set is the concatenation of all of its component animations.
|
void |
initialize(int width, int height, int parentWidth, int parentHeight)
Initialize this animation with the dimensions of the object being animated as well as the objects parents.
|
void |
reset()
Reset the initialization state of this animation.
|
void |
restrictDuration(long durationMillis)
Ensure that the duration that this animation will run is not longer than
durationMillis.确保该动画将运行时间不超过durationmillis
|
void |
scaleCurrentDuration(float scale)
How much to scale the duration by.当前时间缩放scale
|
void |
setDuration(long durationMillis)
Sets the duration of every child animation.设置每个动画持续时间 |
void |
setFillAfter(boolean fillAfter)
If fillAfter is true, the transformation that this animation performed will persist when it is finished.如果fillAfter是trure,保持结束时动画状态
|
void |
setFillBefore(boolean fillBefore)
If fillBefore is true, this animation will apply its transformation before the start time of the animation.
|
void |
setRepeatMode(int repeatMode)
Defines what this animation should do when it reaches the end.定义这个动画应该做什么,当它到达结束。
|
void |
setStartOffset(long startOffset)
When this animation should start relative to the start time.
|
void |
setStartTime(long startTimeMillis)
Sets the start time of this animation and all child animations
|
boolean |
willChangeBounds()
Indicates whether or not this animation will affect the bounds of the animated view.动画是否影响指定的视图范围 |
boolean |
willChangeTransformationMatrix()
Indicates whether or not this animation will affect the transformation matrix.动画是否影响转换矩阵 |
RESTART
or REVERSE
Demo:
package com.lxm.animationset;
import android.util.Log;
/**
* Created by Administrator on 2015/9/19.
*/
public class MyLog {
private static boolean flag = true;
public static void d(Object object){
if(flag) {
Log.d("myLog", object == null ? null : object.toString());
}
}
}
<span style="font-family: Arial, Helvetica, sans-serif;">package com.lxm.animationset;</span>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener{
private ImageView mTv;
private Button mStartBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (ImageView) findViewById(R.id.id_tv);
mStartBtn = (Button) findViewById(R.id.id_startAnimation);
mStartBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.id_startAnimation:
AnimationSet anim = new AnimationSet(true);
AlphaAnimation a=new AlphaAnimation(1f,0f);
RotateAnimation ra = new RotateAnimation(0,680, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
a.setDuration(3000);
anim.addAnimation(a);
anim.addAnimation(ra);
anim.setDuration(3000);
anim.setStartOffset(1000);
// anim.setFillAfter(false);
anim.setFillBefore(false);
a.setRepeatCount(2);
ra.setRepeatCount(2);
//动画结束之后,反向执行动画
ra.setRepeatMode(Animation.REVERSE);
long maxTime = anim.computeDurationHint();
long aDuration = a.getDuration();
List<Animation> animationList = anim.getAnimations();
long startTime = anim.getStartTime();
mTv.startAnimation(anim);
MyLog.d("最大的持续时间" + maxTime);
MyLog.d("透明动画持续"+aDuration);
MyLog.d("动画开始时间"+startTime);
for (int i=0;i<animationList.size();i++){
MyLog.d("动画"+animationList.get(i).getClass().getName());
}
break;
}
}
}
获取最大的持续时间,透明动画持续时间,动画开始时间,获取动画对象。
为什么最大的持续时间是9000?
因为重复两次,每次的时间是3000,加上第一次,所以(1+2)*3000=9000
为什么动画开始时间是-1?
因为AlphaAnimation,RotateAnimation的开始时间都没有设置,取不到,所以显示的是-1。可以看一下getStartTime源码,
@Override
public long getStartTime() {
long startTime = Long.MAX_VALUE;
final int count = mAnimations.size();
final ArrayList<Animation> animations = mAnimations;
for (int i = 0; i < count; i++) {
Animation a = animations.get(i);
startTime = Math.min(startTime, a.getStartTime());
}
return startTime;
}
如果加上这两行代码
a.setStartTime(1000);
ra.setStartTime(2000);
结果如图:
为什么动画开始时间是1000?
看了getStartTime源码会知道,因为返回的是List<Animation>中最小开始时间