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:
- FindViewById devuelve el elemento erróneo?
- ¿Cómo puedo usar View pager con touchimageview?
- Quiero una imagen dentro de SimpleAdapter?
- La dimensión de la pantalla del dispositivo tiene un efecto extraño en las imágenes a escala
- Cómo tener una imagen de fondo de pantalla completa (con centro de cultivo) que no cambia el tamaño
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.
- Android: establece un borde alrededor de un ImageView
- Cómo mover sin problemas una imagen de vista con el dedo de los usuarios en el emulador de Android
- ImageView en ListView que amplía el ancho máximo
- ImageView rellena el ancho o altura de los padres, pero mantiene la relación de aspecto
- Cómo desvanecer imagen en ImageView cargado de url
- ¿Cómo obtengo diferentes posiciones de clic en un solo ImageView
- Calidad de mapa de bits Horrible ImageView?
- Cargar un vector dibujable en la vista de la imagen desde la tarjeta sd
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.
- Error de pelusa "No trate la posición como fija; Sólo usar inmediatamente … "
- ¿Es necesario inicializar Firebase Analytics en cada actividad?