Android TextView 动画 - 淡入、等待、淡出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11444051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:26:01  来源:igfitidea点击:

TextView animation - fade in, wait, fade out

androidandroid-animationtextviewfadeinfadeout

提问by Guy Cothal

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

我正在制作一个图片库应用程序。我目前有一个底部带有文本视图的图像视图。目前它只是半透明的。我想让它淡入,等待 3 秒,然后淡出 90%。将焦点放在它或加载新图片将使其重复循环。我已经通读了十几页并尝试了一些东西,但没有成功。我得到的只是淡入和即时淡出

采纳答案by kunmi

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..

您可以使用额外的动画对象(不会修改其 alpha)来防止即时淡出,为您的淡入效果设置 animationListener 并在淡入的 on animationEnd 中启动额外的动画对象,然后开始在额外动画对象的动画结束时淡出,试试下面的链接,它会有所帮助..

Auto fade-effect for textview

textview 的自动淡入淡出效果

回答by Guy Cothal

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 AlphaAnimationclass. Like this:

非常适合白色背景。否则,您需要在实例化AlphaAnimation类时切换值。像这样:

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.

这适用于黑色背景和白色文本颜色。

回答by Roses

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

这是我在我的项目中用于在 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!

希望这会有所帮助!

回答by Bassam Helal

Kotlin Extension functions for Guy Cothal's answer:

Guy Cothal 的回答的 Kotlin 扩展函数:

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
    })
}

回答by galloper

I was searching for a solution to similar problem (TextView fade in/wait/fade out) and came up with this one (in fact, the official docs point to this too). You can obviously improve this by adding more params.

我正在寻找类似问题的解决方案(TextView 淡入/等待/淡出)并提出了这个(事实上,官方文档也指出了这一点)。您显然可以通过添加更多参数来改善这一点。

public void showFadingText(String text){
    txtView.setText(text);
    Runnable endAction;
    txtView.animate().alpha(1f).setDuration(1000).setStartDelay(0).withEndAction(
            endAction = new Runnable() {
                public void run() {
                    txtView.animate().alpha(0f).setDuration(1000).setStartDelay(2000);
                }
            }
    );
}

Provided you have txtView declared somewhere else with alpha starting at zero.

假设您在其他地方声明了 txtView,alpha 从零开始。