2013-07-17 56 views
0

我想要的可能是分成几种或一种方法,无论哪种方法最好。根据宽度和高度(坐标和大小)计算值的算法

我有一个设置(如字段)四个值:

  1. 宽度图像的整体形象(帆布这么说)
  2. 身高整个图像的(画布的高度)
  3. 填充(我想多少像素具有作为填充)
  4. 多少图像我想在单独的宽度和高度(可以是单独的值)

我想要的是有一个完整的图像,我把小图像放在里面(这些是我想从方法中获得的位图大小)。

我基本上想要里面的图像的大小,宽度和高度。 在整个图像视图中的小图像的坐标。

一个小例子可以是:

  • 图像的宽度是200,我想为4的填充,我想上的图像的一排有3个图像。
  • 计算将可能是这样的:

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

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

但是这只是宽度,我想有一个适用于高度的通用解决方案。

并计算画布图像内每个图像的高度和宽度。

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

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

基本上我想这样,一个基本的网格布局: http://developer.android.com/images/ui/gridview.png 值我已经基于该图像:

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

我想是尺寸(宽度和深蓝色斑点的高度), 和这些图像(其中,它们应该被放置在淡蓝色区域,x和y坐标)的坐标。

有人有这个好的解决方案吗?

更新的CLARITY

+0

你有没有试过自己的东西?这是一个简单的问题。 –

+0

在libgdx游戏引擎中有一个精灵包装器,它会创建整个图像的精灵比特,并将有一个文件包含每个图像坐标 –

+0

@AnttiHuima我试过了,但我得到的是从上面的简单caclulation,我可以得到如果我以某种方式保持眼睛中的图像位于网格中(位置),然后从该图像中分离出来,我不能确定如何以良好的方式做到这一点。但如果这是一个简单的答案,你不能回答,然后得到代表? –

回答

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; 
} 
} 
1

什么你所描述的是什么游戏开发人员调用spritesheets,tilesheets,tilesets,或任何文字。

所有这些背后的一般想法是你有一个特定类型的图像文件,并在该图像中创建子图像,可以在程序中单独使用这些子图像。通常在视频游戏中这些尺寸一致,但不一定是。

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

如果你找不到你要找的东西,那么请查看游戏开发者如何处理spritesheet,以便了解他们如何做到这一点。

+0

我想他想要的是你的建议。他已经收集了大小相同的图像,并且希望将它们排列成一个更大的图像。您正在讨论如何从较大的图像中提取子图像。 – Kevin

+0

你可能是对的。说实话,虽然我不太了解他的帖子,所以我只是从中拿走了我能做的。如果你需要在另一个方向做图像处理库,你可以使用:http://stackoverflow.com/questions/2407113/open-source-image-processing-lib-in-java –

+0

这是正确的我想要它反过来。我想从几个小的图像中找出一张图像。 –

相关问题