xml如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:repeatCount="infinite" android:repeatMode="restart"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000"/> <alpha android:startOffset="3000" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000"/> </set>
有人说在代码中设置setRepeatCount(Animation.INFINITE);就可以了, 实际测试下来还是不行,最好是通过设置AnimationListener, 在onAnimationEnd事件中再次开始动画来完成“repeat”的。
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing); anim.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing); anim.setAnimationListener(this); brackets.startAnimation(anim); } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } });
-------------------------------------------------------------------------------------------------------------------------------------
题外话:
虽然只用alpha动画, 设置如下也可以实现闪烁的效果,但是不太好控制显示时间。如果想让渐变的时间不一致就更没辙了。
alpha.setRepeatCount(Animation.INFINITE);
alpha.setRepeatMode(Animation.REVERSE);
-------------------------------------------------------------------------------------------------------------------------------------
讨论帖: http://stackoverflow.com/questions/4480652/android-animation-does-not-repeat