Я пытаюсь загрузить 11 изображений из доступных для рисования ресурсов и отобразить их в ImageView с задержкой. Практически делать что-то вроде AnimationDrawable, поскольку создание таких результатов приводит к исключению OutOfMemory и сбою. Это и изобретение велосипеда, и некоторая практика, но все равно вызывает сбой приложения. Если я просто запускаю imgView и делаю imgView.setImageResource(R.drawable.pic1);
, изображение загружается. Когда я устанавливаю обработчик и пытаюсь выполнить postDelayed(), как показано ниже, я просто вижу фоновое изображение макета до того, как приложение вылетает без ничего в Logcat. Вот код:
package com.forms.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class Animacia extends Activity {
final static int[] imgGalleryResources = {
R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8, R.drawable.pic9,
R.drawable.pic10, R.drawable.pic11
};
ImageView imgView;
Handler handlerTimer = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
initGUI();
}
private void initGUI() {
int i;
imgView = (ImageView)findViewById(R.id.imgView);
imgView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha));
for(i = 0; i < imgGalleryResources.length ;i++) {
final int res = imgGalleryResources[i];
handlerTimer.postDelayed(
new Runnable() {
public void run() {
imgView.setImageResource(res);
}
},
5000
);
}
}
}
С этим кодом связан imgView из layout/animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/heart"
android:id="@+id/frameLayout1">
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
И альфа-анимация imgView из anim/alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.3"
android:toAlpha="0.9"
android:duration="2000" />
</alpha>
Все советы и комментарии приветствуются.