Анимировать изображение с помощью ImageView

#android #animation #imageview

#Android #Анимация #просмотр изображений

Вопрос:

Я получаю исключение, когда пытаюсь анимировать изображение с помощью frame и ImageView . Вот код:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView rocketImage = (ImageView) findViewById(R.id.imageView1);
    rocketImage.setBackgroundResource(R.drawable.animation_sample);
    AnimationDrawable rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
    rocketAnimation.run();
}
  

и мой animation_sample.xml

 <frames id="anim_sample" oneshot="false">
  <frame drawable="@drawable/a1" duration="50" />
  <frame drawable="@drawable/a2" duration="50" />
  <frame drawable="@drawable/a3" duration="50" />  
  <frame drawable="@drawable/a4" duration="50" />
  <frame drawable="@drawable/a5" duration="50" />
</frames>
  

и 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">
    <ImageView android:id="@ id/imageView1" android:src="@drawable/icon"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
  

Я что-то упускаю (в manifest.xml или любой другой)?

Комментарии:

1. Что не работает? Вы получаете какие-либо ошибки? Пожалуйста, будьте более конкретны

Ответ №1:

попробуйте с этим animation_sample.xml

 <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@ id/anim_sample" android:oneshot="false">
  <item android:drawable="@drawable/a1" android:duration="50" />
  <item android:drawable="@drawable/a2" android:duration="50" />
  <item android:drawable="@drawable/a3" android:duration="50" />  
  <item android:drawable="@drawable/a4" android:duration="50" />
  <item android:drawable="@drawable/a5" android:duration="50" />
</animation-list>
  

извините, если есть какие-то ошибки, но у меня здесь нет eclipse и SDK

Комментарии:

1. Если повезет, отобразится только изображение ImageView по умолчанию, НО анимация изображений, которые я предоставил, отсутствует.