Mazze
Mazze

Reputation: 1404

Animating Drawable change in a Button

I have a Button that has a BitmapDrawable and a row of text in it. Not that it matter but my drawable is "above" my text which would correspond to the third button from the top.

Examples of using setCompundDrawablesWithIntrisicBounds

The drawable is set using:

BitmapDrawable top = new BitmapDrawable(mContext.getResources(), Bitmap.createScaledBitmap(buttonContent.content, 
            mContext.IMAGE_WIDTH, mContext.IMAGE_HEIGHT, false));

    button.setCompoundDrawablesWithIntrinsicBounds(null, top.getCurrent(), null, null);

The image that I set initially changes after some events and I want the image change to be animated. I already have the android:animateLayoutChanges=true field in my layout.

My initial guess is to create a custom Button-class. But I'm wondering if there's another way to resolve my issue?

Upvotes: 1

Views: 11160

Answers (2)

dst
dst

Reputation: 3337

You can animate the drawable itself. A TransitionDrawable allows to cross-fade between layers (setCrossFadeEnabled(true)), you can start the transition by something like

((TransitionDrawable) button.getCompoundDrawables()[INDEX]).startTransition(MILLIS);

Old answer based on Animatables: See the "Drawable Animation" documentation. You can use that technique for compound drawables as well.

Start the animation using something like

((Animatable) button.getCompoundDrawables()[INDEX]).start();

or hold a reference to the Animatable when not setting the drawable from XML.

Upvotes: 1

Mazze
Mazze

Reputation: 1404

The answer "dst" gave is correct. But if you're looking for a cross-fade animation between multiple images and don't want to use the Animator-classes ,the TransitionDrawable isn't gonna cut it. It's only for two images.

There's an answer to an almost similar post. Check it out!

Upvotes: 1

Related Questions