Animating Drawable change in a Button

删除回忆录丶 提交于 2019-12-13 00:24:41

问题


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 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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!