android transition

瘦欲@ 提交于 2020-01-13 18:15:09

问题


I am using the following code to set an animation between 2 images for my 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);
}

My splash.xml is :

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

My splash_animation.xml file is :

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

The transition is working fine but I wanted to know if it is possible to create it for 3 images as well. I tried adding a third image in splash_animation but the transition is only done for the first image. How can I achieve it for as many images as I want?


回答1:


Put the array of the 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]);



回答2:


You can do this with any number of images.

You'll need to re-create the TransitionDrawable with the current and next item in the array and update it.

See my answer at https://stackoverflow.com/a/54584103/114549



来源:https://stackoverflow.com/questions/13284221/android-transition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!