56

I need to display an image button that fades in and out (and in and out and so on...) The transparency can be set with setAlpha but how can I fade in and out? I mean I cannot do it on another thread because you need to do such things on the UI thread, right?

I guess it can be done with animations but I haven't found anything, because I don't have any experience with animations and don't really know what to search for...

Actually what I really want is to fade one image in and another one out but I guess the easiest way is to place the first image button below the second and just fade the second one. Or is there an easier way to do it?

DominicM
  • 2,186
  • 5
  • 24
  • 42
  • You could use an animation, and create an animation `XML` file with your alpha transition, then set it to the `ImageButton` – kabuto178 May 28 '13 at 20:18

6 Answers6

94

Here is the solution I'm using now, that works on API level lower than 12:

AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(1000);
anim.setRepeatCount(NUM_REPEATS);
anim.setRepeatMode(Animation.REVERSE);
button.startAnimation(anim);
DominicM
  • 2,186
  • 5
  • 24
  • 42
28

This is an animation we used in our project. Spinner is a view so you can change this with your imageview. So indeed 2 images on top of eachother, one visible one invisible. This is how we did it. Hope it helps.

    spinner.setVisibility(View.VISIBLE);
    spinner.setAlpha(0);

    spinner.animate().setDuration(200).alpha(1).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            spinner.setVisibility(View.VISIBLE);
        }
    });

     infoActivityContent.animate().setDuration(200).alpha(0).setListener(new AnimatorListenerAdapter() {
       @Override
       public void onAnimationEnd(Animator animation) {
            infoActivityContent.setVisibility(View.GONE);

       mainPresenter.logout();
       }
     });
David Maes
  • 574
  • 6
  • 23
  • this also needs API level 12 and I need 8. Anyway, I'll use it until I find something else. If I want to fade in and out infinitely (meaning that I need to start the fade-in-animation from within the fade-out-animation-end-listener) will it eventually result in a memory overflow? – DominicM May 28 '13 at 21:36
  • 2
    No that normally shouldn't be a problem. They are all running on different threads and once the first one finishes all the memory it allocated will be freed. – David Maes May 29 '13 at 09:38
6

You have to read Crossfading Two Views from Android developers. In this tutorial is explained how to do what you want.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
  • 1
    this looks very good but it requires API level 12 and I need level 8 (I forgot to mention that...) – DominicM May 28 '13 at 21:23
4

in kotlin:

view.animate().alpha(1f).setDuration(1000)
            .setInterpolator(AccelerateInterpolator()).start()

You can add an AnimatorListenerAdapter in setListener to handle other view states.

Abhilash Das
  • 1,388
  • 1
  • 16
  • 22
0

You can make several sequential frames of your first image morphing into your second image and back, then define them as animation-list and start animation in onCreate

button_frames.xml:

   <?xml version="1.0" encoding="utf-8"?>
      <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/frame1" android:duration="100" />
        <item android:drawable="@drawable/frame2" android:duration="100" />
                   ....

layout:

  <ImageView android:id="@+id/button" 
       android:background="@drawable/button_frames"/>

OnCreate:

   ImageView button= (ImageView)findViewById(R.id.button);
   mAnimation = (AnimationDrawable) animationView.getBackground();
   button.postDelayed(new Runnable() {
       public void run() {
         mAnimation.start();
       }
   }, 100);
msh
  • 2,700
  • 1
  • 17
  • 23
-1

Implement Animation Class ( You can load it thru XML Or create it dynamically).

Then you can set it thru API setAnimation(Animation animation).

Munish Katoch
  • 517
  • 3
  • 6