2

我正在尝试在 ImageView 扩展中创建一个动态网格。在 Android 4.0.3 上它看起来不错,但在 2.2 上它在右侧和底部被拉伸,因此我在这些边缘上的单元格看起来两倍大。

这是创建网格的代码:

cellheight = Math.round((float) (Math.floor((height - (bordersize * 2) - (linesize * (numrows - 1))) / numrows)));
cellwidth = Math.round((float) (Math.floor((width - (bordersize * 2) - (linesize * (numcols - 1))) / numcols)));
height = bordersize * 2 + (linesize * (numrows - 1)) + cellheight * numrows;
width = bordersize * 2 + (linesize * (numcols - 1)) + cellwidth * numcols;

bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
setImageBitmap(bitmap);

canvas.drawRect(0, 0, width, height, cellpaint);
canvas.drawRect(0, 0, bordersize, height, borderpaint);
canvas.drawRect(0, 0, width, bordersize, borderpaint);
canvas.drawRect(0, height - bordersize, width, height, borderpaint);
canvas.drawRect(width - bordersize, 0, width, height, borderpaint);

for (int i = 1; i < numrows; ++i)
  canvas.drawRect(bordersize, bordersize + (i * cellheight) - (linesize / 2), width - bordersize, 
    bordersize + (i * cellheight) + (linesize / 2), linepaint);

for (int i = 1; i < numcols; ++i)
  canvas.drawRect(bordersize + (i * cellwidth) - (linesize / 2), bordersize, 
    bordersize + (i * cellwidth) + (linesize / 2), height - bordersize, linepaint);

对于高度和宽度 300、numrows 和 numcols 8、bordersize 8 和 linesize 3,它将高度和宽度重新计算为 293 并制作一个方形网格。但是在 2.2 中,该网格在右侧和底部被拉伸,将边框和线条延伸到几乎另一行/列单元格的大小。

我已经在 ImageView 和包装 LinearLayout 上尝试了 WRAP_CONTENT。我尝试了 setScaleType() 的各种值。我试过 setMaxHeight() 和 setMaxWidth()。我尝试过的任何事情似乎都无法阻止 2.2 的拉伸。

有任何想法吗?

4

0 回答 0