1

I'm new to AndEngine.

For some reason, I have to create a TextureRegion from a Drawable variable.

I don't know if it is possible,

but my code is not working...

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
user538565
  • 513
  • 1
  • 6
  • 21

1 Answers1

2

I know this is not the best way of solving this problem, but you can turn your Drawable to a Bitmap and then create a TextureRegion from the Bitmap. Here's the code for creating a TextureRegion from 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);
        }

    }

Here's a link to help you make a Bitmap from your Drawable.

Hope you'll find a simpler way, but this should do the job as well. Good luck!

Community
  • 1
  • 1
Egor
  • 39,695
  • 10
  • 113
  • 130
  • i want to arrange drawable-button in grid...i done with first part..show drawable on button..but do u have any idea how can fit in grid-rectangle...any suggestion? – CoDe Jul 22 '12 at 10:23