3つの画像を表示するアニメーションを作成する必要があります。各画像は完全にフェードインしてフェードアウト(遅い点滅)し、次の画像が表示されるはずです。私は3つのアニメーションを作ってやった。各アニメーションはonAnimationEndリスナーで次のアニメーションを開始します。そのアニメーションのフェードアニメーションで画像を変更する
private void startAnimation() {
final Animation aniIn = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn1 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn2 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
aniIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom0.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom0.setVisibility(View.INVISIBLE);
ivRandom1.startAnimation(aniIn1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom1.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom1.setVisibility(View.INVISIBLE);
ivRandom2.startAnimation(aniIn2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
showFinal();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivRandom0.startAnimation(aniIn);
}
私は戻って0から0から1にalhpaを設定し、このXMLがあります
fade_in_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="300"
android:fromAlpha="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
/>
</set>
をここ
は私のレイアウトです:
<RelativeLayout
android:id="@+id/images_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_image_random_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator0"
android:visibility="visible" />
<ImageView
android:id="@+id/iv_image_random_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator1"
android:visibility="invisible" />
<ImageView
android:id="@+id/iv_image_random_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator2"
android:visibility="invisible" />
</RelativeLayout>
これが働いているが、私はネストされたが好きではありませんコールバック。また、カルバック内のイメージビューへの参照を保持しているので、メモリリークを引き起こしています。
この種のアニメーションを作成するには、より良い方法がありますか?または少なくともメモリリークを回避するには?私はimageswitcherを試していたが、それは私が欲しくない画像のクロスフェードを引き起こしている。
ありがとうございました!
こんにちはMuhibを参照してください:以下のコードを参照してください。私は、あなたが提案したように、遅延を伴うアニメーションを作成しました。それはすべてが正常に動作しているようで、リスナーを介したアニメーションの連鎖は必要ありません。 – Daniel