ImageButton

时间:2016-08-03 10:40:26

标签: java android libgdx scene2d

我有一个按钮(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()

1 个答案:

答案 0 :(得分:1)

演员界限总是一个矩形。如果要测试其他形状,请覆盖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;
}