ボタンによってトリガーされ、画面上で回転およびズームする 3 つのアニメーション化された ImageView を使用して、単純な「3-2-1」カウントダウンを作成したいと考えています。単一の 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 構成のアニメーションの 1 つを次に示します (これは 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);
}
ズーム、アルファ、回転アニメーションを同時に実行するために、アニメーション セットを使用しています。これまでのところ、ImageView ソースを変更して、アニメーション リスナーの onAnimationEnd() 内からアニメーションを再開しようとしましたが、うまくいきませんでした。配列内の画像をループするだけでも機能しません。
アニメーションを 3 回ループさせて、各反復の最後に ImageView を変更するにはどうすればよいですか? 最後のイテレーションの最後に、インテントを作成して新しいアクティビティをロードしたいと考えています。
前もって感謝します :)
更新... 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;
}