Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View outside bounds not drawing properly

I'm drawing a tooltip after clicking inside a custom bar chart (created with MPAndroidChart). The view hierarchy is as follows

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

While the View is inside the Chart or its inmediate sibling, everthing looks good. But the moment it collides with its sibling, the tooltip is truncated

enter image description here

Using HierarchyViewer I can see that the content is present, but it's not drawn.

In order to get the clipping, I'm using this code inside 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();
  }

If I change Op to Region.Op.Replace, the tooltip is draw correctly but it replaces the Toolbar content, instead of scrolling under it.

enter image description here

like image 931
Maragues Avatar asked May 14 '15 14:05

Maragues


1 Answers

You'll need the bounds of the area in which you want to be able to draw the tooltip, and I'm assuming that would be a scrollview. Then you can intersect the tooltip bounds with the scroll to work out what the clipping should be; and if it should be drawn at all.

To explain it in code it would be something like this (untested):

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);
}
like image 176
Wex Avatar answered Nov 03 '22 01:11

Wex