libgdx有个虚拟分辨率的概念,我们在初始化Stage时,可以传入我们
定义的虚拟分辨率,
new Stage(new ScalingViewport(Scaling.stretch, 480, 800, new OrthographicCamera()));
Scaling.stretch 这个是你的适配策略,适配策略可以选择下面的这些:
<pre name="code" class="java"> /** Scales the source to fit the target while keeping the same aspect ratio. This may cause the source to be smaller than the * target in one direction. */ fit, /** Scales the source to fill the target while keeping the same aspect ratio. This may cause the source to be larger than the * target in one direction. */ fill, //fillX这个适配策略是:宽度即X方向,缩放到和屏幕一样大小,即fill the target in the x direction,而且高度即Y方向 //上根据X方向的缩放比进行缩放(keeping the same aspect ratio),这样图形就不会变形,以为X,Y方向上的缩放比相同, //当时会出现一个问题,就是Y方向上可能出现黑边,也可能超出显示屏幕。这种对宽度要求固定的可以使用。对高度做些处理。 //比如我们根据宽度的缩放比,计算出虚拟高度,然后把我们的图片根据这个虚拟高度摆放。 /** Scales the source to fill the target in the x direction while keeping the same aspect ratio. This may cause the source to be * smaller or larger than the target in the y direction. */ fillX, /** Scales the source to fill the target in the y direction while keeping the same aspect ratio. This may cause the source to be * smaller or larger than the target in the x direction. */ fillY, //stretch长和宽都完全缩放到和屏幕一样大小,这样可能产生变形,如720*1280的屏幕,高度方向的缩放比1208/800 = 1.6 //宽度方向的缩放比720/480 = 1.5,那么,因为长度和宽度方向的缩放比不同,就会到时我们的图片显示到 //屏幕上时,出现变形拉伸。 /** Scales the source to fill the target. This may cause the source to not keep the same aspect ratio. */ stretch, /** Scales the source to fill the target in the x direction, without changing the y direction. This may cause the source to not * keep the same aspect ratio. */ stretchX, /** Scales the source to fill the target in the y direction, without changing the x direction. This may cause the source to not * keep the same aspect ratio. */ stretchY, /** The source is not scaled. */ none;
480,800是虚拟分辨率,我们做图片时,就按照这个
尺寸作图就可以了,而且我们做界面,排布局时,也根据这个大小,放置我们的UI元素,显示到屏幕上时,会根据我们的适配策略进行缩放,
然后显示到屏幕上。
时间: 2024-10-10 09:35:19