ImageButton 的边界
Bounds of an ImageButton
我有一个按钮 (reloadButton),它是一个 ImageButton
我想 setBounds()
但问题是它的 ImageButtonStyle.imageUp
和 ImageButtonStyle.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()
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;
}
我有一个按钮 (reloadButton),它是一个 ImageButton
我想 setBounds()
但问题是它的 ImageButtonStyle.imageUp
和 ImageButtonStyle.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()
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;
}