gpt4 book ai didi

android - 使用 libgdx 裁剪图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:17 25 4
gpt4 key购买 nike

我需要像这样裁剪图像 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();
}

但它没有按预期工作

最佳答案

要裁剪纹理,您只需要使用 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% original texture width - texture region width.

    (1 - 0.44) / 2 = 0.28

关于android - 使用 libgdx 裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com