【问题标题】:AndEngine Texture from Drawable来自 Drawable 的 AndEngine 纹理
【发布时间】:2011-07-13 13:45:14
【问题描述】:

我是 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

【问题讨论】:

    标签: android textures andengine


    【解决方案1】:

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

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

    这里有一个link,可帮助您从您的Drawable 中创建一个Bitmap

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

    【讨论】:

    • 我想在网格中安排可绘制按钮...我完成了第一部分..在按钮上显示可绘制..但是你知道如何适应网格矩形...任何建议?
    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多