我正在尝试为游戏绘制网格,但是我的线条偏离了我想要的位置。我尝试添加固定的偏移量,但希望它在不同的设备上可缩放。
以下是用于创建View绘制路径的代码:
float top = getPaddingTop() + getTop();
float bot = getBottom() - getPaddingBottom();
float left = getLeft() + getPaddingLeft();
float right = getRight() - getPaddingRight();
float squareWidth = (right - left) / 10;
float squareHeight = (bot - top) / 10;
gridPath.moveTo(left, top);
gridPath.lineTo(left, bot);
gridPath.lineTo(right, bot);
gridPath.lineTo(right, top);
gridPath.close();
for (int i = 1; i < 10; i++) {
gridPath.moveTo(squareWidth * i, top);
gridPath.lineTo(squareWidth * i, bot);
}
for (int i = 1; i < 10; i++){
gridPath.moveTo(left, squareHeight * i);
gridPath.lineTo(right, squareHeight * i);
}
gridPath.close();
我有什么想念的吗?
您无需考虑for
循环中的左侧和顶部填充:
for (int i = 1; i < 10; i++) {
gridPath.moveTo(getPaddingLeft() + squareWidth * i, top);
gridPath.lineTo(getPaddingLeft + squareWidth * i, bot);
}
for (int i = 1; i < 10; i++){
gridPath.moveTo(left, getPaddingTop() + squareHeight * i);
gridPath.lineTo(right, getPaddingTop() + squareHeight * i);
}
此外,getLeft()
,getRight()
,getTop()
,getBottom()
方法是相对于View
其父的位置,所以你的初始化会导致问题,如果View
不发生,以填补其母公司。以下可能是更可取的:
float top = getPaddingTop();
float bot = getHeight() - getPaddingBottom();
float left = getPaddingLeft();
float right = getWidth() - getPaddingRight();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句