Splash en Android con animación

De WikiCode

Saltar a navegación, buscar
Delicious

Contenido

Objetivo

Crear una pantalla de Splash en Android que muestre la entrada y salida de una imagen con un fade. Utilizamos las animaciones definidas en XML.

Necesitamos la imagen wikicode.png en los resources.

WikiCode.png

Esta imagen no la cargamos al ImageView en el Layout, sino que se la asociamos en el evento onAnimationStart de la animación. También funcionaría si el ImageView lo tuviera asociado desde el principio.

Archivos

animacion.java

package com.wikicode.animacion;
 
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
 
public class animacion extends Activity {
    /** Called when the activity is first created. */
	AnimationSet animationSet;
	ImageView imagen;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        imagen = (ImageView) findViewById(R.id.logo);
        configurarAnimacion();
        imagen.startAnimation(animationSet);
    }
 
    public void configurarAnimacion(){
    	animationSet = new AnimationSet(true);
 
 
        Animation fadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
        fadein.setDuration(1000);
        Animation fadeout = AnimationUtils.loadAnimation(this, R.anim.fadeout);
        fadeout.setDuration(1000);
        fadeout.setStartOffset(2500);
 
        animationSet.addAnimation(fadein);
        animationSet.addAnimation(fadeout);
        animationSet.setStartOffset(1000);
 
 
 
        animationSet.setAnimationListener(new AnimationListener(){
 
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				//Aqui lanzariamos la siguiente activity
			}
 
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}
 
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				imagen.setImageResource(R.drawable.wikicode);
			}
 
        });
 
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:id="@+id/inicio">
		 <ImageView 
			   android:id="@+id/logo" 
			   android:layout_width="fill_parent" 
			   android:layout_height="fill_parent" 
			   android:adjustViewBounds="true"
			   >
		</ImageView>
</LinearLayout>

Definición de animaciones

Los archivos fadein.xml y feadeout.xml tienen la definición del comportamiento de las animaciones del fade que se va a realizar a la imagen. Estos archivos deben estar en una carpeta dentro de resources llamada "anim".

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="1000" android:repeatCount="0"/>
</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="1000" android:repeatCount="0"/>
</set>

¿Te ha sido útil este artículo?

174 Valoración: 5.3/10 (3 votos)

Herramientas personales
Espacios de nombres
Variantes
Acciones
Navegación
Categorías
support
Sitios
Herramientas