使用libgdx裁剪图像

6
我需要像这样裁剪图片: enter image description here 我需要从中心绘制部分图像。
我知道批处理的draw()方法有很多参数,但是没有关于所有这些参数的良好文档,因此我无法弄清如何使用它。
这是我实现的内容:
public class TexturePart {

    Texture tex;
    Vector2 position;

// Target Dimension of image

    int targetWidth;
    int targetHeight;

// Src Dimensions of Image

    int srcWidth;
    int srcHeight;
    int srcX;
    int srcY;

// Ratio of dimension of target and source

    float srcTargetRatioX;
    float srcTargetRatioY;

// ImagePart variables with values between 0-100 to draw part of image

    int startPercentX;
    int endPercentX;
    int startPercentY;
    int endPercentY;

    int clipWidth;
    int clipHeight;

    int clipSrcWidth;
    int clipSrcHeight;

    public TexturePart(TextureRegion reg, float x, float y) {

        tex = reg.getTexture();
        position = new Vector2(x, y);

        srcX = reg.getRegionX();
        srcY = reg.getRegionY();

        srcWidth = reg.getRegionWidth();
        srcHeight = reg.getRegionHeight();

        clipSrcWidth = srcWidth;
        clipSrcHeight = srcHeight;
        startPercentX = 28;
        startPercentY = 28;
        endPercentX = 72;
        endPercentY = 72;
        SetTargetDimension(srcWidth, srcHeight);
    }

    public void setSrcWidthHeight(int width, int height){
        this.srcWidth=width;
        this.srcHeight=height;
    }

    public void setSrcHeight(int height){
        this.srcHeight=height;
    }

    public void SetTargetDimension(int targetWidth, int targetHeight) {
        this.targetWidth = targetWidth;
        this.targetHeight = targetHeight;
        clipWidth = targetWidth;
        clipHeight = targetHeight;
        srcTargetRatioX = (float) targetWidth / (float) srcWidth;
        srcTargetRatioY = (float) targetHeight / (float) srcHeight;
    }

    public void SetStart(int x, int y) {
        startPercentX = x;
        startPercentY = y;
    }

    public void SetEnd(int x, int y) {
        endPercentX = x;
        endPercentY = y;
    }

    public void Draw(SpriteBatch sp) {

        clipSrcWidth = (int) (Math.abs(startPercentX - endPercentX) / 100f * srcWidth);
        clipSrcHeight = (int) (Math.abs(startPercentX - endPercentY) / 100f * srcHeight);
        int startX = clipWidth/2 + (int) ((float) startPercentX / 100f * (float) srcX);
        int startY = clipHeight/2 + (int) ((float) startPercentY / 100f * (float) srcY);
        clipWidth = (int) (srcTargetRatioX * clipSrcWidth);
        clipHeight = (int) (srcTargetRatioY * clipSrcHeight);
        sp.begin();

        float scaleX=targetWidth/(srcWidth+0.f);
        float scaleY=targetHeight/(srcHeight+0.f);

        sp.draw(tex, 0, 0, srcWidth, srcHeight, srcWidth, srcHeight, 1, 1, 0, startX, startY, clipSrcWidth, clipSrcHeight, false, false);

        //sp.draw(tex, 0,0,clipWidth, clipHeight, clipWidth, clipHeight, clipSrcWidth, clipSrcHeight, false, false);
        sp.end();
    }

但是它没有按预期工作。


你尝试了什么?你想要什么结果?你得到了什么结果?“不按预期工作”对我们来说不太清楚,无法提供帮助。 - pomeh
@pomeh 我已经接受了正确的答案,抱歉没有提供更多细节。 - Jenya Kirmiza
1个回答

8

要裁剪纹理,您只需使用TextureRegion

    TextureRegion(Texture texture, int x, int y, int width, int height) 

在您的情况下,它应该是这样的:
    Texture texture; //this is your original image

    ...

    TextureRegion region = new TextureRegion(texture, texture.getWidth()*0.28f, 0, texture.getWidth()*0.44f, texture.getHeight() );

    ...

    //now you can just draw your texture region
    sp.draw(region); //you can also use other versions of draw to set region position on screen and so on

为什么我将x设置为texture.getWidth()*0.28f呢?因为如果要使其居中,它的左边缘应该是原始纹理宽度的50%减去纹理区域宽度。

    (1 - 0.44) / 2 = 0.28

谢谢你,对我有帮助。我之前知道0.28,感谢你的解释。还可以再告诉我一件事吗?我想改变纹理的宽度并且希望它能正确缩放,应该使用什么? - Jenya Kirmiza
1
首先使用texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);来保持平滑(了解更多),然后在绘制时使用draw(Texture texture, float x, float y, float width, float height)进行缩放,传入所需的宽度和高度即可。 - m.antkowicz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接