TextView animation - fade in, wait, fade out

余生长醉 提交于 2019-12-31 18:59:22

问题


I am making a picture gallery app. I current have a imageview with a text view at the bottom. Currently it is just semitransparent. I want to make it fade in, wait 3 second, then fade out 90%. Bringing focus to it or loading a new pic will make it repeat the cycle. I have read thru a dozen pages and have tried a few things, no success. All I get is a fade in and instant fade out


回答1:


You can use an extra animation object (which doesn't modify its alpha) to prevent the instant fade out, set animationListener for your fade-in effect and start the extra animation object in the on animationEnd of the fade-in, then you start fade-out on animation end of the extra animation object, try the link below, it'll help..

Auto fade-effect for textview




回答2:


protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; 
protected AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ; 
txtView.startAnimation(fadeIn);
txtView.startAnimation(fadeOut);
fadeIn.setDuration(1200);
fadeIn.setFillAfter(true);
fadeOut.setDuration(1200);
fadeOut.setFillAfter(true);
fadeOut.setStartOffset(4200+fadeIn.getStartOffset());

Works perfectly for white backgrounds. Otherwise, you need to switch values when you instantiate AlphaAnimation class. Like this:

AlphaAnimation fadeIn = new AlphaAnimation( 1.0f , 0.0f ); 
AlphaAnimation fadeOut = new AlphaAnimation(0.0f , 1.0f ); 

This works with black background and white text color.




回答3:


That's the solution that I've used in my project for looping fade-in/fade-out animation on TextViews:

private void setUpFadeAnimation(final TextView textView) {
    // Start from 0.1f if you desire 90% fade animation
    final Animation fadeIn = new AlphaAnimation(0.0f, 1.0f);
    fadeIn.setDuration(1000);
    fadeIn.setStartOffset(3000);
    // End to 0.1f if you desire 90% fade animation
    final Animation fadeOut = new AlphaAnimation(1.0f, 0.0f);
    fadeOut.setDuration(1000);
    fadeOut.setStartOffset(3000);

    fadeIn.setAnimationListener(new Animation.AnimationListener(){
        @Override
        public void onAnimationEnd(Animation arg0) {
            // start fadeOut when fadeIn ends (continue)
            textView.startAnimation(fadeOut);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
        }

        @Override
        public void onAnimationStart(Animation arg0) {
        }
    });

    fadeOut.setAnimationListener(new Animation.AnimationListener(){
        @Override
        public void onAnimationEnd(Animation arg0) {
            // start fadeIn when fadeOut ends (repeat)
            textView.startAnimation(fadeIn);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
        }

        @Override
        public void onAnimationStart(Animation arg0) {
        }
    });

    textView.startAnimation(fadeOut);
}

Hope this could help!




回答4:


Kotlin Extension functions for Guy Cothal's answer:

inline fun View.fadeIn(durationMillis: Long = 250) {
    this.startAnimation(AlphaAnimation(0F, 1F).apply {
        duration = durationMillis
        fillAfter = true
    })
}

inline fun View.fadeOut(durationMillis: Long = 250) {
    this.startAnimation(AlphaAnimation(1F, 0F).apply {
        duration = durationMillis
        fillAfter = true
    })
}


来源:https://stackoverflow.com/questions/11444051/textview-animation-fade-in-wait-fade-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!