tomislav2012
tomislav2012

Reputation: 117

Android animating an animation-list

My question, is it possible to animate an item in an animation-list. Specifically, say you have:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">  
   <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
   <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
   <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

I want to fade the alpha of each <item> rather than simply jump from one image to the next, is it possible?

Upvotes: 6

Views: 6205

Answers (1)

Mark Allison
Mark Allison

Reputation: 21924

You'll need to use tweened animations to do this. Essentially what you need to do is have two ImageView objects, one for the current image, and one for the new image. Create two tweened animations for res/anim/fadeout.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="1.0" 
    android:toAlpha="0.0"
    android:startOffset="500" 
    android:duration="500" />

and res/anim/fadein.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0"
    android:startOffset="500" 
    android:duration="500" />

Then use an ImageSwitcher widget to switch between the views:

@Override
public void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    LinearLayout ll = new LinearLayout( this );
    ll.setOrientation( LinearLayout.VERTICAL );
    setContentView( ll );
    final ImageSwitcher is = new ImageSwitcher( this );
    is.setOutAnimation( this, R.anim.fadeout );
    is.setInAnimation( this, R.anim.fadein );
    ImageView iv1 = new ImageView( this );
    iv1.setImageResource( R.drawable.icon );
    is.addView( iv1 );
    is.showNext();
    ll.addView( is );

    Button b = new Button( this );
    ll.addView( b );

    b.setOnClickListener( new OnClickListener()
    {

        @Override
        public void onClick( View v )
        {
            ImageView iv2 = new ImageView( MainActivity.this );
            iv2.setImageResource( R.drawable.icon2 );
            is.addView( iv2 );
            is.showNext();
        }
    });
}

There is a series of articles on my blog about tweened animations.

Upvotes: 3

Related Questions