还记得以前小时候玩GBA口袋妖怪,神奇宝贝进化的时候就有一个动画。现在就用属性动画来实现他吧~
先上传一张效果图
IMG_2767.mp4_1471923480.gifpublic class MainActivity extends AppCompatActivity {
Button btn_start;
ImageView imageView;
float[] moveArray = new float[30];
final int[] imgSource = new int[]{R.mipmap.little, R.mipmap.middle, R.mipmap.max};
int repeatCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < moveArray.length; i++) {
if (i % 2 == 0) {
moveArray[i] = 0f;
continue;
}
moveArray[i] = 20f;
}
btn_start = (Button) findViewById(R.id.btn_start);
imageView = (ImageView) findViewById(R.id.imageView_animation);
imageView.setImageResource(imgSource[repeatCount]);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator ob1 = ObjectAnimator.ofFloat(imageView, "translationX", moveArray);
ObjectAnimator ob2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.7f, 5f);
ObjectAnimator ob3 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.7f, 5f);
ObjectAnimator ob4 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
final AnimatorSet animationSet = new AnimatorSet();
animationSet.setDuration(1000);
AnimatorSet animationSet1 = new AnimatorSet();
animationSet1.playTogether(ob2, ob3, ob4);
animationSet1.setDuration(500);
//AnimatorSetq 嵌套使用
animationSet.playSequentially(ob1, animationSet1);
animationSet.start();
animationSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
repeatCount++;
//全部复原
imageView.setAlpha(1f);
imageView.setScaleX(1f);
imageView.setScaleY(1f);
if (repeatCount == imgSource.length) {
repeatCount = -1;
return;
}
imageView.setImageResource(imgSource[repeatCount]);
animationSet.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
});
}
}
/
主要想说的就是AnimatorSet最强大的地方就是play、with、after、before可以解决所有动画播放的顺序问题,再加上嵌套使用以及添加监听器可以组合处多种花样的动画。
另外因为AnimatorSet没有setRepeatMode以及setRepeatCount的方法,所以也可以在
/
@Override
public void onAnimationEnd(Animator animation) {
animationSet.start();
}
再次调用animatorSet的start()方法实现循环的作用,加上自己的判断条件就可以设置循环的次数。
网友评论