淡入淡出Java中的Android动画

plo*_*man 141 animation android

我希望有一个2秒的ImageView动画,花费1000毫秒渐弱,然后1000毫秒淡出.

这是我在ImageView构造函数中到目前为止的内容:

Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);

Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
Run Code Online (Sandbox Code Playgroud)

当我运行该动画时,没有任何显示.但是,当我删除其中一个alpha动画时,行为按预期工作.

我已经尝试过的事情:

  • 的所有可能的组合setFillBefore,setFillAftersetFillEnabled.
  • 添加LinearInterpolatorAnimationSet.

plo*_*man 242

弄清楚我自己的问题.该解决方案最终基于内插器.

Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);

Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
Run Code Online (Sandbox Code Playgroud)


如果你正在使用Kotlin

val fadeIn = AlphaAnimation(0f, 1f)
fadeIn.interpolator = DecelerateInterpolator() //add this
fadeIn.duration = 1000

val fadeOut = AlphaAnimation(1f, 0f)
fadeOut.interpolator = AccelerateInterpolator() //and this
fadeOut.startOffset = 1000
fadeOut.duration = 1000

val animation = AnimationSet(false) //change to false
animation.addAnimation(fadeIn)
animation.addAnimation(fadeOut)
this.setAnimation(animation)
Run Code Online (Sandbox Code Playgroud)

  • 我认为你不需要有两个独立的动画......我想你可以有一个动画并设置参数:fadeInOut.setRepeatMode(Animation.REVERSE); (8认同)

Kon*_*ski 127

我知道这已经得到了解答但是......

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0" 
    android:toAlpha="0.0" 
    android:duration="1000"    
    android:repeatCount="infinite" 
    android:repeatMode="reverse"
    />
Run Code Online (Sandbox Code Playgroud)

快速简便的方法,通过自我重复快速淡入淡出.请享用

编辑:在您的活动中添加:

yourView.startAnimation(AnimationUtils.loadAnimation(co??ntext, R.anim.yourAnimation));
Run Code Online (Sandbox Code Playgroud)

  • +1因为有了这个答案,我可以做出淡入,淡出和淡入的安全性(只需设置repeatCount ="2").我不能像接受的答案所提出的那样连接动画. (6认同)

Vit*_*nko 26

viewToAnimate.animate().alpha(1).setDuration(1000).setInterpolator(new DecelerateInterpolator()).withEndAction(new Runnable() {
    @Override
    public void run() {
        viewToAnimate.animate().alpha(0).setDuration(1000).setInterpolator(new AccelerateInterpolator()).start();
    }
}).start();
Run Code Online (Sandbox Code Playgroud)

  • 棉花糖的Elegante! (4认同)

TWi*_*lly 25

这是我使用AnimatorSet的解决方案,它似乎比AnimationSet更可靠.

// Custom animation on image
ImageView myView = (ImageView)splashDialog.findViewById(R.id.splashscreenImage);

ObjectAnimator fadeOut = ObjectAnimator.ofFloat(myView, "alpha",  1f, .3f);
fadeOut.setDuration(2000);
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(myView, "alpha", .3f, 1f);
fadeIn.setDuration(2000);

final AnimatorSet mAnimationSet = new AnimatorSet();

mAnimationSet.play(fadeIn).after(fadeOut);

mAnimationSet.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        mAnimationSet.start();
    }
});
mAnimationSet.start();
Run Code Online (Sandbox Code Playgroud)


Mar*_*ple 19

另一种选择:

无需为fadeInfadeOut定义2个动画.fadeOutfadeIn相反.

所以你可以用Animation.REVERSE这样做:

AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
view.findViewById(R.id.imageview_logo).startAnimation(alphaAnimation);
Run Code Online (Sandbox Code Playgroud)

然后onAnimationEnd:

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
        public void onAnimationStart(Animation animation) {
            //TODO: Run when animation start
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //TODO: Run when animation end
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            //TODO: Run when animation repeat
        }
    });
Run Code Online (Sandbox Code Playgroud)


小智 9

这是我以前淡出/淡出视图,希望这有助于某人.

private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
    AnimatorSet mAnimationSet = new AnimatorSet();
    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA,  1f, 0f);
    fadeOut.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            fadeOutTarget.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
    fadeOut.setInterpolator(new LinearInterpolator());

    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
    fadeIn.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            fadeInTarget.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {}

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
    fadeIn.setInterpolator(new LinearInterpolator());
    mAnimationSet.setDuration(duration);
    mAnimationSet.playTogether(fadeOut, fadeIn);
    mAnimationSet.start();
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*nus 9

由于我相信XML的强大功能(用于布局),这相当于接受的答案,但纯粹作为动画资源:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/accelerate_decelerate"
    android:fillAfter="true">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="1000" />
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="1000"
        android:startOffset="1000"/>
</set>
Run Code Online (Sandbox Code Playgroud)

fillAfter是淡出留在完成动画后.您可以猜测动画的interpolator手柄插值.您还可以使用其他类型的插值器,如线性或过冲.

一定要在视图上开始动画:

yourView.startAnimation(AnimationUtils.loadAnimation(co??ntext, R.anim.fade));
Run Code Online (Sandbox Code Playgroud)


Ric*_*ard 8

AnimationSets似乎根本无法正常工作.最后我放弃并使用Handler类的postDelayed()来对动画进行排序.


Pha*_*inh 8

如果您使用Animator制作动画,则可以

anim(目录) - > fade_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<objectAnimator
    android:propertyName="alpha"
    android:valueFrom="0"
    android:valueTo="1"
    xmlns:android="http://schemas.android.com/apk/res/android"/>
Run Code Online (Sandbox Code Playgroud)

在java中

Animator animator = AnimatorInflater.loadAnimator(context, R.animator.fade_out);
animator.setTarget(the_view_you_want_to_animation);
animator.setDuration(1000);
animator.start();
Run Code Online (Sandbox Code Playgroud)

用java代码制作动画淡出的其他方法是

ObjectAnimator fadeOut = ObjectAnimator.ofFloat(the_view_you_want_to_animation, "alpha",  1f, 0);
fadeOut.setDuration(2000);
fadeOut.start();
Run Code Online (Sandbox Code Playgroud)