我一直在旋转我的轮子,试图了解为什么我的ViewGroup实现自从我开始尝试向我的活动添加一个ActionBar以来一直出现故障。当屏幕上有一个ActionBar时,为什么ViewGroup不能正确绘制?
我有一个自定义的ViewGroup,它根据与视图的当前宽度和高度相关的比率来绘制一组视图。 ViewGroup实现了一种有点像绝对布局的布局算法,但它不是绝对坐标,而是取决于ViewGroup的宽度和高度。
我几乎忽略了布局的onMeasure传递,因为我只是告诉子视图他们的尺寸是多少。
的onLayout做类似以下内容:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
if (isInEditMode()) return;
int height = getHeight();
int width = getWidth();
for (int i = 0; i < getChildCount(); i++)
{
View child = getChildAt(i);
ImageCell cell = cells.get(i);
int l = (int)(cell.getX0() * width);
int t = (int)(height - (cell.getY1() * height));
int r = (int)(cell.getX1() * width);
int b = (int)(height - (cell.getY0() * height));
child.layout(l, t, r, b);
child.setBackgroundColor(0xFFFF0000);
}
}
不幸被设置为我定制的ViewGroup不用油漆本身正常的孩子的意见。这些视图是简单的位图绘图视图,但位图可能会应用缩放和平移。我查看的onDraw()被写入类似以下内容:
@Override
public void onDraw(Canvas canvas)
{
if (canvasMatrix == null)
{
canvasMatrix = new Matrix();
}
canvasMatrix.reset();
canvasMatrix.setScale(scaleX, scaleY);
canvasMatrix.postTranslate(getLeft() + translationX, getTop() + translationY);
canvas.setMatrix(canvasMatrix);
synchronized (renderBuffer)
{
/**
* We need to offset the renderBuffer, the scaling is done by the matrix
* on the canvas.
*/
canvas.drawBitmap(renderBuffer, offsetX, offsetY, null);
}
}
我设立,将应用缩放和平移,以根据需要显示的图像的缩放和平移画布矩阵。然后用指定的x和y偏移量在画布上绘制图像。
似乎在发生的事情是,位图被绘制在ActionBar下方,在屏幕底部留下大量空间,并且未被绘制。如果我添加一个常数系数来抵消Y,则可以使位图在屏幕上的正确位置上绘制(取决于设备的旋转)。但我不确定这个常数因子应该在哪里计算。
我对Android绘图了解甚少。我希望Canvas能够相对于视图在屏幕上的位置,这样如果我在(0,0)处绘制,绘画将出现在视图的原点,而不是屏幕的原点。
所以这个形象是我所看到的一个例子。底部的红色区域是不被drawBitmap绘制的子视图的一部分。
任何想法我在这里做错了吗?如果视图是全屏的话,所有东西都能很好地绘制,但是使用ActionBar时,我没有给出包含ActionBar大小的坐标,所以我似乎正在绘制它。
谢谢,罗曼。我现在重写了我的代码,以确保不会删除原始矩阵,而是将适当的比例和平移添加到现有矩阵。这绝对解决了我的问题。 – Thorinside 2011-12-14 05:19:04