4

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.

Graykos
  • 1,281
  • 3
  • 20
  • 31

4 Answers4

2

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.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
0

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);
Kamil
  • 13,363
  • 24
  • 88
  • 183
0

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

aaronvargas
  • 12,189
  • 3
  • 52
  • 52
-1

Sounds like you might want AnimationDrawable:

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

dreid
  • 397
  • 2
  • 9