Android alpha 动画淡入淡出带延迟

问题描述 投票:0回答:4

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

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

  1. 1 秒从 0 到 1 的 alpha
  2. 将 alpha 保持在 1 5 秒
  3. 1 秒从 1 到 0 的 alpha
  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 管理此类动画的方式出现内部不连贯。

有什么想法吗?

谢谢你。

android animation alpha fadein fadeout
4个回答
113
投票

好的记住这2点就可以解决这个问题


  • 如果我想在 5 秒后制作动画

    1.0f to 0.0f
    ,动画持续时间为 1 秒,这最终是一个 1 秒的动画,暂停 5 秒。

    要实现这一目标:

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

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

    1.

    0.0f to 1.0f
    暂停 5 秒,持续时间 1 秒

    2.

    1.0f to 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);

20
投票

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

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

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

通过同样的方式你可以添加动画监听器。

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


1
投票

试试这个

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 值即可。


0
投票

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

fun View.fadeIn(durationMillis: Long = 250, delay: Long = 1000) {
    this.startAnimation(AlphaAnimation(0F, 1F).apply {
        duration = durationMillis
        startOffset = delay
        fillAfter = true
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.