Android淡入,并与ImageView淡出

我正在build设一个幻灯片,有一些麻烦。

我已经在xml中创build了2个淡入淡出animation

fadein.xml

<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="2000"/> </set> 

fadeout.xml

  <?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="2000"/> </set> 

需要做的是从ImageView中使用淡入淡出效果来改变图像,所以当前显示的图像会淡出,而另一个淡入淡出。考虑到我已经设置了一个图像,我可以淡出这个图像没有问题,与此:

  Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim); imageView.startAnimation(fadeoutAnim); 

但是,然后,我设置下一个图像显示:

  imageView.setImageBitmap(secondImage); 

它只是显示在imageView,当我设置animation隐藏图像,淡入淡出…有什么办法解决,我的意思是,当我做imageView.setImageBitmap(secondImage); 命令,图像不会立即显示出来,只有在执行淡入淡出animation时?

Solutions Collecting From Web of "Android淡入,并与ImageView淡出"

为了实现这种方式,您需要添加一个AnimationListener,以便检测animation的开始和结束。 当调用淡出的onAnimationEnd()时,可以将ImageView对象的可见性设置为View.INVISIBLE,切换图像并在animation中开始淡入淡出 – 此处还需要另一个AnimationListener。 当您接收onAnimationEnd()以淡入animation时,将ImageView设置为View.VISIBLE,并且应该给您所需的效果。

我之前实现了一个类似的效果,但是我使用了一个ViewSwitcher和2个ImageView,而不是一个ImageView。 您可以使用淡入淡出设置ViewSwitcher的“in”和“out”animation,以便pipe理AnimationListener实现。 然后,您只需在两个ImageView之间切换即可。

编辑:为了更有用,这里是一个如何使用ViewSwitcher的简单例子。 我已经在https://github.com/aldryd/imageswitcher包含了完整的源代码&#x3002;

activity_main.xml中

  <ViewSwitcher android:id="@+id/switcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:inAnimation="@anim/fade_in" android:outAnimation="@anim/fade_out" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/sunset" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/clouds" /> </ViewSwitcher> 

MainActivity.java

  // Let the ViewSwitcher do the animation listening for you ((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ViewSwitcher switcher = (ViewSwitcher) v; if (switcher.getDisplayedChild() == 0) { switcher.showNext(); } else { switcher.showPrevious(); } } }); 

我想达到和你一样的目标,所以我写了下面这个方法,如果你把一个ImageView和一个引用列表传递给图像的话,

 ImageView demoImage = (ImageView) findViewById(R.id.DemoImage); int imagesToShow[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3 }; animate(demoImage, imagesToShow, 0,false); private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) { //imageView <-- The View which displays the images //images[] <-- Holds R references to the images to display //imageIndex <-- index of the first image to show in images[] //forever <-- If equals true then after the last image it starts all over again with the first image resulting in an infinite loop. You have been warned. int fadeInDuration = 500; // Configure time values here int timeBetween = 3000; int fadeOutDuration = 1000; imageView.setVisibility(View.INVISIBLE); //Visible or invisible by default - this will apply when the animation ends imageView.setImageResource(images[imageIndex]); Animation fadeIn = new AlphaAnimation(0, 1); fadeIn.setInterpolator(new DecelerateInterpolator()); // add this fadeIn.setDuration(fadeInDuration); Animation fadeOut = new AlphaAnimation(1, 0); fadeOut.setInterpolator(new AccelerateInterpolator()); // and this fadeOut.setStartOffset(fadeInDuration + timeBetween); fadeOut.setDuration(fadeOutDuration); AnimationSet animation = new AnimationSet(false); // change to false animation.addAnimation(fadeIn); animation.addAnimation(fadeOut); animation.setRepeatCount(1); imageView.setAnimation(animation); animation.setAnimationListener(new AnimationListener() { public void onAnimationEnd(Animation animation) { if (images.length - 1 > imageIndex) { animate(imageView, images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array } else { if (forever){ animate(imageView, images, 0,forever); //Calls itself to start the animation all over again in a loop if forever = true } } } public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } }); } 

你有没有想过使用TransitionDrawable而不是自定义animation? https://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html

达到你要找的东西的一个方法是:

 // create the transition layers Drawable[] layers = new Drawable[2]; layers[0] = new BitmapDrawable(getResources(), firstBitmap); layers[1] = new BitmapDrawable(getResources(), secondBitmap); TransitionDrawable transitionDrawable = new TransitionDrawable(layers); imageView.setImageDrawable(transitionDrawable); transitionDrawable.startTransition(FADE_DURATION); 

基于阿拉丁Q的解决scheme,这里是我写的一个帮助函数,它将改变图像视图中的图像,同时在animation中稍微淡出/淡入:

 public static void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) { final Animation anim_out = AnimationUtils.loadAnimation(c, android.R.anim.fade_out); final Animation anim_in = AnimationUtils.loadAnimation(c, android.R.anim.fade_in); anim_out.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { v.setImageBitmap(new_image); anim_in.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) {} }); v.startAnimation(anim_in); } }); v.startAnimation(anim_out); } 

我使用fadeInanimationreplace旧的一个新的图像

 ObjectAnimator.ofFloat(imageView, View.ALPHA, 0.2f, 1.0f).setDuration(1000).start(); 

我正在使用这种程序链接animation的例程。

  final Animation anim_out = AnimationUtils.loadAnimation(context, android.R.anim.fade_out); final Animation anim_in = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); anim_out.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { //////////////////////////////////////// // HERE YOU CHANGE YOUR IMAGE CONTENT // //////////////////////////////////////// //ui_image.setImage... anim_in.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) {} }); ui_image.startAnimation(anim_in); } }); ui_image.startAnimation(anim_out); 

最好的和最简单的方式,对我来说,这是..

– >简单地创build一个包含sleep()的Handler的线程。

 private ImageView myImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shape_count); myImageView= (ImageView)findViewById(R.id.shape1); Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein); myImageView.startAnimation(myFadeInAnimation); new Thread(new Runnable() { private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { Log.w("hendler", "recived"); Animation myFadeOutAnimation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fadeout); myImageView.startAnimation(myFadeOutAnimation); myImageView.setVisibility(View.INVISIBLE); } }; @Override public void run() { try{ Thread.sleep(2000); // your fadein duration }catch (Exception e){ } handler.sendEmptyMessage(1); } }).start(); } 

你可以通过两个简单的点来完成,并在代码中进行更改

1.在您的项目的anim文件夹的xml中,设置淡入和淡出持续时间不相等

2.在你的java类开始淡出animation之前,设置第二个imageView的可见性在淡出之后animation开始设置第二个imageView你要淡入的可见性可见

fadeout.xml

 <alpha android:duration="4000" android:fromAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="0.0" /> 

fadein.xml

 <alpha android:duration="6000" android:fromAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="1.0" /> 

在你的java类

 Animation animFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out); ImageView iv = (ImageView) findViewById(R.id.imageView1); ImageView iv2 = (ImageView) findViewById(R.id.imageView2); iv.setVisibility(View.VISIBLE); iv2.setVisibility(View.GONE); animFadeOut.reset(); iv.clearAnimation(); iv.startAnimation(animFadeOut); Animation animFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in); iv2.setVisibility(View.VISIBLE); animFadeIn.reset(); iv2.clearAnimation(); iv2.startAnimation(animFadeIn);