AndEngine纹理从绘制对象 [英] AndEngine Texture from Drawable
问题描述
我是新来的AndEngine。
由于某些原因,我要创建从绘制对象变量TextureRegion。
我不知道这是否是可能的,
但我的code不工作...
公共类DrawableTextureSource实现ITextureSource {
私人最终诠释mWidth;
私人最终诠释mHeight;
私人最终绘制对象mDrawable;
私人最终语境mContext;
公共DrawableTextureSource(上下文的背景下,可绘制D){
mContext =背景;
mDrawable = D;
mWidth = D.getIntrinsicWidth();
mHeight = D.getIntrinsicHeight();
} //结束DrawableTextureSource()
公共DrawableTextureSource(上下文的背景下,可绘制研发,INT W,INT高){
mContext =背景;
mDrawable = D;
mWidth = W;
mHeight = H;
} //结束DrawableTextureSource()
公众诠释的getWidth(){
返回mWidth;
} //结束的getWidth()
公众诠释的getHeight(){
返回mHeight;
} //结束的getHeight()
公共位图onLoadBitmap(配置pBitmapConfig){
点阵位图= Bitmap.createBitmap(1024,1024,pBitmapConfig);
mDrawable.draw(新的Canvas(位图));
返回的位图;
} // END onLoadBitmap()
公共DrawableTextureSource的clone(){
返回新DrawableTextureSource(mContext,mDrawable,mWidth,mHeight);
} //结束的clone()
} //结束类
我知道这是不是解决这个问题的最好办法,但你可以把你的绘制对象
来一个位图
,然后创建一个 TextureRegion
从位图
。
这里的$ C $下创建一个 TextureRegion
从位图
:
公共类BitmapTextureSource实现ITextureSource {
私人位图mBitmap = NULL;
公共BitmapTextureSource(位图位图){
this.mBitmap =位图;
}
@覆盖
公众诠释的getWidth(){
返回mBitmap.getWidth();
}
@覆盖
公众诠释的getHeight(){
返回mBitmap.getHeight();
}
@覆盖
公共位图onLoadBitmap(){
返回mBitmap.copy(mBitmap.getConfig(),FALSE);
}
@覆盖
公共BitmapTextureSource的clone(){
返回新BitmapTextureSource(mBitmap);
}
}
下面是一个链接,以帮助你做出位图
从绘制对象
。
希望你会发现一个更简单的方法,但这应该做的工作也是如此。祝你好运!
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
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!
这篇关于AndEngine纹理从绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!