Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation fade in and out

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> 
like image 485
Pol Hallen Avatar asked Jan 04 '13 12:01

Pol Hallen


People also ask

What is fadeIn and fadeOut in animation?

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.

What is the difference between fadeIn and fadeOut?

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.


1 Answers

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(); } 
like image 80
TWilly Avatar answered Oct 19 '22 03:10

TWilly