2017-04-12 15 views
0

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を試していたが、それは私が欲しくない画像のクロスフェードを引き起こしている。

ありがとうございました!

答えて

0

ObjectAnimatorを使用することができます。これには、setDurationと開始遅延のメソッドがあり、必要に応じて計算できます。

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha",0, 1); 
objectAnimator.setDuration(DEFAULT_ANIMATION_DURATION); 
objectAnimator.setStartDelay(milliseconds); 
objectAnimator.start(); 

あなたは3つの異なるオブジェクトアニメーターを作成して設定することができます。 for more refer this link

+1

こんにちはMuhibを参照してください:以下のコードを参照してください。私は、あなたが提案したように、遅延を伴うアニメーションを作成しました。それはすべてが正常に動作しているようで、リスナーを介したアニメーションの連鎖は必要ありません。 – Daniel

0

この動作を実装するには、viewswitcherを使用できます。あなたの答えをありがとう、

レイアウト

<ViewSwitcher 
    android:id="@+id/switcher" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:inAnimation="@anim/fade_in" 
    android:outAnimation="@anim/fade_out" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/sunset" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/clouds" /> 
</ViewSwitcher> 

コード

((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     ViewSwitcher switcher = (ViewSwitcher) v; 

     if (switcher.getDisplayedChild() == 0) { 
      switcher.showNext(); 
     } else { 
      switcher.showPrevious(); 
     } 
    } 
}); 

このlink

+0

あなたの答えをありがとうが、これは動作していません。 'ViewSwitcher'には2つの子ビューしか含めることができません。そして、私が質問に書いたように、私は 'イメージスウィッチャー'も試みていたが、ティはクロスフェードを引き起こしていた。 – Daniel

関連する問題