還記得以前小時候玩GBA口袋妖怪,神奇寶貝進化的時候就有一個動畫。現(xiàn)在就用屬性動畫來實現(xiàn)他吧~
先上傳一張效果圖
IMG_2767.mp4_1471923480.gif
public 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++;
//全部復(fù)原
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可以解決所有動畫播放的順序問題,再加上嵌套使用以及添加監(jiān)聽器可以組合處多種花樣的動畫。
另外因為AnimatorSet沒有setRepeatMode以及setRepeatCount的方法,所以也可以在
/
@Override
public void onAnimationEnd(Animator animation) {
animationSet.start();
}
再次調(diào)用animatorSet的start()方法實現(xiàn)循環(huán)的作用,加上自己的判斷條件就可以設(shè)置循環(huán)的次數(shù)。