Android alpha 动画淡入淡出带延迟

发布于 2024-09-10 22:30:05 字数 896 浏览 3 评论 0原文

我想做一个非常简单的阿尔法动画,但我找不到有效的方法。

这个想法是在视图上执行此动画:

  1. 从 0 到 1 的 1 秒的 alpha
  2. 将 alpha 保持在 1 5 秒,
  3. 从 1 到 0 的 1 秒
  4. 将 alpha 保持在 0 5 秒。
  5. 从 1 重新开始。

我尝试使用 AnimationSet 来实现它:

AnimationSet animationSet = new AnimationSet(true);

Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animation1.setDuration(1000);

Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);

Animation animation3 = new AlphaAnimation(0.0f, 0.0f);
animation3.setDuration(4000)
animation3.setStartOffset(6000);

animationSet.add(animation1);
animationSet.add(animation2);
animationSet.add(animation3);

等等。

但是第三个动画似乎弄乱了所有的 alpha 动画,我认为这会导致 Android 管理方式的内部不连贯这种类型的动画。

有什么想法吗?

谢谢。

I want to do a very simple alpha animation but I cannot find a valid way.

The idea is to perform this animation over a view:

  1. alpha from 0 to 1 of 1 second
  2. hold alpha at 1 for 5 seconds
  3. alpha from 1 to 0 of 1 second
  4. hold alpha at 0 for 5 seconds.
  5. start again on 1.

I've tried to implement that with an AnimationSet as:

AnimationSet animationSet = new AnimationSet(true);

Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animation1.setDuration(1000);

Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);

Animation animation3 = new AlphaAnimation(0.0f, 0.0f);
animation3.setDuration(4000)
animation3.setStartOffset(6000);

animationSet.add(animation1);
animationSet.add(animation2);
animationSet.add(animation3);

etc..

but it seams that the third animation do a mess with all the alpha animations, I supose that this cause an internal incoherence in the way that Android manage this type of animation.

Any idea?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜无邪 2024-09-17 22:30:05

好的,记住这两点来解决这个问题


  • 如果我想在 5 秒后将 1.0f 变为 0.0f,动画持续时间为 1 秒,这最终是一个 1 秒的动画,暂停 5 秒。

    要实现这一点:

    1. setDuration(1000)(持续时间为 1 秒)
    2. setStartOffset(5000)(5秒后开始)

  • 您只需要 2 个动画这将永远循环。

    1.0.0f 至 1.0f,暂停 5 秒,持续时间 1 秒

    2.1.0f 到 0.0f,暂停 5 秒,持续时间 1 秒


代码如下:

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    textView.startAnimation(animation1);

然而,为了永远循环,我将使用AnimationListener,因为repeatCount有问题:

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    //animation1 AnimationListener
    animation1.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation2 when animation1 ends (continue)
            textView.startAnimation(animation2);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    //animation2 AnimationListener
    animation2.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation1 when animation2 ends (repeat)
            textView.startAnimation(animation1);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    textView.startAnimation(animation1);

Ok Keep in mind these 2 points to solve this


  • If I want to animate 1.0f to 0.0f after 5 seconds with an animation duration of 1 seconds, this is ultimately a 1 second animation with a pause of 5 seconds.

    To acheive this:

    1. setDuration(1000) (it has a 1 second duration)
    2. setStartOffset(5000) (it will start after 5 seconds)

  • You only need 2 animations that will loop forever.

    1.0.0f to 1.0f with 5 seconds pause and 1 second duration

    2.1.0f to 0.0f with 5 seconds pause and 1 second duration


And here is the code:

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    textView.startAnimation(animation1);

However to loop forever I will use AnimationListener because repeatCount is buggy:

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    //animation1 AnimationListener
    animation1.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation2 when animation1 ends (continue)
            textView.startAnimation(animation2);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    //animation2 AnimationListener
    animation2.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation1 when animation2 ends (repeat)
            textView.startAnimation(animation1);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    textView.startAnimation(animation1);
慈悲佛祖 2024-09-17 22:30:05

对此有一个更简单的解决方案。

假设您的视图处于 GONE 状态。要为其可见性设置动画:

yourView.setVisibility(View.VISIBLE);
yourView.animate().alpha(1).setDuration(300);

通过相同的方式您可以添加动画侦听器。

这也适用于缩放和平移动画。

There is a simpler solution to this.

Lets assume that your view is in state GONE. To animate to its visibility:

yourView.setVisibility(View.VISIBLE);
yourView.animate().alpha(1).setDuration(300);

Through the same way you can add animation listeners.

This also works for scale and translation animations.

神经暖 2024-09-17 22:30:05

试试这个

val shortAnimationDuration = 1000
            yourView.apply {
                alpha = 0f
                animate()
                    .alpha(1f)
                    .setDuration(shortAnimationDuration.toLong())
                    .setListener(object :AnimatorListenerAdapter(){
                        override fun onAnimationEnd(animation: Animator?) {
                           //You can add what you want to do after completing animation
                        }
                    })
            }

这是一个淡入动画。如果您想要淡出,只需交换 alpha 值即可。

Try This

val shortAnimationDuration = 1000
            yourView.apply {
                alpha = 0f
                animate()
                    .alpha(1f)
                    .setDuration(shortAnimationDuration.toLong())
                    .setListener(object :AnimatorListenerAdapter(){
                        override fun onAnimationEnd(animation: Animator?) {
                           //You can add what you want to do after completing animation
                        }
                    })
            }

This is a Fade in animation. If you want Fade out you just swap the alpha values.

殤城〤 2024-09-17 22:30:05

在我的解决方案中,我在任何通用视图的 util 函数中使用动画,并且我能够通过以这种方式定义内联值来实现这一点:

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

In my solution, I am using the animation inside a util function for any generic View and I was able to achieved this by defining the values inline this way:

fun View.fadeIn(durationMillis: Long = 250, delay: Long = 1000) {
    this.startAnimation(AlphaAnimation(0F, 1F).apply {
        duration = durationMillis
        startOffset = delay
        fillAfter = true
    })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文