我正在尝试将 libgdx 作为 opengl 包装器,但它的图形渲染存在一些问题:由于某种原因,android 设备上的所有图像(纹理)在使用 libgdx 时看起来都有些模糊。这也包括文本(字体)。
对于文本,我认为这是因为我使用了位图字体,但我找不到替代品 - 我发现有一个名为“gdx-stb-truetype”的库,但我找不到如何下载并使用它。
对于普通图像,即使我在没有任何缩放的情况下显示整个图像,我希望它看起来和我在计算机屏幕上看到的一样清晰,特别是如果我在设备上有这么好的屏幕(它是银河系)。我尝试使用以下代码关闭抗锯齿功能:
final AndroidApplicationConfiguration androidApplicationConfiguration=new AndroidApplicationConfiguration();
androidApplicationConfiguration.numSamples=0; //tried the value of 1 too.
...
我也尝试将缩放方法设置为各种方法,但没有运气。例子:
texture.setFilter(TextureFilter.Nearest,TextureFilter.Nearest);
作为测试,我发现了一个与设备上看到的分辨率完全相同的清晰图像(galaxy nexus 为 720x1184,因为按钮栏),并且我已将其放在 libgdx 的背景上应用程序 。当然,我必须添加额外的空白空间才能加载纹理,所以图像的最终尺寸(包括内容和空白空间)仍然是宽度和高度的 2 的幂(在此为 1024x2048案子) 。在桌面应用程序上,它看起来不错。在设备上,它看起来很模糊。
我注意到的一件奇怪的事情是,当我更改设备的方向(水平 <=> 垂直)时,在旋转动画开始之前的很短的时间内,我可以很好地看到图像和文本。
当然 libgdx 可以处理这个问题,因为 android 的 api-tests 项目的 opengl 部分可以很好地显示图像。
谁能帮帮我吗?
@user1130529:我确实使用 spritebatch。另外,这就是我设置视口的方法。无论我是否选择保持纵横比,都会发生这种情况。
public static final int VIRTUAL_WIDTH =720;
public static final int VIRTUAL_HEIGHT =1280-96;
private static final float ASPECT_RATIO =(float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
...
@Override
public void resize(final int width,final int height)
{
// calculate new viewport
if(!KEEP_ASPECT_RATIO)
{
_viewport=new Rectangle(0,0,Gdx.app.getGraphics().getWidth(),Gdx.app.getGraphics().getHeight());
Gdx.app.log("DEBUG","size:"+_viewport);
return;
}
final float currentAspectRatio=(float)width/(float)height;
float scale=1f;
final Vector2 crop=new Vector2(0f,0f);
if(currentAspectRatio>ASPECT_RATIO)
{
scale=(float)height/(float)VIRTUAL_HEIGHT;
crop.x=(width-VIRTUAL_WIDTH*scale)/2f;
}
else if(currentAspectRatio<ASPECT_RATIO)
{
scale=(float)width/(float)VIRTUAL_WIDTH;
crop.y=(height-VIRTUAL_HEIGHT*scale)/2f;
}
else scale=(float)width/(float)VIRTUAL_WIDTH;
final float w=VIRTUAL_WIDTH*scale;
final float h=VIRTUAL_HEIGHT*scale;
_viewport=new Rectangle(crop.x,crop.y,w,h);
Gdx.app.log("DEBUG","viewport:"+_viewport+" originalSize:"+VIRTUAL_WIDTH+","+VIRTUAL_HEIGHT+" aspectRatio:"+ASPECT_RATIO+" currentAspectRatio:"+currentAspectRatio);
}