Java TextButton背景上的NinePatch渲染问题(libgdx)
我正在测试使用一个舞台作为一个主菜单,使用一个简单的按钮,使用一个NinePatch作为按钮背景 设置此设置的代码如下所示: NinePatch文本按钮设置Java TextButton背景上的NinePatch渲染问题(libgdx),java,libgdx,nine-patch,scene2d,Java,Libgdx,Nine Patch,Scene2d,我正在测试使用一个舞台作为一个主菜单,使用一个简单的按钮,使用一个NinePatch作为按钮背景 设置此设置的代码如下所示: NinePatch文本按钮设置 NinePatch btnNormal9 = NinePatchHelper.processNinePatchFile("data/button_normal.9.png"); NinePatchDrawable btnNormal9Drawable = new NinePatchDrawable(btnNormal9); TextBu
NinePatch btnNormal9 = NinePatchHelper.processNinePatchFile("data/button_normal.9.png");
NinePatchDrawable btnNormal9Drawable = new NinePatchDrawable(btnNormal9);
TextButtonStyle style = new TextButtonStyle(btnNormal9Drawable, btnNormal9Drawable, btnNormal9Drawable);
style.font = new BitmapFont();
style.fontColor = new Color(1, 1, 1, 1);
button = new TextButton("Start Game", style);
button.setSize(200, 100);
button.setPosition(0, 0);
stage.addActor(button);
然后,在我的调整大小功能中,我现在只需执行以下操作: 调整代码大小
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
无论窗口的大小(即摄影机视口的大小),按钮都应位于屏幕上的(0,0)处,且为200像素乘以100像素 NinePatch图像(“data/button_normal.9.png”)为26x26像素。但使用以下代码(取自此处:)裁剪为具有4px分割的24x24纹理: 创建NinePatch
public static NinePatch processNinePatchFile(String filename) {
final Texture t = new Texture(Gdx.files.internal(filename));
final int width = t.getWidth() - 2;
final int height = t.getHeight() - 2;
return new NinePatch(new TextureRegion(t, 1, 1, width, height), 4, 4, 4, 4);
}
只要窗口尺寸为偶数(例如640x400像素),此功能就可以正常工作,如下面的屏幕截图所示: 但是,如果窗口尺寸为奇数,例如639x401像素,则无法正确缩放NineMatch: 如果只有高度是奇数,则渲染错误仅发生在NinePatch的垂直缩放中,如下所示: 我不明白为什么会发生这种情况,因为按钮的大小和位置在任何情况下都是硬编码相同的,所以NinePatch的绘制应该完全相同 如果有人能提供任何帮助,我将不胜感激
提前谢谢。好吧,我想我意识到发生了什么,我太傻了 如果屏幕尺寸为奇数,则屏幕中心不再是整数像素值,而是.5值。这意味着在整数位置绘制的任何内容都将模糊 我能想到的解决这个问题的最简单方法是改变resize方法。如果高度或宽度为奇数,请将相机的x或y位置设置为0.5,而不是0,如下所示。这解决了问题:)
if (width % 2 != 0) guiCamera.position.x = 0.5f;
else guiCamera.position.x = 0f;
if (height % 2 != 0) guiCamera.position.y = 0.5f;
else guiCamera.position.y = 0f;