Animating all items in Animation-list

2019-03-02 12:52发布

问题:

I have a list of images(around 10) in my "image_list.xml" under "animation-list" specified with "item". And I have made one "fade_in.xml" file for fade in effect of the imgaes. The code works fine without errors.

The only problem I have is that the fade in effect appears in the first image but not in the other items of the image_list. Please tell me a way to make this possible so that all the images have fade in effect when they appear.My code is:

image_list.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
        android:oneshot="false">
<item android:drawable="@drawable/sample1"
    android:duration="2000"/>
<item android:drawable="@drawable/sample2"
    android:duration="2000"/>
<item android:drawable="@drawable/sample3"
    android:duration="2000"/>
<item android:drawable="@drawable/sample4"
    android:duration="2000"/>
. . .
</animation-list>

ImagesActivity.java

   public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    ImageView img_change = (ImageView) findViewById(R.id.images);
    Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    img_change.setBackgroundResource(R.drawable.image_list);
    AnimationDrawable splashAnimation = (AnimationDrawable) img_change.getBackground();
    if(hasFocus) {
        img_change.setAnimation(animationFadeIn);
        splashAnimation.start();
}}

回答1:

Add enter and exit fade duration before start AnimationDrawable

AnimationDrawable splashAnimation = ((AnimationDrawable) iv.getBackground());

splashAnimation .setEnterFadeDuration(500);
splashAnimation .setExitFadeDuration(500);

animDrawable.start();

It Works for me.

Happy Coding