如何将方形网格缩放到任何手机的尺寸 [英] How to scale a square grid to any phone's dimensions

查看:85
本文介绍了如何将方形网格缩放到任何手机的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在画布上制作一个8x8的正方形网格.我设法制作了一个网格,但是它实际上是矩形的,但是对于我正在制作的游戏,它必须是正方形的.如何更改代码,使其变成可缩放到手机的正方形网格.

Hi I'm trying to make a square 8x8 grid on a canvas. I've managed to make a grid, but it turns out to be rectangular, but for the game I'm making it needs to be square. How do I change my code to make it a square grid scaled to the phone.

float testWidth = (getWidth() - 16f) / 9f;
float testHeight = (getHeight() - 16f) / 9f;
for (int i = 0; i < 9; i++) {
            canvas.drawLine(padding + testWidth* i, padding, padding
                    + testWidth * i, testHeight* 8+padding, dark);
            canvas.drawLine(padding,  padding+testHeight* i, testWidth* 8
                    + padding, padding+testHeight* i, dark);
        }

我现在可以制作一个正方形网格,但是我不知道如何将网格居中放置在手机中央

I can now make a square grid, but I don't know how to center the grid into the middle of the phone

推荐答案

您将要选择两者中最短的一个(宽度或高度),并以此来构建网格. (以便您的网格可以显示在屏幕上)

You'll want to take the shortest of the two (Width or Height) and use that to build the grid upon. (So your grid can fit on the screen)

类似...的东西:

float gridSide = 0;
if (getWidth() > getHeight()) {
  gridSide = getHeight();
}
else {
  gridSide = getWidth();
}

appsroxcom 提供的简单逻辑:

float gridSide = Math.min(testWidth(), testHeight());

使用 gridSide 作为网格的总长度总宽度

Use gridSide as the total length and total width of the grid

这篇关于如何将方形网格缩放到任何手机的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆