我不确定解决这个问题的
推荐方法是什么,但其中一种方法是创建一个类,该类是
TiledDrawable
和
NinePatch
的组合,并让该类扩展
BaseDrawable
。
这样,您可以重写
draw
方法,首先将背景绘制为
TiledDrawable
(偏移量考虑
NinePatch
的插图),然后再将
NinePatch
作为边框绘制。
例如:
package com.bornander.sandbox;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TransformDrawable;
public class BorderedTiledDrawable extends BaseDrawable implements TransformDrawable {
private TiledDrawable region;
private NinePatch patch;
private int left;
private int top;
private int right;
private int bottom;
public BorderedTiledDrawable(Texture background, Texture border, int left, int top, int right, int bottom) {
region = new TiledDrawable(new TextureRegion(background));
this.patch = new NinePatch(border, left, top, right, bottom);
this.left = left - 1;
this.top = top - 1;
this.right = right - 1;
this.bottom = bottom - 1;
setMinWidth(patch.getTotalWidth());
setMinHeight(patch.getTotalHeight());
setTopHeight(patch.getPadTop());
setRightWidth(patch.getPadRight());
setBottomHeight(patch.getPadBottom());
setLeftWidth(patch.getPadLeft());
}
@Override
public void draw(Batch batch, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) {
region.draw(batch, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
patch.draw(batch, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
}
@Override
public void draw(Batch batch, float x, float y, float width, float height) {
region.draw(batch, x + left, y + top, width - (left + right), height - (top + bottom));
patch.draw(batch, x, y, width, height);
}
}
这是它的样子:
![平铺和九宫格](https://istack.dev59.com/C0Za7.webp)
如果你的九宫格有非常圆润的角落,它可能不能完全工作,但这也很容易调整。