Android sprite VM内存不足错误

时间:2011-10-21 23:35:28

标签: android android-emulator sprite

我一直在努力让我的动态壁纸停止超出VM内存并且似乎没有任何效果。 我有一些很大的动画,但希望不大,因为如果我再把它们拉下来,它们看起来会很糟糕。 我有一个30像素长7800x329像素的精灵我放在mdpi文件夹中它是356 kb大。 我有两个较小的动画,我很想添加,但它也死了,即使它加载我尝试将其设置为livewallpaper,它再次死亡。 这是代码(我使用水族馆教程)的精灵

public class SpriteOne extends SpriteMovement {
    private static final int TOTAL_FRAMES_IN_SPRITE = 30;
    private static final int SPRITE_ONE_FPS = 15;

    public SpriteOne(Context context, TheTemplate thetemplate,  Point startPoint, int speed){               
        super(context, thetemplate);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        Bitmap leftBitmap = BitmapFactory.decodeResource(getContext().getResources(), ca.samsstuff.steampunkdroid.R.drawable.droidfinal, options);
        this.initialize(leftBitmap, SPRITE_ONE_FPS, TOTAL_FRAMES_IN_SPRITE, startPoint, speed);

    }

    public void render(Canvas canvas){
        super.render(canvas);   
    }

}

我听说过回收,但不太确定如何在这里添加。 这进入另一个名为TheTemplate的类,我也会添加它。

//template file

    public class TheTemplate {

        private SpriteThread spriteThread;  
        private SurfaceHolder _surfaceHolder;   
        private ArrayList<Renderable> _sprites;
        public Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background1);
        //droid measurements
        public Bitmap thedroid = BitmapFactory.decodeResource(getResources(), R.drawable.droidfinal);
        public int thedroidHeight = thedroid.getHeight();
        public int thedroidWidth = thedroid.getWidth() / 30;
        private Context _context;

        // add rescale stuff
        private float screenWidth = initFrameParamsWidth();
        private Bitmap theBackgroundImage;
        private float totalHeight = _backgroundImage.getHeight();
        private int screenSized = initFrameParams();
        private float theScaler = (float) (screenSized / totalHeight);

        private Bitmap oneBackImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);

        public void render(){
            Canvas canvas = null;
            try{

                canvas = this._surfaceHolder.lockCanvas(null);
                synchronized (this._surfaceHolder) {
                    this.onDraw(canvas);
                }

            }finally{
                if(canvas != null){
                    this._surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   
        }

        protected void onDraw(Canvas canvas) {
            this.renderBackGround(canvas);
            for (Renderable renderable : this._sprites) {
                renderable.render(canvas);
            }
        };

        public void start(){
            this.spriteThread.switchOn();
        }

        public void stop(){
            boolean retry = true;
            this.spriteThread.switchOff();
            while (retry) {
                try {
                 this.spriteThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }

        public int backgroundLeft() {
            int startMovement = (int) ((screenWidth - theBackgroundImage.getWidth()) / 2); 
            return startMovement;
        }


        public int backgroundRight() {
            return this.theBackgroundImage.getWidth();
        }

        public void initialize(Context context, SurfaceHolder surfaceHolder) {
            this.spriteThread = new SpriteThread(this); 
            this._surfaceHolder = surfaceHolder;        
            this._sprites = new ArrayList<Renderable>();
            this._context = context;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            this.theBackgroundImage = oneBackImage;
            this.addSprites();
        }

        private void addSprites() {
            Point startPoint = new Point((int) ((screenWidth / 2) - (thedroidWidth / 2)), ((screenSized / 2) - (thedroidHeight / 2)));
            this._sprites.add(new SpriteOne(this._context, this, startPoint , 30));
        }

        private void renderBackGround(Canvas canvas)
        {
            float canvasewidthsize = (float) ((screenWidth / 2) - (theBackgroundImage.getWidth() / 2));
            canvas.drawBitmap(this.theBackgroundImage, canvasewidthsize, 0, null);
        }
    }

任何帮助都会非常感激,因为这个让我疯狂。 提前致谢 萨姆

1 个答案:

答案 0 :(得分:0)

抱歉,我很久以前就应该回答这个问题了。 只需减少框架和图像尺寸使用可清除或回收或两者