Let's say I have an ImageButton btn
. I have two different Bitmap
, bmp1
and bmp2
. By default, btn
displays bmp1
, like this:
btn.setImageBitmap(bmp1);
Can I somehow make an animation, which crossfades bmp1
and bmp2
, when calling the following:
btn.setImageBitmap(bmp2);
So the question is, is it possible - if yes, how - to animate the changing of bitmaps on ImageButtons?
The way I see it, there are two main ways to implement this functionality. Note that there may be other methods that will make this happen exactly as you are describing, but these would be my approaches.
onAnimationEnd()
CallbackHere, you would want to essentially fade out the first ImageButton
, change the resource in the onAnimationEnd()
callback, and then fade it back in. To do this, you would have to implement the below code.
Create an Animation
resource in XML that would be used to fade the View
in:
<!-- Filename: fadein.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="250"/>
</set>
Create an Animation
resource in XML that would be used to fade the View
out:
<!-- Filename: fadeout.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="250"/>
</set>
Load the fade out and fade in Animation
s in your code:
// Obtain a reference to the Activity Context
Context context = getContext();
// Create the Animation objects.
Animation outAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeout);
Animation inAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
Assign a new AnimationListener
to this Animation
, and build it out as follows:
outAnimation.setAnimationListener(new AnimationListener(){
// Other callback methods omitted for clarity.
public void onAnimationEnd(Animation animation){
// Modify the resource of the ImageButton
yourImageButton.setImageResource(R.drawable.[your_second_image]);
// Create the new Animation to apply to the ImageButton.
yourImageButton.startAnimation(inAnimation);
}
}
Assign the Animation
to the ImageButton
, and start the first Animation
:
yourImageButton.startAnimation(outAnimation);
Then, if you wish to animate back to the previous image, you would simply do the same but in the reverse order.
ImageButton
For this approach, you would simply assign two ImageButton
s to the same coordinates on the screen, and with the same width. Then, you would fade out the first ImageButton
and fade in the second one after the first Animation
ended (much like the example above).
I hope that this answer aligns with your expectations. My apologies if there are any code errors, as I am doing this without an editor at the moment. Please let me know if you would like any additional clarification!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With