Android过渡效果

3
我使用以下代码来设置我的启动屏幕(SplashScreen)中两个图像之间的动画:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

     // Show A Transitions for Splash image here.
    TransitionDrawable transition = (TransitionDrawable) getResources()
            .getDrawable(R.drawable.splash_animation);

    //Set interval for the transition between two image.
    transition.startTransition(5000);

    //Fetch imageView from your layout and apply transition on the same.

    ImageView imageView= (ImageView) findViewById(R.id.splash_image);
    imageView.setImageDrawable(transition);
}

我的splash.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:scaleType="fitXY"
        android:id="@+id/splash_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/img_1" />
</RelativeLayout>

我的 splash_animation.xml 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/img_1"></item>
  <item android:drawable="@drawable/img_2"></item>
</transition>

转换效果很好,但我想知道是否可以将其应用于3张图片。我尝试在启动动画中添加第三张图片,但转换只对第一张图片执行。我该如何实现我想要的任意数量的图片?
2个回答

1
TransitionDrawable 数组放置在代码中。
List<TransitionDrawable>array = new ArrayList<TransitionDrawable>();

TransitionDrawable transition1 = (TransitionDrawable) getResources()
            .getDrawable(R.drawable.splash_animation1); // first,second image

TransitionDrawable transition2 = (TransitionDrawable) getResources()
            .getDrawable(R.drawable.splash_animation2); // third,fourth image

array.add(transition1);
array.add(transition2);
// call array
for(TransitionDrawable transition :array){
transition.start(5000);
}
 ImageView imageView= (ImageView) findViewById(R.id.splash_image);
 imageView.setImageDrawable(transition[0]); // if transition[0] is finished  setImageDrawable(transition[1]);

对于数组中的每个交易,执行transition.startTransition(5000)。 - Yahor10

0

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