【问题标题】:On Android, how to draw a Rect like pygame Rect? (x, y, w, h)在 Android 上,如何像 pygame Rect 一样绘制 Rect? (x, y, w, h)
【发布时间】:2014-04-08 19:58:32
【问题描述】:

有没有办法在 Android 中按照以下模式绘制 Rect,而不是“左、上、右、下”?问题是我使用的是随机坐标:

Rect e = new Rect(random.nextInt(300)+20,20,random.nextInt(300)+45,70);

而且很难跟踪第一个左侧随机位置以包含在右侧的宽度中。在“x,y,w,h”中,很容易应用我的碰撞测试:

if ((((p.left+p.width())>e.left)&&(p.left<(e.left+e.width())))&&
       (((p.top+p.height())>e.top)&&(p.top<(e.top+e.height())))) {
    gameScreen = 2;
}

【问题讨论】:

    标签: android position height width rect


    【解决方案1】:

    只需将随机数移到矩形的构造函数之外。

    int left = random.nextInt(300)+20;
    int right = left + 20;
    int top = random.nextInt(300)+45;
    int bottom =  top + 70;
    
    Rect e = new Rect(left, top, right, bottom);
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2015-05-19
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多