淡入淡出Java中的Androidanimation

我想要一个ImageView的2秒钟的animation,花费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); 

当我运行该animation时, 什么也没有显示出来。 但是,当我删除一个alphaanimation,行为按预期工作。

我已经尝试过的事情:

  • setFillBeforesetFillAftersetFillEnabled每个可能的组合。
  • 将一个LinearInterpolator添加到AnimationSet

找出了我自己的问题。 该解决scheme最终基于内插器。

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

我知道这已经被回答,但…..

 <?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" /> 

快速简单的方法,快速做一个自我重复淡入淡出。 请享用

编辑:在你的acticity添加这个:

 yourView.startAnimation(AnimationUtils.loadAnimation(co‌​ntext, R.anim.yourAnimation)); 

这里是我使用AnimatorSet的解决scheme,它似乎比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(); 
 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(); 

另一种select:

无需为fadeInfadeOut定义2个animation。 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); 

然后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 } }); 

AnimationSets不会像预期的那样工作。 最后,我放弃了使用Handler类的postDelayed()来对animation进行sorting。

这是我用来淡入/淡出视图,希望这有助于某人。

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

如果你使用animation制作animation,你可以

animator (目录) – > 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"/> 

在java中

 Animator animator = AnimatorInflater.loadAnimator(context, R.animator.fade_out); animator.setTarget(the_view_you_want_to_animation); animator.setDuration(1000); animator.start(); 

其他方法使animation淡出只有Java代码是

 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(the_view_you_want_to_animation, "alpha", 1f, 0); fadeOut.setDuration(2000); fadeOut.start(); 

你也可以使用animationListener,如下所示:

 fadeIn.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { this.startAnimation(fadeout); } });