android过渡

时间:2012-11-08 07:12:43

标签: android android-animation android-image

我正在使用以下代码为我的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张图片创建它。我尝试在splash_animation中添加第三个图像,但只对第一个图像进行了转换。如何在我想要的图像中实现它?

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分:0)

您可以对任意数量的图像进行此操作。

您需要使用数组中的当前项和下一项重新创建TransitionDrawable并对其进行更新。

https://stackoverflow.com/a/54584103/114549上查看我的答案