开发者

Storing an Animation in a static field

I'm looking for ways to speed up a few animations.

I came across this article that mentions in passing that storing a large bitmap in a static field can help with application load times (see paragraph 4).

What would be the equivalent technique for an AnimationDrawable?

Is it even possible to preload the frames in an animation drawable? Are they preloaded by default? Will this help me speed things up?

I am running a frame animation which is composed of .png's which are 256x256, they are being scaled down to the size of the image view, which I would guess is about 100x100, this will change when the app is used on a device with a different screen size then mine, however the size of this 100x100 image view will not change after the onCreate method of my app is called.

Solution: I modified the accepted solution as follows so that I could use the standard animation format that android uses (and that I already had my animations encoded in):

public Bitmap[] setAnimationArray(int res_anim){
        this.my_view.setBackgroundResource(res_anim); //view sized properly elsewhere
        AnimationDrawable t_anim = (Animati开发者_运维百科onDrawable) this.my_view.getBackground();
        Bitmap[] anim = new Bitmap[t_anim.getNumberOfFrames()];
        for(int i = 0;i<t_anim.getNumberOfFrames();i++){
            anim[i]=((BitmapDrawable) t_anim.getFrame(i)).getBitmap(); //extract bitmaps from the animation
        }
    return anim;

Edit: to the answer below I would like to add that performing animations in a surface view manually seems to be much faster then animating using the standard methods. Search Android SurfaceView for more info.


Define your bitmaps in arrays.xml like so:

    <array name="targetFrames">
        <item>@drawable/bitmap1</item>
        <item>@drawable/bitmap2</item>
        ...
   </array>

You can do something like this to create an array of bitmaps:

private void setupTargetFrames() {
        TypedArray targetResources = context.getResources().obtainTypedArray(R.array.targetFrames);
        targetFrames = new Bitmap[targetResources.length()];

        for (int i = 0; i < targetResources.length(); i++) {
            targetFrames[i] = BitmapFactory.decodeResource(context.getResources(), targetResources.getResourceId(i, R.drawable.defaulBitmap));
        }

        targetResources.recycle();
    }

Then play the animation by cycling through the bitmaps.

See BitmapFactory's other methods if you want to scale the bitmap when loading it in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜