我想使用三个动画 ImageView 创建一个简单的“3-2-1”倒计时,这些 ImageView 在屏幕上旋转和缩放,由按钮触发。我对单个 ImageView 的动画没有问题,但我无法顺序执行 3... 然后 2... 然后 1 帧。有人可以帮忙吗?这是 MainActivity 的简化布局 XML
<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rotate"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:text="GO!" />
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="220dp" />
这是我的 XML 配置动画之一(这个将 ImageView 缩小):
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="0"
android:fromXScale="5.0"
android:toXScale="1.0"
android:fromYScale="5.0"
android:toYScale="1.0"
android:duration="600"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
这是 MainActivity Java 文件:
public class MainActivity extends ActionBarActivity {
private Button animateCountdown;
private ImageSwitcher myImageSwitcher;
private static final int[] IMAGES = { R.drawable.icon_3, R.drawable.icon_2, R.drawable.icon_1};
private int currentIMAGESIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
myImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
animateCountdown = (Button) findViewById(R.id.go_button);
animateCountdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
runCountdownAnimation(view);
}
});
}
private void runCountdownAnimation(View view){
//Using an animation set, to allow for concurrent animations.
AnimationSet animationSet = getCountdownAnimationSet();
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
myImageSwitcher.setInAnimation(animationSet);
myImageSwitcher.setOutAnimation(fadeOutAnimation);
while(currentIMAGESIndex < (IMAGES.length - 1)){
myImageSwitcher.setImageResource(IMAGES[currentIMAGESIndex]);
currentIMAGESIndex++;
myImageSwitcher.postDelayed(this, 1000);
}
我正在使用动画集,以便同时运行缩放、alpha 和旋转动画。到目前为止,我已经尝试更改 ImageView 源并从动画侦听器的 onAnimationEnd() 中重新启动动画......但没有运气。仅循环遍历数组中的图像也不起作用。
如何循环动画 3 次,但在每次迭代结束时更改 ImageView?在最后一次迭代结束时,我想创建一个 Intent 并加载一个新活动。
提前致谢 :)
更新...这是 getCountDownAnimationSet() 代码:
private AnimationSet getCountdownAnimationSet(){
Animation rotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
Animation fadeAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom);
AnimationSet animationSet = new AnimationSet(false); //false means don't share interpolators
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(fadeAnimation);
animationSet.addAnimation(zoomAnimation);
return animationSet;
}