【问题标题】:Bounds of an ImageButtonImageButton 的边界
【发布时间】:2016-08-03 10:40:25
【问题描述】:

我有一个按钮 (reloadButton),它是一个 ImageButton

我想给它setBounds(),但问题是它的ImageButtonStyle.imageUpImageButtonStyle.imageDown 是圆形图像,我不想让边界变成矩形,因为这个按钮可以被触摸

我怎样才能setBounds这个按钮?

这是我的代码

TextureRegion buttonUp = Assets.getTextureRegion(Assets.getTextureAtlas(Assets.reloadButton), "up"); 

TextureRegion buttonDown = Assets.getTextureRegion(Assets.getTextureAtlas(Assets.reloadButton), "down"); 

ImageButton.ImageButtonStyle buttonStyle = new ImageButton.ImageButtonStyle(); 

buttonStyle.imageUp = new TextureRegionDrawable(buttonUp); 

buttonStyle.imageDown = new TextureRegionDrawable(buttonDown); 

reloadButton = new ImageButton(buttonStyle);

 // reloadButton.addListener()

【问题讨论】:

  • 您能提供您当前的代码吗?
  • 我不认为你可以对setBounds 这么具体。您可以覆盖Actor.fire(Event) 方法并检查您的圆角矩形是否包含该点(尽管您需要半径)。

标签: java android libgdx scene2d


【解决方案1】:

Actor 边界总是一个矩形。如果您想测试不同的形状,请覆盖 Actor 的 hit 方法。例如,对于圆角矩形,子类化 ImageButton 并执行此操作(其中rad 是圆角半径):

@Override
public Actor hit (float x, float y, boolean touchable) {
    Actor hit = super.hit(x, y, touchable); //is in rectangle

    if (hit != null){ //reject corners if necessary
        boolean keep = true;
        if (x < rad){
            if (y < rad) keep = inCircle(x, y, rad, rad, rad);
            else if (y > getHeight() - rad) keep = inCircle(x, y, rad, getHeight() - rad, rad);
        } else if (x > getWidth() - rad){
            if (y < rad) keep = inCircle(x, y, getWidth() - rad, rad, rad);
            else if (y > getHeight() - rad) keep = inCircle(x, y, getWidth() - rad, getHeight() - rad, rad);
        }
        if (!keep) hit = null;
    }
    return hit;
}

private boolean inCircle(float x, float y, float centerX, float centerY, float radius) {
    float dx = x - centerX;
    float dy = y - centerY;
    return dx * dx + dy * dy <= radius * radius;
}

【讨论】:

  • 但 super.hit(x, y, touchable) 返回 Actor 不是布尔值
  • @MAGS94 哎呀!固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多