Я пытаюсь заставить этот пример работать:
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
но при запуске происходит сбой (принудительное закрытие), и я не совсем уверен, где проблема, поскольку я новичок в этом.
Мой код:
(Файл Java)
package com.ryan.test;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class TestAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
setContentView(R.layout.main);
}
}
Файл 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/note0" android:duration="50" />
<item android:drawable="@drawable/note1" android:duration="50" />
</animation-list>
Файл 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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/note0"
android:id="@+id/spinning_wheel_image"></ImageView>
</LinearLayout>
Обратите внимание, что XML-файл анимации находится в файле res / drawable-hdpi, поэтому его помещение в папку res / anim приводило к ошибкам. Также обратите внимание, что я Android-нуби;)
Спасибо!
Райан
EDIT;
В демонстрационном коде есть ошибка: http://code.google.com/p/android/issues/detail?id=1818
поэтому я изменил код: pastebin.com/ZtLf8J87
и это работает;)