public class GuideActivity extends Activity {
// 显示图片的ImageView组件
private ImageView guidePicture;
// 要展示的一组图片资源
private Drawable[] pictures;
// 每张展示图片要执行的一组动画效果
private Animation[] animations;
// 当前执行的是第几张图片(资源索引)
private int currentItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
initView();
initData();
}
private void initView() {
// 实例化ImageView引导图片
guidePicture = (ImageView) findViewById(R.id.guide_picture);
// 实例化引导图片数组
pictures = new Drawable[] {
getResources().getDrawable(R.drawable.guide_pic1),
getResources().getDrawable(R.drawable.guide_pic2),
getResources().getDrawable(R.drawable.guide_pic3) };
// 实例化动画效果数组
animations = new Animation[] {
AnimationUtils.loadAnimation(this, R.anim.guide_fade_in),
AnimationUtils.loadAnimation(this, R.anim.guide_fade_in_scale),
AnimationUtils.loadAnimation(this, R.anim.guide_fade_out) };
}
private void initData() {
// 给每个动画效果设置播放时间
animations[0].setDuration(1500);
animations[1].setDuration(3000);
animations[2].setDuration(1500);
// 给每个动画效果设置监听事件
animations[0].setAnimationListener(new GuideAnimationListener(0));
animations[1].setAnimationListener(new GuideAnimationListener(1));
animations[2].setAnimationListener(new GuideAnimationListener(2));
// 设置图片动画初始化默认值为0
guidePicture.setImageDrawable(pictures[currentItem]);
guidePicture.startAnimation(animations[0]);
}
class GuideAnimationListener implements AnimationListener {
private int index;
public GuideAnimationListener(int index) {
// TODO Auto-generated constructor stub
this.index = index;
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
// 重写动画结束时的监听事件,实现了动画循环播放的效果
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (index < (animations.length - 1)) {
guidePicture.startAnimation(animations[index + 1]);
} else {
currentItem++;
if (currentItem > (pictures.length - 1)) {
currentItem = 0;
}
guidePicture.setImageDrawable(pictures[currentItem]);
guidePicture.startAnimation(animations[0]);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
}
标记为红色的部分,当每次startAnimation时,GuideAnimationListener的对象都是不一样的,所以Index的值一次为0、1、2