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

BitmapTextureAtlas源代码:

 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)

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

4

2 回答 2

0

我建议您将分辨率策略设置为RatioResolutionPolicy并让引擎为您进行缩放。

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

于 2012-11-28T01:00:55.370 回答
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);       
}

}

于 2012-11-28T14:10:20.890 回答