使用 libgdx 裁剪图像

2024-05-08

I need to crop image like this 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();
    }

但它没有按预期工作


要裁剪纹理,您只需要使用纹理区域 https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureRegion.html

    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 as 纹理.getWidth()*0.28f?因为如果想让它居中,它应该有左边距 = 50%原始纹理宽度 -纹理区域 width.

    (1 - 0.44) / 2 = 0.28
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 libgdx 裁剪图像 的相关文章

随机推荐

  • R:如何添加具有从矩阵的每一行中随机选择的值的列?

    我会先说我是一个 R 菜鸟 我认为这可能有一个简单的解决方案 但我正在努力寻找它 我有一个 2 列 1 000 行的矩阵 保持行固定 我想创建一个新变量 从两列中随机选择一个元素 例如制作一个简单的矩阵 matrix c 1 1 4 6 1
  • PostgreSQL 中的 LATERAL JOIN 和子查询有什么区别?

    自从 PostgreSQL 推出以来 它具备了以下功能 LATERAL连接 我一直在阅读它 因为我目前为我的团队进行复杂的数据转储 其中有许多低效的子查询 使整个查询需要四分钟或更长时间 我明白那个LATERALjoins 可能可以帮助我
  • java.net.ServerSocket.accept () 在 Android 上不返回

    我正在尝试找到一种方法来远程登录到未root的机器人 我有INTERNET权限处于活动状态 我的设备与我的设备连接在同一网络上Mac OS X通过 WiFi 我可以 ping 通我打开的端口 在最初的实验中 我让它在有根测试设备上工作 但我
  • 使用 PyQt5 拖放 QLabels

    我正在尝试使用 PyQt5 将 Qlabel 拖放到另一个 Qlabel 上 from PyQt5 QtWidgets import QApplication QWidget QToolTip QPushButton QMessageBox
  • UITableView 显示的行数多于 numberOfRowsInSection 中指定的行数:

    我希望我的 tableView 显示 6 行 其中包含文本 在本例中为 示例 据我所知 我有我的numberOfSectionsInTableView and numberOfRowsInSection 设置正确 请参阅下面的示例代码 NS
  • 如何将 man 和 zip 添加到 Windows 上的“git bash”安装中

    我在用git bash https git for windows github io 在 Windows 上 即git对于 Windows 通过集成bash 显然它使用的是MINGW MSYS支撑 来自 VonC 的更新 现在使用 msy
  • 有关 Linux 内存类型的问题

    关于Linux内存我有以下问题 我知道活动内存是最常访问的内存部分 但是有人可以解释一下 linux 如何考虑将内存位置用于活动内存或非活动内存 主动存储器由哪些部分组成 磁盘 文件缓存是否被视为活动内存的一部分 有什么区别Buffers
  • 按行号和列号对文件进行子集化

    我们想要按行和列对文本文件进行子集化 其中行数和列数是从文件中读取的 不包括标题 第 1 行 和行名称 第 1 列 输入文件 txt制表符分隔的文本文件 header 62 9 3 54 6 1 25 1 2 3 4 5 6 96 1 1
  • 现已弃用使用 Google Places API 获取多种类型

    谷歌最近宣布 自 2016 年 2 月 16 日起 types 参数已被弃用 取而代之的是新的类型参数 每个搜索请求仅支持一种类型 我的问题是 现在有什么方法 不使用已弃用的参数 从单个 API 调用中获取多个地点类型吗 谢谢 None
  • dplyr :过滤一系列行(在一列中)

    虚拟数据框 id family lt c 1 1 2 2 3 3 people lt c male female male female male children dataset lt data frame id family peopl
  • 演员邮箱溢出。斯卡拉

    我目前正在与 scala 的两位演员合作 一 producer 产生一些数据并将其发送到parcer 生产者发送一个HashMap String HashMap Object List Int 通过消息 以及this标记发件人 parcer
  • 不透明div内的透明文本

    我有一个背景图像 上面有一个白色的 div 我希望该 div 内的文本是透明的 以便您可以 透过 背景图像 这有可能吗 应该看起来像这样 您需要将其用于您的文本CSS webkit text fill color transparent
  • 使用 Azure 表存储进行代码优先和身份验证

    我正在开发一个小型网络应用程序 并且刚刚达到开发阶段 我需要开始做出数据库决策 我最初的计划是在 Azure 上使用 EF Code First 和 MSSQL 因为它只是简化了使用数据库的过程 然而 当我研究 Azure 上的数据库托管功
  • 具有异步管道多个条件的 Angular *ngIf 变量

    有一个关于在 Angular 中使用 ngIf 的很好的文档 https angular io api common NgIf https angular io api common NgIf但是 是否可以有 ngIf 异步变量并对其进行多
  • iOS 8 使用 UITextView 自动调整 UITableViewCell 大小

    iOS 8 引入了一种让 tableView 根据内容自动调整单元格高度的方法 通过 AutoLayout in viewDidLoad tableView rowHeight UITableViewAutomaticDimension t
  • 引发 404 并继续 URL 链

    我有一个像这样的 URL 模式 urlpatterns url r list titles name list url r P
  • 在 C 或 C++ 中使用逗号作为宏名称

    我想做这样的事情 define define MAX 10 000 000 undef 有什么技巧可以做到吗 编辑 我知道 C 14 中的数字分隔符 我正在寻找一种技巧来对不兼容的编译器执行相同的操作 EDIT2 请考虑Variadic M
  • ASP.NET 搜索表单 - 动态 Linq to SQL?

    我有一个搜索表单 允许用户以多种不同的方式搜索多个不同的字段 这是我的代码的示例 var claims from c in db Claims select c switch ddlSearchField Text case StartsW
  • Twitter 不再使用请求库 python

    我有一个 python 函数 它使用 requests 库和 BeautifulSoup 来抓取特定用户的推文 import requests from bs4 import BeautifulSoup contents requests
  • 使用 libgdx 裁剪图像

    I need to crop image like this 我需要从中心绘制部分图像 我知道有一个带有很多参数的批处理的draw 方法 但是没有关于所有这些参数的良好文档 所以我不知道如何使用它 这是我实现的 public class T