2011-10-21 69 views
0

我一直在尝试几乎所有的东西来让我的动态壁纸停止超出虚拟机的内存,似乎没有任何工作。 我有一些大的动画,但希望不要太大,因为如果我再把它们放下来,它们会看起来很可怕。 我有一个30帧长的精灵7800x329像素,我把它放在mdpi文件夹中,它的大小是356 kb。 我有两个较小的动画,我想添加,但它也死了,即使当它加载时,我尝试将它设置为livewallpaper,并再次死亡。 下面的代码(我用水族教程本)的精灵Android sprite VM内存不足错误

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); 
     } 
    } 

在此任何帮助将不胜感激,因为这是一个驱使我坚果。 在此先感谢 山姆

回答

0

对不起,我应该回答很久以前。 只是减少帧和图像尺寸使用可清除或回收或两者兼有