Transition Drawable with more than 2 layers

3.1k Views Asked by At

Can there be more than 2 items in transition drawable? I need to change background so second frames fades in than on top of it third does and so on to fourth...

for now I have this:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/voting_button_not1"/>
    <item android:drawable="@drawable/voting_button_not2"/>
    <item android:drawable="@drawable/voting_button_not3"/>
    <item android:drawable="@drawable/voting_button_not4"/>
    <item android:drawable="@drawable/voting_button_not5"/>
    <item android:drawable="@drawable/voting_button_not1"/>
</transition>

And I got the button:

<ImageButton android:id="@+id/skipButton"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/coldf1f2"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>

P.S. never mind that its an ImageButton, that doesn't make any difference.

And in my code I got smth like this:

TransitionDrawable vote_not = (TransitionDrawable)skip.getBackground();
vote_not.startTransition(1000);

It plays transition from first item to second. But I need the full list be played.

4

There are 4 best solutions below

0
Boris Strandjev On

It seems like TransitionDrawable is meant to operate only with two layers. Taken from the Android documentation for the class:

An extension of LayerDrawables that is intended to cross-fade between the first and second layer.

I think you can specify more than two layers, because this is extension of layered drawable, but in the case of the TransitionDrawable actually only the first two are used.

0
dreid On
0
Om Prakash Agrahari On

you can try this option using a handler

mAnimateImage is a imageView and DrawableImage is an array with drawables

int DrawableImage[] = {R.drawable.back_red , R.drawable.back_green, R.drawable.back_purple};

final Handler handler = new Handler();
final int[] i = {0};
final int[] j = {1};
handler.postDelayed(new Runnable() {
    @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Resources res = getApplicationContext().getResources();
                    TransitionDrawable out = new TransitionDrawable(new Drawable[]{res.getDrawable(DrawableImage[i[0]]), res.getDrawable(DrawableImage[j[0]])});
                    out.setCrossFadeEnabled(true);
                    mAnimateImage.setImageDrawable(out);
                    out.startTransition(4000);
                    i[0]++;
                    j[0]++;
                    if (j[0] == DrawableImage.length) {
                        j[0] = 0;
                    }
                    if (i[0] == DrawableImage.length) {
                        i[0] = 0;
                    }
                    handler.postDelayed(this, 8000);
                }
            });
        }
    }, 0);
0
aaronvargas On

You can do this with a combination of using a Handler and re-applying the TransitionDrawable for the elements of the array.

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