I have two Images that I want to cross fade. Initially they both use imageview. I then use .getDrawable() to get the drawable of the images.
This is the code I used
Drawable backgrounds[] = new Drawable[2];
backgrounds[0] = BackgroundImage.getDrawable();
backgrounds[1] = BackgroundImageBlurred.getDrawable();
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
crossfader.startTransition(3000);
It only shows the image on the first array element, which it shows anyway since both images were set to visible in the XML.
The transition doesn't start
Any help would be appreciated :)
Here's an example IF you have 2 drawables and want to animate their transition in some
ImageView
:Then if you want to transition back to the original image you can call
Please correct me if I misunderstood your question.
A
TransitionDrawable
is just a special kind of drawable. It is not drawn by itself, it must be placed somewhere (e.g. on a View background, on an ImageView, &c) to be displayed.If you're trying to crossfade two images, you have two possible approaches:
TransitionDrawable
.Maybe you like this one
- res/drawable/transition.xml:
ImageView:
Javacode:
If it is helpfull, you can see Google-Dev:
http://developer.android.com/guide/topics/resources/drawable-resource.html
You should use
setCrossFadeEnabled
properyIf you are using Vectors, your VectorDrawables won't be cross-faded in the TransitionDrawable.
You'll need to convert them into BitmapDrawables first.
See my answer here https://stackoverflow.com/a/54583929/114549 for an example