I am trying to load 11 images from the drawable resources and display them in an ImageView with a delay. Practically doing something like AnimationDrawable, since creating such results in an OutOfMemory exception and a crash. It is both reinventing the wheel and some practice but still causes the app to crash. If I just init the imgView and do imgView.setImageResource(R.drawable.pic1);
the image is loaded. When I set a Handler and try to do postDelayed() like below I'm just able to see the background image of the layout before the app crashes with nothing in Logcat. Here is the code:
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
);
}
}
}
Related to this code is the imgView from 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>
And the alpha animation of the imgView from 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>
All tips and comments are welcome.