Vaibs
Vaibs

Reputation: 1138

How to optimize frame animation?

I want to do Frame Animation with 30 frames. My code for the same is:

animation = new AnimationDrawable();
    for (int i= 1; i<= 30 ; i++){
        frame=  "xyz"+ i;
        resource    =   this.getResources().getIdentifier(frame, "drawable", "com.example.frameanimation");
        Log.e("123", "resource value======"+resource+"============"+frame);

        animation.addFrame(getResources().getDrawable(resource), 50);


            System.gc();

    }

    animation.setOneShot(false);

In this code I have included System.gc();. Will this help me in optimizing in memory related issues. Or it can still give memory related issues. And does this mean that 30 images which are present in memory while frame animation will be destroyed.?

Is there any other way to optimize this? Suggestions please.

Thanks in Advance.

Upvotes: 0

Views: 1184

Answers (1)

minhaz
minhaz

Reputation: 4233

System.gc() is a hints to garbage collector. It means garbage collector may not collect garbage after executing this line of code.

Good way to optimize this is to optimize the drawable you are loading. You can load based on screen DPI. It is not useful to load a large image inside a small screen. Read this post about image loading link.

You can also try, use the main image as background and only animated part inside animation.

Alternatively, you can try using SoftReference. If you run into heap memory issue this will at least solve this problem.

Upvotes: 1

Related Questions