Android SurfaceView 中的分辨率独立性

发布于 2024-08-18 20:05:18 字数 154 浏览 9 评论 0原文

我目前正在 Android 中启动一个游戏引擎,首先涉足该平台并具备基础知识,但是我不确定在使用 SurfaceView 绘制图形时实现分辨率独立性的最佳方法。

寻找关于如何保持游戏/精灵等在屏幕上看起来相同的指针,显然,缩放每帧的所有精灵或存储不同分辨率的许多变化是没有效率的

I am currently starting a game engine in Android, first foray into the platform and have the basics in place however i am unsure of the best way to approach resolution independence when using SurfaceView to draw graphics.

Looking for pointers as to how to keep the game / sprites etc all looking the same independent of the screen, obviously it wouldn't be efficient to scale all the sprites every frame or store many variations for differing resolutions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-08-25 20:05:18

您只需在加载精灵时缩放它们即可。我猜你是从资源中将它们作为位图加载的,对吗?如果是这种情况,那么您需要做的就是这样:

BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight = spriteHeight * scale;
options.outWidth = spriteWidth * scale;

Bitmap sprite = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sprite, options)

其中比例基于屏幕尺寸的变化。

You'd just scale the sprites when you load them. I am guessing you're loading them as Bitmaps from a Resource, right? If that's the case then all you need to do it something like this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight = spriteHeight * scale;
options.outWidth = spriteWidth * scale;

Bitmap sprite = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sprite, options)

Where scale is based on the change of the screen size.

乙白 2024-08-25 20:05:18

有两种简单的方法:

第一:

在创建 Surface 视图时,它调用:

onSizeChanged  (int w, int h, int oldw, int oldh)

创建一些全局变量 x 和 x 。 y 因此,如果您使用以下命令覆盖它:

onSizeChanged(int w, int h, int oldw, int oldh){
this.w=w;
this.h=h;
}

那么在任何绘图或游戏计算中,您可以使用窗口 w 和 h。

第二:

调用 getWidth() & getHeight() 在绘图和游戏计算中。

祝你游戏愉快!

There are two simple approaches:

First:

On creation of a Surface view, it calls:

onSizeChanged  (int w, int h, int oldw, int oldh)

Create a few global variables, x & y so if you override this with:

onSizeChanged(int w, int h, int oldw, int oldh){
this.w=w;
this.h=h;
}

Then in any drawing or game calculations you can use the window w and h.

Second:

call getWidth() & getHeight() in drawing and game calculations.

Good luck with your game!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文