来自 Drawable 的 AndEngine 纹理

发布于 2024-11-19 14:52:53 字数 1325 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2024-11-26 14:52:53

我知道这不是解决此问题的最佳方法,但您可以将 Drawable 转换为 Bitmap,然后从位图。
以下是从 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);
        }

    }

这是 链接可帮助您从 Drawable 制作位图

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文