2012-01-04 11 views
15

Tengo un pequeño problema para conseguir que un spinner de carga animado trabaje para una página de bienvenida. No aparece nada cuando trato de ejecutar el siguiente código. ¿Alguna sugerencia? Parece que algunas personas tienen problemas con esto en Google, pero no entiendo por qué el mío no funciona. ¡Gracias!usando android animation-list

animationloader.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/loadingspinner1" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner2" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner3" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner4" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner5" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner6" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner7" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner8" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner9" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner01" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner11" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner12" android:duration="200" /> 
</animation-list> 

SplashScreen.java

package com.secure.inmatecanteen; 

import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class SplashScreen extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splashscreen); 

    //Beginning the loading animation as we attempt to verify registration with SIP 
    ImageView ivLoader = (ImageView) findViewById(R.id.IVloadinganimation); 
    ivLoader.setBackgroundResource(R.anim.animationloader); 


    AnimationDrawable frameAnimation = (AnimationDrawable) ivLoader.getBackground(); 
    frameAnimation.start(); 
} 
} 

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:background="@android:color/white" > 

<ImageView 
android:id="@+id/iclogo" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:src="@drawable/iclogo" 
android:adjustViewBounds="true" 
/> 

<ImageView 
android:id="@+id/IVloadinganimation" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:adjustViewBounds="true" 
/> 

    </LinearLayout> 
+0

tienen u incluido esta actividad en manifest.xml fil ¿mi? – sampathpremarathna

+0

Sí, tengo. La actividad se muestra sin la animación – tier1

Respuesta

22

solucionado mi problema, no se puede iniciar animaciones en la alcrear. Tiene que estar en un oyente onclick o dentro de un ejecutable.

+4

pero la mía funciona en oncreate, pero no en onclick. –

+3

De acuerdo con http://developer.android.com/guide/topics/graphics/drawable-animation.html, nunca debe llamarlo a onCreate(). – rude

4

Puede reproducir/iniciar animation desde el método onWindowFocusChanged(boolean hasFocus).

15

creo que la opción más elegante y versátil es extender de la clase ImageView:

public class Loader extends ImageView { 

    public Loader(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public Loader(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public Loader(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     setBackgroundResource(R.drawable.loader); 
     final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground(); 
     post(new Runnable(){ 
      public void run(){ 
       frameAnimation.start(); 
      } 
     }); 
    } 
} 

El loader.xml ubicado en la carpeta dibujable:

<?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/loader_1" android:duration="50" /> 
    <item android:drawable="@drawable/loader_2" android:duration="50" /> 
    <item android:drawable="@drawable/loader_3" android:duration="50" /> 
    <item android:drawable="@drawable/loader_4" android:duration="50" /> 
    ..... 
</animation-list> 

ahora incluyen en sus puntos de vista algo tan simple como esto:

<com.yourpackage.Loader 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+0

tenía 3 dispositivos, solo podía hacer que funcionara en un Nexus 4 con Android 4.2.2, no funcionaba en los dispositivos Honeycomb o Gingerbread. Probablemente tenga que seguir con viewflippers. – CQM

+0

He actualizado el método init para agregar compatibilidad con honeycomb & gingerbread. Revisalo. –

+0

funciona como un encanto, gracias! – CQM

2

En este controlador, la animación no está completamente asociada a t La ventana, por lo que las animaciones no se pueden iniciar; en su lugar, esto generalmente se realiza como resultado de la acción del usuario (como presionar un botón) o dentro del onWindowFocusChangedhandler.

se refieren: androide 4 desarrollo de aplicaciones profesionales

0

No coloque recurso de imagen en el código XML.

Mi XML es:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:gravity="center" 
android:keepScreenOn="true" 
android:id="@+id/splashLayout" 
android:background="@color/colorPrimary" 
android:layout_height="match_parent"> 


<ImageView 
    android:layout_width="230dp" 
    android:layout_height="230dp" 
    android:id="@+id/iv_splash" 
    android:layout_marginTop="-80dp" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 


</RelativeLayout> 

En la Actividad hago

public class SplashActivity extends Activity { 

ImageView iv_splash; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 

    iv_splash=(ImageView)findViewById(R.id.iv_splash); 
    iv_splash.setBackgroundResource(R.drawable.splash); 
    final AnimationDrawable progressAnimation =(AnimationDrawable)iv_splash.getBackground(); 
    progressAnimation.start(); 
    } 
} 

archivo Disponibles

<?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/logo" android:duration="400"/> 
<item android:drawable="@drawable/logo1" android:duration="400"/> 

</animation-list> 

Está funcionando bien :)

Cuestiones relacionadas