This example displays a transaction for an image view with only two images.(can use more images as well one after the other for the first and second layer positions after each transaction as a loop)
privateDrawable[] backgroundsDrawableArrayForTransition;privateTransitionDrawable transitionDrawable;privatevoidbackgroundAnimTransAction(){// set res image arrayResources resources =getResources();TypedArray icons = resources.obtainTypedArray(R.array.splash_images);@SuppressWarnings("ResourceType")Drawable drawable = icons.getDrawable(0);// ending image@SuppressWarnings("ResourceType")Drawable drawableTwo = icons.getDrawable(1);// starting image
backgroundsDrawableArrayForTransition =newDrawable[2];
backgroundsDrawableArrayForTransition[0]= drawable;
backgroundsDrawableArrayForTransition[1]= drawableTwo;
transitionDrawable =newTransitionDrawable(backgroundsDrawableArrayForTransition);// your image view here - backgroundImageView
backgroundImageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(4000);
transitionDrawable.setCrossFadeEnabled(false);// call public methods }
Fade in/out animation
In order to get a view to slowly fade in or out of view, use an ObjectAnimator. As seen in the code below, set a duration using .setDuration(millis) where the millis parameter is the duration (in milliseconds) of the animation. In the below code, the views will fade in / out over 500 milliseconds, or 1/2 second. To start the ObjectAnimator's animation, call .start(). Once the animation is complete, onAnimationEnd(Animator animation) is called. Here is a good place to set your view's visibility to View.GONE or View.VISIBLE.
importandroid.animation.Animator;importandroid.animation.AnimatorListenerAdapter;importandroid.animation.ValueAnimator;voidfadeOutAnimation(View viewToFadeOut){ObjectAnimator fadeOut =ObjectAnimator.ofFloat(viewToFadeOut,"alpha",1f,0f);
fadeOut.setDuration(500);
fadeOut.addListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationEnd(Animator animation){// We wanna set the view to GONE, after it's fade out. so it actually disappear from the layout & don't take up space.
viewToFadeOut.setVisibility(View.GONE);}});
fadeOut.start();}voidfadeInAnimation(View viewToFadeIn){ObjectAnimator fadeIn =ObjectAnimator.ofFloat(viewToFadeIn,"alpha",0f,1f);
fadeIn.setDuration(500);
fadeIn.addListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationStar(Animator animation){// We wanna set the view to VISIBLE, but with alpha 0. So it appear invisible in the layout.
viewToFadeIn.setVisibility(View.VISIBLE);
viewToFadeIn.setAlpha(0);}});
fadeIn.start();}
ValueAnimator
ValueAnimator introduces a simple way to animate a value (of a particular type, e.g. int, float, etc.).
The usual way of using it is:
Create a ValueAnimator that will animate a value from min to max
ValueAnimator animator =(ValueAnimator)AnimatorInflater.loadAnimator(context,R.animator.example_animator);
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){@OverridepublicvoidonAnimationUpdate(ValueAnimator anim){// ... use the anim.getAnimatedValue()}});// set all the other animation-related stuff you want (interpolator etc.)
animator.start();
From the code:
ValueAnimator animator =ValueAnimator.ofFloat(20f,40f);
animator.setDuration(250);
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){@OverridepublicvoidonAnimationUpdate(ValueAnimator anim){// use the anim.getAnimatedValue()}});// set all the other animation-related stuff you want (interpolator etc.)
animator.start();
ObjectAnimator animator =(ObjectAnimator)AnimatorInflater.loadAnimator(context,R.animator.example_animator);
animator.setTarget(exampleView);// set all the animation-related stuff you want (interpolator etc.)
animator.start();
From code:
ObjectAnimator animator =ObjectAnimator.ofFloat(exampleView,View.ALPHA,0.4f,0.2f);
animator.setDuration(250);// set all the animation-related stuff you want (interpolator etc.)
animator.start();
Every single View has a ViewPropertyAnimator object available through the animate()(opens new window) method. You can use that to animate multiple properties at once with a simple call. Every single method of a ViewPropertyAnimator specifies the target value of a specific parameter that the ViewPropertyAnimator should animate to.
Note: Calling start() on a ViewPropertyAnimator object is NOT mandatory. If you don't do that you're just letting the platform to handle the starting of the animation in the appropriate time (next animation handling pass). If you actually do that (call start()) you're making sure the animation is started immediately.