Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create animation in Android like this Animation example[^]. I am using fade-in fade-out effects and PNG images saved in R drawable folder. All the images have the same dimensions. Therefore, I want to achieve the animation like the one shown in the above web URL.

The problem is fade-in and fade-out effects are quite prominent, it's hard to see what's happening in the changing pictures. How can I make simultaneous fade-in fade-out effects so that the fading effect is not prominent, and I can achieve animation effect shown in the above web URL?

For animation, I am using images stored from drawable resources. Can one also load images directly from web URL and animate?

What I have tried:

Here is the method that animates the images. For the time being, I am using images stored in drawable resource folder.

private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever){
    
//imageView: ImageView in XML to show the animation
//final int images[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3,
// R.drawable.image4, R.drawable.image5,..}; Several images!
            //imageIndex: index of the first image to show in images[]
            //forever: boolean to run animation in infinite loop
    		
            int fadeInDuration = 100;
            int timeBetween = 100;
            int fadeOutDuration = 100;
    
            imageView.setVisibility(View.INVISIBLE); 
            imageView.setImageResource(images[imageIndex]);
    
            Animation fadeIn = new AlphaAnimation(0, 1);
            fadeIn.setInterpolator(new DecelerateInterpolator()); 
            fadeIn.setDuration(fadeInDuration);
    
            Animation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setInterpolator(new AccelerateInterpolator());
            fadeOut.setStartOffset(fadeInDuration + timeBetween);
            fadeOut.setDuration(fadeOutDuration);
    
            AnimationSet animation = new AnimationSet(false);
            animation.addAnimation(fadeIn);
            animation.addAnimation(fadeOut);
            animation.setRepeatCount(1);
            imageView.setAnimation(animation);
    
            animation.setAnimationListener(new Animation.AnimationListener() {
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    if(images.length -1 > imageIndex){
                        animate(imageView, images, imageIndex + 1, forever); // Calls itself until it gets to the end of the array.
                    }
                    else{
                        if(forever == false){
                            animate(imageView, images, 0, forever); //Calls itself to start the animation all over again in a loop if forever = true
                        }
                    }
    
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
            });
        }
Posted
Comments
[no name] 10-Jan-22 4:39am    
"Prominent" doesn't mean much to a non-observer. You have durations you can play it. You can also try to vary the Opacity of the image and / or an overlay panel. You could vary a border.
Naive Pro 10-Jan-22 5:10am    
@Gerry: By "prominent", I meant, I couldn't reach the animation (please see example web link in the question) where fading in and out is "almost" invisible or not noticeable. I tried playing with durations, but still one image fading out and the other fading in is visible/noticeable. By "overlay panel", perhaps you meant using two overlapping imageViews? I couldn't figure out how to do that. Care to drop some suggestions?
[no name] 10-Jan-22 11:33am    
The overlay "panel" would be an object like a Rectangle (for example) that allows for a transparent background. By adding color (or black) to the background of the panel and varying the opacity, you get a fade in / fade out. The panel can be collapsed or added on the fly as needed if you need to interact with what's behind any overlay. I used a "sliding" panel in a kiosk app. (customer request)

https://stackoverflow.com/questions/5646944/how-to-set-shapes-opacity
Naive Pro 10-Jan-22 12:41pm    
Again, thanks a lot for the suggestion :) The link that you posted doesn't deal with the animation. Would you help to me understand your suggestion by posting a proposed solution instead?
[no name] 10-Jan-22 17:36pm    
https://stackoverflow.com/questions/20628973/android-animation-alpha

https://www.tutlane.com/tutorial/android/android-fade-in-out-animations-with-examples

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900