简体   繁体   English

Android Java:高效的位图动画分配

[英]Android java: Efficient bitmap animation allocation

I have 17 bitmaps of the same size that are frames of a character walking animation, and I want them to loop in my code. 我有17个大小相同的位图,它们都是角色行走动画的帧,我希望它们在我的代码中循环播放。 I want to be able to use inBitmap since I am reusing the same bitmaps. 我希望能够使用inBitmap,因为我正在重用相同的位图。 I have a few questions about doing this: 我对此有一些疑问:

  1. can I / should I cache the bitmaps, and if so how? 我/应该缓存位图吗?

  2. is there any more memory efficient way of doing this? 有没有更多的内存有效的方式来做到这一点?

My code looks like this: (i am using a custom draw view) 我的代码如下所示:(我正在使用自定义绘制视图)

onCreate: onCreate:

        bitmapOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.walk0, bitmapOptions);
        aBitmap = Bitmap.createBitmap(bitmapOptions.outWidth,
                bitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
        bitmapOptions.inJustDecodeBounds = false;
        bitmapOptions.inBitmap = aBitmap;
        bitmapOptions.inSampleSize = 1;
        BitmapFactory.decodeResource(getResources(), R.drawable.walk0, bitmapOptions);
        ship = Bitmap.createBitmap(aBitmap, 0, 0, aBitmap.getWidth(), aBitmap.getHeight(), shipMatrix, true);

onDraw: onDraw:

@Override
protected void onDraw(Canvas canvas) 
{
shipRectf.right = ship.getWidth();
shipRectf.left = 0;
shipRectf.top = 0;
shipRectf.bottom = ship.getHeight();
canvas.drawBitmap(ship, null, shipRectf, null);
}

Animating: 动画制作:

Runnable walkData = new Runnable()
    {
        public void run(){
            try{    

                BitmapFactory.Options shipOptions = null;
                shipOptions = bitmapOptions;

                if(shipOptions != null){
                shipOptions.inBitmap = aBitmap;
                }
            if(walkAnim==true)
                {
                    walkNum = (walkNum + 1) % imageIDs.length;
                   //walknum = index, imageids = int[] {R.drawables}
                    aBitmap = BitmapFactory.decodeResource(getResources(), imageIDs[walkNum], shipOptions);
                    ship = Bitmap.createBitmap(aBitmap, 0, 0, aBitmap.getWidth(), aBitmap.getHeight(), shipMatrix, true);
                                           invalidate();

                }
                if(walkAnim==false)
                {
                    walkNum = 0;
                    ship = BitmapFactory.decodeResource(getResources(), imageIDs[walkNum], shipOptions);
                }


            handler3.postDelayed(this, 50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    };

Loading your bitmaps at runtime like that is going to end up being very slow. 这样在运行时加载位图将变得非常缓慢。

What I do is load all my bitmaps when the game is created, and store them in an array. 我要做的是在创建游戏时加载我的所有位图,并将它们存储在数组中。 Then when you are drawing you just reference the bitmap array and get the correct image for the current frame. 然后,在绘制时,您只需引用位图数组并获取当前帧的正确图像。 It uses more memory but that's always the trade-off for faster runtime. 它使用更多的内存,但这始终是提高运行速度的折衷方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM