2017-05-05 10 views
0

私は以下の関数を使用して、自分のアクティビティのビューを動的に追加しています。私はこの機能を使って5つのビューを生成しています。これはうまくいきます。私の問題は、私がビューにアニメーションを適用しているとき、すべてのビューがアニメーション化されているときです。私は最初に生成されたビューをアニメートしてから2番目のようにしたい。アクティビティの開始時には、5つのビューすべてがフェードインしています。すでにsetStartOffsetを試しましたが、動作していません。私は何をすべきか?ビューの動的追加(MainActivity.java)用動的に追加されたビューのシーケンスアニメーション

機能:

public void addViewInActivity() { 
    container.addView(addView); 

    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); 

    final long startOffset = 1000; 
    animation.setStartOffset(startOffset); 

    addView.startAnimation(animation); 
} 

fade_in.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha 
     android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:duration="500" 
     android:repeatCount="0" /> 
</set> 
+0

を助けるかもしれない、あなたは、各ビューにIDSを割り当てたのですか? –

+0

各ビューにはidが1つしかありませんが、各ビューに固有のタグが割り当てられています。 –

+0

各ビューにユニークなIDを設定しようとし、アニメーション終了リスナで最初のIDアニメーションを開始し、2番目のアニメーションを開始します。 –

答えて

1

使用すると、これは、それはあなたに

private long startOffset = 1000; 

public void addViewInActivity() { 
    container.addView(addView); 

    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); 
    startOffset += 500; 
    animation.setStartOffset(startOffset); 

    addView.startAnimation(animation); 
} 
+0

私はそれを試して、それは完璧に働いた。迅速な返信をありがとう。あなたの方法はとても簡単です。 –

0

あなたはコンテナにアニメーションを適用することができます。以下の例を考えてみましょう。

これはParentLayoutです。ここでは、複数のビューを動的に追加しています。

<LinearLayout 
android:id="@+id/layoutStatus" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layoutAnimation="@anim/list_layout_animation" 
android:orientation="horizontal" 
android:padding="10.0dip" /> 

後..あなたが行く準備ができているlist_layout_animation

<?xml version="1.0" encoding="utf-8"?> 
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
    android:animation="@anim/slide_in_right" 
    android:delay="0.5" /> 

されて、これはこれだけですslide_in_right

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="400" 
    android:fillAfter="true" 
    android:fromXDelta="-100.0%" 
    android:toXDelta="0.0%" /> 

です!

関連する問題