减少纹理的大小

时间:2012-11-27 20:44:33

标签: java andengine

对于我的应用程序,我有一个高分辨率的纹理。为了减小小屏幕的尺寸我喜欢这样:

@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)

请告诉我我做错了什么。我将不胜感激任何信息

2 个答案:

答案 0 :(得分:0)

我建议您将解决方案政策设置为RatioResolutionPolicy,然后让引擎为您进行扩展。

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

答案 1 :(得分: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);       
}

}