Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 901
tomislav2012 Avatar asked Jun 03 '11 06:06

tomislav2012


People also ask

Can I make animation on android?

With over a million downloads, Animation Desk is one of the best animation apps for Android that you can try. It has all the things that make an animation app great—a clutter-free user interface, an adequate amount of options, and a handful of ways to export the finished animation.

Which statement should be placed in an animation list to make an animation play once and then stop?

You might notice the android:oneshot=”true” in both of these code snippets, which is simply an attribute of the animation-list for playing the animation once and then stopping. If this is set to “false,” the animation will play on repeat.

What is pivotX and pivotY in android?

The pivotX and pivotY is the central point of the animation. So for example if you want to do Zoom In animation you can use this <? xml version="1.0" encoding="utf-8"?> <


1 Answers

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.

like image 158
Mark Allison Avatar answered Oct 19 '22 21:10

Mark Allison