Android: Fade view in and out

AndroidImageFade

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

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();
       }
     });

Solution 3 - Android

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

Solution 4 - Android

in kotlin:

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

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

Solution 5 - Android

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);

Solution 6 - Android

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDominicMView Question on Stackoverflow
Solution 1 - AndroidDominicMView Answer on Stackoverflow
Solution 2 - AndroidDavid MaesView Answer on Stackoverflow
Solution 3 - AndroidMario KutlevView Answer on Stackoverflow
Solution 4 - AndroidAbhilash DasView Answer on Stackoverflow
Solution 5 - AndroidmshView Answer on Stackoverflow
Solution 6 - AndroidMunish KatochView Answer on Stackoverflow