Android动画:如何制作一个animation-list动画

6

我的问题是,是否可以在动画列表中对一个项目进行动画处理。具体来说,假设您有以下内容:

<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>

我想淡化每个<item>的透明度,而不是简单地从一张图像跳到下一张,这可能吗?

1个回答

3

您需要使用缓动动画来完成这个任务。基本上,您需要有两个ImageView对象,一个用于当前图像,另一个用于新图像。为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" />

以及 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" />

然后使用ImageSwitcher小部件在视图之间进行切换:
@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();
        }
    });
}

我在我的博客上写了一系列关于缓动动画的文章。


我不太确定这是否是我要找的。我正在尝试在应用程序下载数据时播放闪屏动画,以便我不仅有静态图像。动画至少包含4个图像,并且动画应该自动发生--没有用户交互。 - tomislav2012
我使用一个按钮来保持代码的简洁。你可以使用带有后台线程的ImageSwitcher定期更改图像。 - Mark Allison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接