使用libgdx裁剪图像 [英] crop image using libgdx

查看:106
本文介绍了使用libgdx裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样裁剪图像

I need to crop image like this

我需要从中心绘制部分图像

I need to draw partial image from center

我知道有很多参数的批处理的draw()方法,但是没有关于所有这些参数的好的文档,所以我不知道如何使用它.

I know there is draw() method of batch with a lot of params, but there is no good documentation about all this params, so i can't figure out how to use it.

这是我实现的:

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();
    }

但是它没有按预期工作

推荐答案

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

To crop the texture you just need to use TextureRegion

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

在您的情况下,其外观应为:

in your case it should look like:

    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%.

Why i set x as texture.getWidth()*0.28f? Because if want it centered it should have left margin = 50% of original texture width - texture region width.

    (1 - 0.44) / 2 = 0.28

这篇关于使用libgdx裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆