和引擎背景图片
And Engine background image
最近我开始研究AndEngine。
我正在尝试设置场景的背景图片。
但由于某种原因,背景图像没有完全填满屏幕。
任何建议和帮助?
这是我的源代码:
public class MainActivity extends BaseGameActivity {
Scene sc;
protected final static int cam_wid = 320;
protected final static int cam_heigh = 456;
BitmapTextureAtlas texAtlas;
ITextureRegion texRegion;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
Camera ca = new Camera(0, 0, cam_wid, cam_heigh);
EngineOptions en = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
cam_wid, cam_heigh), ca);
return en;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
private void load() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
texAtlas = new BitmapTextureAtlas(getTextureManager(), 256, 512);
texRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
texAtlas, this, "imagery.png", 0, 0);
texAtlas.load();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
this.sc = new Scene();
Sprite s = new Sprite(0, 0, cam_wid, cam_heigh, texRegion,
this.mEngine.getVertexBufferObjectManager());
SpriteBackground sb = new SpriteBackground(s);
sc.setBackground(sb);
pOnCreateSceneCallback.onCreateSceneFinished(sc);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
像下面这样获取设备的宽度和高度并使用它
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
cam_wid =dm.widthPixels;
cam_heigh =dm.heightPixels;
另一件事您的背景图片可能比您的屏幕小(高度更小)。
因此,要么缩放图片,要么重复。在最后一种情况下,您可以将纹理图集设置为 TextureOptions.REPEATING_BILINEAR
,然后让精灵变大。
最近我开始研究AndEngine。
我正在尝试设置场景的背景图片。
但由于某种原因,背景图像没有完全填满屏幕。
任何建议和帮助?
这是我的源代码:
public class MainActivity extends BaseGameActivity {
Scene sc;
protected final static int cam_wid = 320;
protected final static int cam_heigh = 456;
BitmapTextureAtlas texAtlas;
ITextureRegion texRegion;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
Camera ca = new Camera(0, 0, cam_wid, cam_heigh);
EngineOptions en = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
cam_wid, cam_heigh), ca);
return en;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
private void load() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
texAtlas = new BitmapTextureAtlas(getTextureManager(), 256, 512);
texRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
texAtlas, this, "imagery.png", 0, 0);
texAtlas.load();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
this.sc = new Scene();
Sprite s = new Sprite(0, 0, cam_wid, cam_heigh, texRegion,
this.mEngine.getVertexBufferObjectManager());
SpriteBackground sb = new SpriteBackground(s);
sc.setBackground(sb);
pOnCreateSceneCallback.onCreateSceneFinished(sc);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
像下面这样获取设备的宽度和高度并使用它
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
cam_wid =dm.widthPixels;
cam_heigh =dm.heightPixels;
另一件事您的背景图片可能比您的屏幕小(高度更小)。
因此,要么缩放图片,要么重复。在最后一种情况下,您可以将纹理图集设置为 TextureOptions.REPEATING_BILINEAR
,然后让精灵变大。