Using this code I only have a fade in, I'm looking for how to add a fade out as well. I've added another xml called "fadeout" but I can't integrate it in my code.
ImageView imageView = (ImageView)findViewById(R.id.imageView); Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein); imageView.startAnimation(fadeInAnimation); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { imageView.startAnimation(fadeInAnimation); } }
fadein.xml
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="Your Duration(in milisecond)" android:repeatCount="infinite"/> </set>
In android, Fade In and Fade Out animations are used to change the appearance and behavior of the objects over a particular interval of time. The Fade In and Fade Out animations will provide a better look and feel for our applications.
A fade-in effect begins with a solid color (Movie Maker supports both black and white) and then fades into your video. A fade-out effect begins with the video and then fades into the solid color, again either black or white.
Here is my solution. It uses AnimatorSet. AnimationSet library was too buggy to get working. This provides seamless and infinite transitions between fade in and out.
public static void setAlphaAnimation(View v) { ObjectAnimator fadeOut = ObjectAnimator.ofFloat(v, "alpha", 1f, .3f); fadeOut.setDuration(2000); ObjectAnimator fadeIn = ObjectAnimator.ofFloat(v, "alpha", .3f, 1f); fadeIn.setDuration(2000); final AnimatorSet mAnimationSet = new AnimatorSet(); mAnimationSet.play(fadeIn).after(fadeOut); mAnimationSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); mAnimationSet.start(); } }); mAnimationSet.start(); }
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