Android实现动画循环的方式
每次想到循环播放、重复执行时,脑海中总是冒出在while(true)的实现方式。
Thread thread = new Thread(new Runnable(){ public void run(){ while(true){ // do animation operation } } }).start();
但这种方式总给人一种不可靠的感觉。
为此,在这多记录几种实现方式,方便以后参考。
第一种:使用属性动画实现(ObjectAnimator)
Path path = new Path(); path.addOval(100, -500, 500, -100, Path.Direction.CW); ObjectAnimator ivGreenObjectAnimator = ObjectAnimator.ofFloat(ivRed, View.TRANSLATION_X, View.TRANSLATION_Y, path); ivGreenObjectAnimator.setDuration(5000).setRepeatCount(ValueAnimator.INFINITE); ivGreenObjectAnimator.setRepeatMode(ValueAnimator.RESTART); ivGreenObjectAnimator.start();
效果:绿色小球沿着椭圆循环运动。
第二种:使用属性动画实现(ViewPropertyAnimator)
private void startAnimateByViewAnimationProperty() { ViewPropertyAnimator ivGreenAnimate = ivGreen.animate();int[] positions = new int[]{600, 100, 100, 400}; ivGreenAnimate.translationX(400).setDuration(500).setListener(new ComplexAnimatorListener(ivGreen, positions)); } static class ComplexAnimatorListener implements Animator.AnimatorListener { View view; int[] positions; int times = 0; public ComplexAnimatorListener(View view, int[] positions) { this.view = view; this.positions = positions; } @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { Log.v("qian", "repeat running!"); times++; if (times % 4 == 1) { view.animate().translationY(positions[0]).setDuration(500).setListener(this); } else if (times % 4 == 2) { view.animate().translationX(positions[1]).setDuration(500).setListener(this); } else if (times % 4 == 3) { view.animate().translationY(positions[2]).setDuration(500).setListener(this); } else view.animate().translationX(positions[3]).setDuration(500).setListener(this); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }
效果:绿色小球沿着矩形循环运动。
第三种:使用一般动画实现(TranslateAnimation)
TranslateAnimation translateAnimation = new TranslateAnimation(-400, -100, -400, -100); translateAnimation.setRepeatCount(Animation.INFINITE); translateAnimation.setRepeatMode(Animation.REVERSE); ivGreen.startAnimation(translateAnimation);
效果:小球沿着矩形循环运动。
第四种:使用handler及其callback递归调用实现
Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if(msg.what==1){ startAnimation(); } return false; } }); private void startAnimation(){ //do animation operation Message message = new Message(); message.what = 1; handler.sendMessage(message); }
一次执行完又执行
第五种:使用handler及其runnable递归调用实现
Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { ViewPropertyAnimator animate = binding.ivBlue.animate(); animate.translationXBy(-200).translationYBy(-200).scaleX(2.0f).scaleY(2.0f). setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(500).start(); printProperty(binding.ivBlue); handler.postDelayed(this, 500); } };
private void startAnimation(){
handler.postDelayed(this, 500);
}
递归调用postDelayed方法。