来自 Drawable 的 AndEngine 纹理 [英] AndEngine Texture from Drawable

查看:24
本文介绍了来自 Drawable 的 AndEngine 纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AndEngine 的新手.

I'm new to AndEngine.

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

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

不知道有没有可能,

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

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

推荐答案

我知道这不是解决这个问题的最好方法,但是你可以把你的 Drawable 变成一个 Bitmap 然后从 Bitmap 创建一个 TextureRegion.下面是从 Bitmap 创建 TextureRegion 的代码:

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

    }

这是一个链接,可帮助您制作<Drawable 中的 code>Bitmap.

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!

这篇关于来自 Drawable 的 AndEngine 纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆