查看外部边界未正确绘制

时间:2015-05-14 14:25:42

标签: android android-canvas android-custom-view mpandroidchart

我点击自定义条形图(使用MPAndroidChart创建)后绘制工具提示。视图层次结构如下

<LinearLayout>
     <TextView text=Move & Max Pain/>
     <RelativeLayout with 2 textviews>
     <chart
       clipToChildren=false
       clipToPadding=false
     />
</LinearLayout>

虽然View位于Chart或其中间兄弟中,但看起来很好。但是当它与它的兄弟碰撞时,工具提示被截断

enter image description here

使用HierarchyViewer我可以看到内容已存在,但未绘制。

为了获得剪辑,我在draw

中使用了这段代码
    @Override
  public void draw(Canvas canvas, float posx, float posy) {
    // take offsets into consideration
    posx += getXOffset();
    posy += getYOffset();

    canvas.save();

    // translate to the correct position and draw
    canvas.translate(posx, posy);

    Rect clipBounds = canvas.getClipBounds();
    clipBounds.inset(0, -getHeight());
    canvas.clipRect(clipBounds, Region.Op.INTERSECT);

    draw(canvas);
    canvas.translate(-posx, -posy);

    canvas.restore();
  }

如果我将Op更改为Region.Op.Replace,工具提示会正确绘制,但它会替换工具栏内容,而不是在其下滚动。

enter image description here

1 个答案:

答案 0 :(得分:2)

你需要你想要能够绘制工具提示的区域的边界,我假设这将是一个scrollview。然后,您可以将工具提示边界与滚动相交,以确定裁剪应该是什么;如果它应该被绘制。

要在代码中解释它,就像这样(未经测试):

Rect scrollViewRect;  // the bounds of your scrollview
Rect tooltipRect;     // the bounds of your tooltip

bool intersects = tooltipRect.intersect(scrollViewRect)
if(intersects)
{
    canvas.clipRect(tooltipRect, Region.Op.REPLACE);
    draw(canvas);
}