1

我是 AndEngine 的新手。

出于某种原因,我必须从 Drawable 变量创建一个 TextureRegion。

不知道有没有可能

但我的代码不起作用......

public class DrawableTextureSource implements ITextureSource {
    private final int mWidth;
    private final int mHeight;
    private final Drawable mDrawable;
    private final Context mContext;
    public DrawableTextureSource(Context context, Drawable D) {
        mContext = context;
        mDrawable = D;
        mWidth = D.getIntrinsicWidth();
        mHeight = D.getIntrinsicHeight();
    } // end DrawableTextureSource()
    public DrawableTextureSource(Context context, Drawable D, int W, int H) {
        mContext = context;
        mDrawable = D;
        mWidth = W;
        mHeight = H;
    } // end DrawableTextureSource()
    public int getWidth() {
        return mWidth;
    } // end getWidth()
        public int getHeight() {
        return mHeight;
    } // end getHeight()
        public Bitmap onLoadBitmap(Config pBitmapConfig) {
        Bitmap bitmap = Bitmap.createBitmap(1024, 1024, pBitmapConfig);
        mDrawable.draw(new Canvas(bitmap));
        return bitmap;
    } // end onLoadBitmap()
    public DrawableTextureSource clone() {
        return new DrawableTextureSource(mContext, mDrawable, mWidth, mHeight);
    } // end clone()
} // end class
4

1 回答 1

2

我知道这不是解决此问题的最佳方法,但您可以将您的Drawable转为 a Bitmap,然后TextureRegionBitmap. TextureRegion这是从 a创建 a 的代码Bitmap

public class BitmapTextureSource implements ITextureSource {

        private Bitmap mBitmap = null;

        public BitmapTextureSource(Bitmap bitmap) {
            this.mBitmap = bitmap;
        }

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

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

        @Override
        public Bitmap onLoadBitmap() {
            return mBitmap.copy(mBitmap.getConfig(), false);
        }

        @Override
        public BitmapTextureSource clone() {
            return new BitmapTextureSource(mBitmap);
        }

    }

这是一个链接,可帮助您BitmapDrawable.

希望您能找到一种更简单的方法,但这也应该可以完成工作。祝你好运!

于 2011-07-13T13:51:39.020 回答