Android cambia la imagen de fondo con la animación de entrada / salida

Escribí el código que puede cambiar la imagen de fondo al azar cada 5 segundos. Ahora quiero usar la animación de entrada / salida de desvanecimiento para cambiar la imagen de fondo, pero no sé cómo puedo utilizar esta animación.

Esta es mi fuente:

void handlechange() { Handler hand = new Handler(); hand.postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // change image here change(); } private void change() { // TODO Auto-generated method stub Random rand = new Random(); int index = rand.nextInt(image_rundow.length); mapimg.setBackgroundResource(image_rundow[index]); handlechange(); } }, 4000); } 

En este momento todo está bien. Puedo cambiar la imagen de fondo al azar, pero no sé cómo puedo utilizar animación fade in / out.

Si alguien sabe la solución por favor ayúdeme, gracias.

Debe llamar a estos códigos.

En primer lugar, usted tiene que hacer dos archivos xml para fade in & out animación como esta.

Fade_in.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:fillAfter="true" android:duration="2000" /> </set> 

Fade_out.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:fillAfter="true" android:duration="2000" /> </set> 

En segundo lugar, tienes que ejecutar animación de imageView como a continuación y también tienes que configurar AnimationListener para cambiar fadeout cuando final fadein.

 Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in); imageView.startAnimation(fadeIn); fadeIn.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out); imageView.startAnimation(fadeOut); } @Override public void onAnimationRepeat(Animation animation) { } }); 

Para el fade en la animación, cree una nueva carpeta llamada 'anim' en su carpeta res y dentro de ella, cree 'fade_in.xml' con el siguiente código

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

Ahora para ejecutar esta animación en su imagen, utilice el siguiente código en su actividad

 Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in); imageView.setAnimation(anim); anim.start(); 

Para la animación de fadeout, basta con intercambiar valores de android: fromAlpha and android: toAlpha

Espero que esto ayude.

Puede utilizar relativeLayout y agregar una capa de vista de fondo que establezca la altura y la anchura match_parent. Todos los demás elementos de la interfaz de usuario deben situarse encima de esta vista. En su código. Defina fadeOut y fadeIn animación. Encuentra esa vista de fondo por id, y luego haz esto:

  xxxBackground.startAnimation(fadeOut); xxxBackground.setBackgroundResource(R.drawable.your_random_pic); xxxBackground.startAnimation(fadeIn); 

Puede utilizar algún interpolador para ajustar el rendimiento.

Necesitas AnimationDrawable con animación.

Primer paso AnimationDrawable

-Crear un archivo /res/anim/anim_android.xml

 <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"> <item android:drawable="@drawable/android_1" android:duration="100"/> <item android:drawable="@drawable/android_2" android:duration="100"/> <item android:drawable="@drawable/android_3" android:duration="100"/> <item android:drawable="@drawable/android_4" android:duration="100"/> <item android:drawable="@drawable/android_5" android:duration="100"/> <item android:drawable="@drawable/android_6" android:duration="100"/> <item android:drawable="@drawable/android_7" android:duration="100"/> </animation-list> 

-Agregar un ImageView con android: src = "@ anim / anim_android".

 <ImageView android:id="@+id/myanimation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@anim/anim_android" /> 

Segundo paso

-En su actividad, cree AnimationDrawable y Animation

 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.setOneShot(true); animationDrawable.start(); Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in); imageView.setAnimation(animation); animation.start(); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out); imageView.startAnimation(fadeOut); } @Override public void onAnimationRepeat(Animation animation) { } }); 

Usted no necesita Handler.

  • Volley NetworkImageView - cómo reciclar mapas de bits?
  • HorizontalScrollView o Carrousel?
  • ¿Cuándo (si es que) debería usar Bitmap.recycle ()?
  • Cómo cargar la imagen del GIF en el marcador de posición de Glide / Picasso / Ion etc
  • ListView como en la aplicación de ejemplo de cargador de imágenes universal
  • Rotar una imagen con animación
  • Cómo mantener la relación de aspecto de la imagen si imageview se extiende primero para rellenar padre
  • Tamaño de las imágenes en imageview android
  • Cuando haga clic en un botón, gire la imagen en sentido horario en android
  • Alpha-gradient en Android
  • ¿Cómo puedo guardar mi mapa de bits correctamente por segunda vez?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.