2012-11-27 146 views
1

对于我的应用程序,我有高分辨率的纹理。为了减少小屏幕大小我做这样的:减少纹理的大小

@Override 
public void onLoadResources(){ 
    Options options = new BitmapFactory.Options(); 
    options.inScaled = false; 

    // calculation inSampleSize 
    int sm = 1; 
    if (cameraWidth+cameraHeight < 1280) sm = 2;// < 800x480 
    if (cameraWidth+cameraHeight < 800) sm = 4;// < 480x320 
    options.inSampleSize = sm; 

    mTexture = new BitmapTextureAtlas(2048/sm, 2048/sm, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
    // Loading bitmap 
    sky_bm = BitmapFactory.decodeResource(getResources(), R.drawable.sky, options); 
    sky_src = new BitmapTextureAtlasSource(sky_bm); 

    skyRegion = TextureRegionFactory.createFromSource(mTexture, sky_src, 0, 0, false); 

    mEngine.getTextureManager().loadTexture(mTexture); 

BitmapTextureAtlasSource代码:

public class BitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { 

     private Bitmap mBitmap; 

     public BitmapTextureAtlasSource(Bitmap pBitmap) { 
      super(0,0); 
      //this.mBitmap = pBitmap; 
      this.mBitmap = pBitmap.copy(Bitmap.Config.ARGB_8888, false); 
     } 

     public int getWidth() { 
      return mBitmap.getWidth(); 
     } 

     public int getHeight() { 
      return mBitmap.getHeight(); 
     } 

     @Override 
     public BitmapTextureAtlasSource clone() { 
      return new BitmapTextureAtlasSource(Bitmap.createBitmap(mBitmap)); 
     } 

     public Bitmap onLoadBitmap(Config pBitmapConfig) { 
       return mBitmap; 
     } 

     @Override 
     public IBitmapTextureAtlasSource deepCopy() { 
      return null; 
     } 
    } 

但旋转屏幕的时候,我得到的错误:

FATAL EXCEPTION: GLThread 4895 
java.lang.IllegalArgumentException: bitmap is recycled 
    at android.opengl.GLUtils.texSubImage2D(GLUtils.java:220) 
    at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:162) 
    at org.anddev.andengine.opengl.texture.Texture.loadToHardware(Texture.java:116) 
    at org.anddev.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:146) 
    at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:507) 
    at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154) 
    at net.rbgrn.opengl.GLThread.guardedRun(GLThread.java:235) 
    at net.rbgrn.opengl.GLThread.run(GLThread.java:94) 

请告诉我我做错了什么。我会很感激的任何信息

+0

我想问题可能是在BitmapTextureAtlasSource。你能发布它的代码吗? –

+0

另一件我会质疑的是2048x2048的BitmapTextureAtlas的使用 - 甚至一些高端设备可能会抱怨。 – jmroyalty

回答

0

我想这个问题可能是在onLoadBitmap,你应该返回副本。我建议你试试这个实现扩展EmptyBitmapTextureAtlasSource:

public class BitmapTextureSource extends EmptyBitmapTextureAtlasSource { 

private Bitmap mBitmap; 

public BitmapTextureSource(Bitmap bitmap) { 
    super(bitmap.getWidth(), bitmap.getHeight()); 
    mBitmap = bitmap;  
} 

@Override 
public Bitmap onLoadBitmap(Config pBitmapConfig) { 
    return mBitmap.copy(pBitmapConfig, true);  
} 

}

+0

是的,它的工作原理!如果mBitmap.copy(Bitmap.Config.ARGB_8888,true)也插入到构造函数中。非常感谢罗德里戈迪亚斯。 – TimaCh

0

我建议你设置你的决议政策RatioResolutionPolicy并让引擎做你的缩放。

http://andengine.wikidot.com/detect-screen-resolution

+0

是的,我在我的应用程序中执行它。但我不想为弱设备加载较大的纹理。 – TimaCh

+0

为什么不能有2(或更多)不同的图像并加载相应的图像? – jmroyalty

+0

因为如果您支持多种设备(从240x320到720x1280),您将获得非常大的APK大小。如果您正在使用动画,为不同分辨率生成数百个文件是非常痛苦的。 –