Skip to content

Instantly share code, notes, and snippets.

@rhonyabdullah
Last active March 21, 2016 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhonyabdullah/72b9b5b8af7ad9727593 to your computer and use it in GitHub Desktop.
Save rhonyabdullah/72b9b5b8af7ad9727593 to your computer and use it in GitHub Desktop.
Android Image Animation Fade IN/OUT method
private void animateFadeInOut() {
/*
bgImageView.setImageBitmap(
BitmapFactory.decodeResource(this.getResources(), R.drawable.bgimage)
);
*/
bgImageView.setColorFilter(Color.argb(255, 255, 255, 255));
bgImageView.setScaleType(ImageView.ScaleType.CENTER);
bgImageView.setAlpha(0.7f);
long fadeInDuration = 3000; // Configure time values here
int timeBetween = 950;
long fadeOutDuration = 3000;
//Visible or invisible by default - this will apply when the animator ends
bgImageView.setVisibility(View.INVISIBLE);
Animation fadeIn = new AlphaAnimation(0.0f, 0.7f);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(0.7f, 0.0f);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false); // change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(Animation.INFINITE);
bgImageView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
//Calls itself to start the animator all over again in a loop if forever = true
animateFadeInOut();
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationStart(Animation animation) {}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment