【问题标题】:Algorithm to calculate values based on width and height (coordinates and size)基于宽度和高度(坐标和大小)计算值的算法
【发布时间】:2013-07-16 01:09:33
【问题描述】:

我想要的可能是分成几种或一种方法,最好的。

我设置了四个值(作为字段):

  1. 整个图像的图像宽度(可以说是画布)
  2. 整个图像的高度(画布的高度)
  3. 填充(我希望有多少像素作为填充)
  4. 我想要多少张图片的宽度和高度分别(可以是单独的值)

我想要的是有一个完整的图像,我将小图像放入其中(这些是我想从该方法获得的位图大小)。

我基本上想要内部图像的大小,包括宽度和高度。 以及整个图像视图中小图像的坐标。

一个小例子可能是:

  • 图片的宽度是 200,我想要一个 4 的内边距,我想在图片的一行上有 3 张图片。
  • 计算可能如下所示:

    (200 - (4*(3+1))) / 3 = 61,33;

  • 3+1 只是因为我想在小图像的两侧都有填充。

但这仅适用于宽度,我希望有一个适用于高度的通用解决方案。

并计算 canvas-image 中每个图像的高度和宽度。

只是在上面放一些糖。 我想知道里面每个图像的 x 和 y 坐标。

如何做到这一点? 我如何获得每一个价值? 位图(我正在开发 android)存储在 ArrayList 中。

基本上我想要这个,一个基本的网格布局: http://developer.android.com/images/ui/gridview.png 我基于这张图片的价值观:

  • 我有浅蓝色区域的大小,
  • 包括深蓝色图像之间的空间(填充)
  • 以及我想要在每个 行和列。

我想要的是大小(深蓝色斑点的宽度和高度), 以及这些图像的坐标(它们应该放置在浅蓝色区域的位置,x 和 y 坐标)。

有人对此有好的解决方案吗?

为清晰起见更新了文本

【问题讨论】:

  • 您自己尝试过什么吗?这是一个简单的问题。
  • 在 lib gdx 游戏引擎中有一个精灵打包器,它会从精灵位创建整个图像,并且会有一个包含每个图像坐标的文件
  • @AnttiHuima 我试过了,但我得到的是上面的简单计算,如果我以某种方式关注哪个图像在网格(位置)中,我可以从每个图像中获取坐标值,并且然后从中计算,我也不完全确定如何以一种好的方式去做。但如果这是一个简单的答案,你不能回答然后得到代表吗?

标签: java android algorithm layout coordinates


【解决方案1】:

这是我正在做和想做的代码。 如果有人知道如何改进代码,我会更改我接受的答案。

public class ClassName {

public static int bitmapSizeWidth;
public static int bitmapSizeHeight;
public static final int bitmapPadding = 8;
public static final int howManyImagesColumn = 3;
public static final int howManyImagesRows = 2;

public static Bitmap folderBitmap(ArrayList<Bitmap> bitmapArray, int imageViewWidth, int imageViewHeight) {
    bitmapSizeWidth = imageViewWidth;
    bitmapSizeHeight = imageViewHeight;

    Bitmap b = Bitmap.createBitmap(bitmapSizeWidth, bitmapSizeHeight, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);

    //Lets do it in a set coordinate system now
    if (bitmapArray.size() >= 1)
        c.drawBitmap(bitmapArray.get(0), bitmapPadding, bitmapPadding, paint);

    if (bitmapArray.size() >= 2)
        c.drawBitmap(bitmapArray.get(1), calculateSecondCoord().xPos, bitmapPadding, paint);

    if (bitmapArray.size() >= 3)
        c.drawBitmap(bitmapArray.get(2), calculateThirdCoord().xPos, bitmapPadding, paint);

    if (bitmapArray.size() >= 4)
        c.drawBitmap(bitmapArray.get(3), bitmapPadding, calculateSecondCoord().yPos, paint);

    if (bitmapArray.size() >= 5) {
        c.drawBitmap(bitmapArray.get(4), calculateSecondCoord().xPos, calculateSecondCoord().yPos, paint);
    }

    if (bitmapArray.size() >= 6) {
        c.drawBitmap(bitmapArray.get(5), calculateThirdCoord().xPos, calculateSecondCoord().yPos, paint);
    }

    return b;
}

public static BitmapSize calculateSingleBitmapSize(int imageViewWidth, int imageViewHeight) {
    bitmapSizeWidth = imageViewWidth;
    bitmapSizeHeight = imageViewHeight;
    BitmapSize bsize = new BitmapSize();
    bsize.widthSize = (int) (bitmapSizeWidth -
            (bitmapPadding * (howManyImagesColumn + 1))) / howManyImagesColumn;
    bsize.heightSize = (int) (imageViewHeight - (bitmapPadding * (howManyImagesRows + 1))) / howManyImagesRows;
    return bsize;
}

/*
 * First coord = padding
 * Second coord (bitmapPadding) + calculateSingleBitmapSize(bitmapSizeWidth);
 * Third coord (bitmapPadding * 3) + (calculateSingleBitmapSize(bitmapSizeWidth) * 2)
 * The math is supposed to be correct but can perhaps be done in a more efficient way
 */
private static BitmapCoord calculateSecondCoord() {
    BitmapCoord bCoord = new BitmapCoord();
    bCoord.xPos = (int) (bitmapPadding * (howManyImagesColumn - 1)) + calculateSingleBitmapSize(bitmapSizeWidth, bitmapSizeHeight).widthSize;
    bCoord.yPos = (int) (bitmapPadding * (howManyImagesRows)) + calculateSingleBitmapSize(bitmapSizeWidth, bitmapSizeHeight).heightSize;

    return bCoord;
}

private static BitmapCoord calculateThirdCoord() {
    BitmapCoord bCoord = new BitmapCoord();
    bCoord.xPos = (int) (bitmapPadding * howManyImagesColumn) + calculateSingleBitmapSize(bitmapSizeWidth, bitmapSizeHeight).widthSize * 2;
    bCoord.yPos = (int) (bitmapPadding * howManyImagesRows - 1) + calculateSingleBitmapSize(bitmapSizeWidth, bitmapSizeHeight).heightSize * 2;

    return bCoord;
}

public static class BitmapSize {
    public int widthSize;
    public int heightSize;
}

static class BitmapCoord {
    int xPos;
    int yPos;
}
}

【讨论】:

    【解决方案2】:

    您所描述的是游戏开发者所称的 spritesheets、tilesheets、tilesets 等等。

    所有这些背后的总体思路是,您拥有一个特定类型的图像文件,并在该图像中创建子图像,这些子图像可以被拉出并在您的程序中单独使用。通常在视频游戏中,它们的大小是一致的,但不一定要如此。

    查看此链接上的答案:How to extract part of this image in Java? 这里的答案解释了如何将图像拆分为多个部分。

    如果您在此处找不到所需内容,请查看游戏开发人员如何处理 spritesheets,例如了解他们是如何做到的。

    【讨论】:

    • 我认为他想要你所建议的相反。他已经收集了相同大小的图像,并希望将它们排列成一个更大的图像。您正在谈论如何从较大的图像中提取子图像。
    • 你可能是对的。老实说,虽然我不太了解他的帖子,所以我只是从它那里得到了我能做的。如果您需要在另一个方向上进行操作,可以使用图像处理库:stackoverflow.com/questions/2407113/…
    • 没错,我想反过来。我想要几张小图片中的一张。
    • 现在更新问题,理解上有些问题。
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 2020-12-28
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多