问题
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.
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?
回答1:
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 Animatable
s: 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.
回答2:
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!
来源:https://stackoverflow.com/questions/18234613/animating-drawable-change-in-a-button