2012-11-29 4 views
3

Я прочитал несколько статей, но никто не исправил мою проблему, используя список анимации. Я уже знаю, что я не могу запустить анимацию в OnCreate(), так что я положил его внутри OnClick(), но она по-прежнему не работает ...android animation-list

public class Main extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 

AnimationDrawable explosionAnimation; 
Button btnGo; 
ImageView explosionImage; 

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

    explosionImage = (ImageView) findViewById(R.id.balloon); 
    explosionImage.setBackgroundResource(R.drawable.explosion); 
    explosionAnimation = (AnimationDrawable) explosionImage.getBackground(); 

    btnGo = (Button)findViewById(R.id.btnGo); 
    btnGo.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) 
    { 
    case R.id.btnGo: 
     explosionAnimation.start(); 
     break; 
    } 
} 
} 

explosion.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="true"> 

<item android:drawable="@drawable/explode_1" android:duration="200" /> 
<item android:drawable="@drawable/explode_2" android:duration="200" /> 
<item android:drawable="@drawable/explode_3" android:duration="200" /> 
<item android:drawable="@drawable/explode_4" android:duration="200" /> 
<item android:drawable="@drawable/explode_5" android:duration="200" /> 

</animation-list> 

main.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="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<ImageView 
    android:id="@+id/balloon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="30dp" 
    android:src="@drawable/balloon" /> 

<Button 
    android:id = "@+id/btnGo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="10dp" 
    android:text="Play" 
    /> 

</LinearLayout> 

ответ

0

На вашем main.xml изменить вид изображения на SRC android:src="@drawable/explosion"

затем на вашем изменении Main.java

explosionAnimation=(AnimationDrawable)((ImageView) explosionImage).getDrawable();

попробовать. он должен работать

0

Я думаю, вы должны заменить

explosionAnimation.start(); 

с

explosionImage startAnimation(explosionAnimation); 

Потому что в вашем заявлении, это не указано, что «На каком виде вы хотите, чтобы начать анимацию!»

0

Ну, я бы посоветовал вам поместить его в метод onWindowsFocusChange вашей деятельности внутри потока. Например:

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
    super.onWindowFocusChanged(hasFocus); 
    delayedactivation(R.drawable.animationname, yourview, delay); 
} 

public void delayedActivation(final int res, final View view, final int delay) 
{ 
    final Handler handle = new Handler(); 
    Thread t = new Thread() 
    { 
    @Override 
    public void synchronized run() 
    { 
     try 
     { 
     handle.postDelayed(new Runnable() 
     { 
      @Override 
      public void run() 
      {  
       view.setBackgroundResource(res); 
       anime = (AnimationDrawable) getResources().getDrawable(res); 
       anime = (AnimationDrawable)view.getBackground(); 
       anime.start();    
      } 
      },delay); 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    }; 
}; 
t.start();  
} 
Смежные вопросы