-1

I need to display around 125 images in a imageview like a gif animate in 3-5 seconds. I tried with a timertask but look like slowly, how can I do?

max246
  • 225
  • 3
  • 13

2 Answers2

1

I would insist that you should try using AnimationDrawable. Keep all the image in the drawable folder and you can Animate them one by one after a particular duration.

say you have images in drawable folder like abc1, abc2, abc3,......Then just take one ImageView and iterate all the images in that ImageView using the Animation.

So try it like this, here I am only considering 5 images so if you have 125 then the loop should move upto 125 times.

My xml - 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"
    android:gravity="center_vertical|center_horizontal"
    >
<ImageView  android:id="@+id/imgView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />
</LinearLayout>

My Java Class - mainAct.java

public class mainAct extends Activity {

    AnimationDrawable anim = new AnimationDrawable();
    Handler handler = new Handler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView imageView = (ImageView) findViewById(R.id.imgView);

        for (int i = 1; i <= 5; i++) {
            anim.addFrame(getResources().getDrawable(getResources().getIdentifier("abc"+i, "drawable", getPackageName())), 1000);
        }

        imageView.setBackgroundDrawable(anim);
        imageView.post(new Runnable() {
            @Override
            public void run() {
                anim.start();
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mainAct.this.finish();
                    }
                },getTotalAnimationDuration());
            }
        });
    }

    private int getTotalAnimationDuration() {
        int mDuration = 0;
        for (int i = 0; i < anim.getNumberOfFrames(); i++) {
            mDuration = mDuration + anim.getDuration(i);
        }
        return mDuration;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        anim.stop();
    }
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 1
    That it's a good solution but I tried it and the application crashed about too many space has been alloced: 12-14 11:19:15.762: E/AndroidRuntime(14259): java.lang.OutOfMemoryError: bitmap size exceeds VM budget – max246 Dec 14 '11 at 11:20
  • try System.gc() after every 20 to 30 images in the loop. – Lalit Poptani Dec 14 '11 at 16:23
  • 1
    Well, the problem is that if I am going to add the frame to the AnimationDrawable I can't call the System.gc(); I can call it on my TimerTask but the script should display 2-3 images each second – max246 Dec 15 '11 at 01:09
0

This resource should be more than helpful, considering.

Display Animated GIF

You could also try: http://obviam.net/index.php/sprite-animation-with-android/

Community
  • 1
  • 1
ahodder
  • 11,353
  • 14
  • 71
  • 114