Making a smooth fade out for imageview in android

后端 未结 2 791
既然无缘
既然无缘 2020-12-25 14:57

I managed to make the imageView dissapear after 10 seconds(a tutorial image on my mainActivity). But i want to make a smooth fade out because like this it doesn`t look good,

相关标签:
2条回答
  • 2020-12-25 15:16

    take hint from this code snippet. The code required goes like this-

    Animation fadeOut = new AlphaAnimation(1, 0);  // the 1, 0 here notifies that we want the opacity to go from opaque (1) to transparent (0)
    fadeOut.setInterpolator(new AccelerateInterpolator());
    fadeOut.setStartOffset(500); // Start fading out after 500 milli seconds
    fadeOut.setDuration(1000); // Fadeout duration should be 1000 milli seconds
    

    Now set this to an element say an image view -

    myImageView.setAnimation(fadeOut);
    
    0 讨论(0)
  • 2020-12-25 15:29

    Replace img.setVisibility(View.GONE) in your code with a call to fadeOutAndHideImage(img) which is defined like this:

      private void fadeOutAndHideImage(final ImageView img)
      {
        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setDuration(1000);
    
        fadeOut.setAnimationListener(new AnimationListener()
        {
                public void onAnimationEnd(Animation animation) 
                {
                      img.setVisibility(View.GONE);
                }
                public void onAnimationRepeat(Animation animation) {}
                public void onAnimationStart(Animation animation) {}
        });
    
        img.startAnimation(fadeOut);
    }
    

    It will apply the fade out animation first, then will hide the image view.

    0 讨论(0)
提交回复
热议问题